{% import "lib/githubLinks.html" as github -%}
{% import "lib/paramList.html" as params -%}

{%- macro renderHeritage(exportDoc) -%}
  {%- if exportDoc.extendsClauses.length %} extends {% for clause in exportDoc.extendsClauses -%}
  <a class="code-anchor" href="{$ clause.doc.path $}">{$ clause.text $}</a>{% if not loop.last %}, {% endif -%}
  {% endfor %}{% endif %}
  {%- if exportDoc.implementsClauses.length %} implements {% for clause in exportDoc.implementsClauses -%}
  <a class="code-anchor" href="{$ clause.doc.path $}">{$ clause.text $}</a>{% if not loop.last %}, {% endif -%}
  {% endfor %}{% endif %}
{%- endmacro -%}

{%- macro renderMembers(doc) -%}
  {%- for member in doc.staticProperties %}{% if not member.internal %}
  <a class="code-anchor" href="{$ doc.path $}#{$ member.anchor | urlencode $}">{$ renderMemberSyntax(member, 1) $}</a>{% endif %}{% endfor -%}
  {% for member in doc.staticMethods %}{% if not member.internal %}
  <a class="code-anchor" href="{$ doc.path $}#{$ member.anchor | urlencode $}">{$ renderMemberSyntax(member, 1) $}</a>{% endif %}{% endfor -%}
  {% if doc.constructorDoc and not doc.constructorDoc.internal %}
  <a class="code-anchor" href="{$ doc.path $}#{$ doc.constructorDoc.anchor | urlencode $}">{$ renderMemberSyntax(doc.constructorDoc, 1) $}</a>{% endif -%}
  {% for member in doc.properties %}{% if not member.internal %}
  <a class="code-anchor" href="{$ doc.path $}#{$ member.anchor | urlencode $}">{$ renderMemberSyntax(member, 1) $}</a>{% endif %}{% endfor -%}
  {% for member in doc.methods %}{% if not member.internal %}
  <a class="code-anchor" href="{$ doc.path $}#{$ member.anchor | urlencode $}">{$ renderMemberSyntax(member, 1) $}</a>{% endif %}{% endfor -%}

  {%- for ancestor in doc.extendsClauses %}{% if ancestor.doc %}

  // inherited from <a class="code-anchor" href="{$ ancestor.doc.path $}">{$ ancestor.doc.id $}</a>{$ renderMembers(ancestor.doc) $}{% endif %}{% endfor -%}
{%- endmacro -%}

{%- macro renderMemberSyntax(member, truncateLines) -%}
    {%- if member.accessibility !== 'public' %}{$ member.accessibility $} {% endif -%}
    {%- if member.isAbstract %}abstract {% endif -%}
    {%- if member.isStatic %}static {% endif -%}
    {%- if (member.isGetAccessor or member.isReadonly) and not member.isSetAccessor %}get {% endif -%}
    {%- if member.isSetAccessor and not member.isGetAccessor %}set {% endif -%}
    {$ member.name $}{$ member.typeParameters | escape $}{% if not member.isGetAccessor %}{$ params.paramList(member.parameters, truncateLines) | trim $}{% endif %}
    {%- if member.isOptional  %}?{% endif -%}
    {$ params.returnType(member.type) | trim | truncateCode(truncateLines) $}
{%- endmacro -%}

{%- macro renderOverloadInfo(overload, cssClass, method) -%}

{% if overload.shortDescription and (overload.shortDescription != method.shortDescription) %}
<div class="short-description">
  {$ overload.shortDescription | marked $}
</div>{% endif %}

<code-example language="ts" hideCopy="true" linenums="false" class="no-box api-heading">{$ renderMemberSyntax(overload) $}</code-example>

<h4 class="no-anchor">Parameters</h4>
{$ params.renderParameters(overload.parameterDocs, cssClass + '-parameters', cssClass + '-parameter') $}

{% if overload.type or overload.returns.type %}
<h4 class="no-anchor">Returns</h4>
{% marked %}`{$ (overload.type or overload.returns.type) $}`{% if overload.returns %}: {$ overload.returns.description $}{% endif %}{% endmarked %}
{% endif %}


{% if overload.throws.length %}
<h4 class="no-anchor">Throws</h4>
{% for error in overload.throws %}
{% marked %}`{$ (error.typeList or 'Error') $}` {$ error.description $}{% endmarked %}
{% endfor %}
{% endif %}

{% if overload.description and (overload.description != method.description) -%}
<div class="description">
  {$ overload.description | marked $}
</div>
{%- endif %}
{%- endmacro -%}

{%- macro renderMethodDetail(versionInfo, method, cssClass) -%}
<a id="{$ method.anchor $}"></a>
<table class="is-full-width method-table {$ cssClass $}">
  {% if method.name !== 'constructor' %}<thead><tr><th>
    <div class="with-github-links">
      <h3>
        {% if method.isCallMember %}<i>call signature</i>
        {% elseif method.isNewMember %}<i>construct signature</i>
        {% else %}{$ method.name $}()
        {% endif %}
      </h3>
      {$ github.githubLinks(method, versionInfo) $}
    </div>
  </th></tr></thead>{% endif %}
  <tbody>
    {% if method.shortDescription %}<tr>
      <td class="short-description">
        {$ method.shortDescription | marked $}
      </td>
    </tr>{% endif %}
  {% if method.overloads.length == 0 %}
    <tr>
      <td>
        {$ renderOverloadInfo(method, cssClass + '-overload', method) $}
      </td>
    </tr>
  {% elseif method.overloads.length < 3 -%}
    {% for overload in method.overloads -%}
    <tr>
      <td>
        {$ renderOverloadInfo(overload, cssClass + '-overload', method) $}
      </td>
    </tr>
    {% endfor -%}
  {% else -%}
    <tr>
      <td>
        <details class="overloads">
          <summary><h4 class="no-anchor">{$ method.overloads.length $} overloads...</h4></summary>
          <div class="detail-contents">
              {% for overload in method.overloads %}
                {$ renderOverloadInfo(overload, cssClass + '-overload', method) $}
                {% if not loop.last %}<hr class="hr-margin fullwidth">{% endif %}
              {% endfor %}
          </div>
        </details>
      </td>
    </tr>
  {% endif %}

  {% if method.description -%}
    <tr>
      <td class="description">
        {$ method.description | marked({ h3: 'h4' }) $}
      </td>
    </tr>
  {%- endif %}

  {% if method.usageNotes -%}
    <tr>
      <td class="usage-notes">
        {$ method.usageNotes | marked({ h3: 'h4' }) $}
      </td>
    </tr>
  {%- endif %}
    </tbody>
</table>
{% endmacro -%}

{%- macro renderMethodDetails(versionInfo, methods, containerClass, itemClass, headingText) -%}
{% set nonInternalMethods = methods | filterByPropertyValue('internal', undefined) %}
{% if nonInternalMethods.length %}
<section class="{$ containerClass $}">
  <h2>{$ headingText $}</h2>
  {% for member in nonInternalMethods %}
    {$ renderMethodDetail(versionInfo, member, itemClass) $}
  {% endfor %}
</section>
{% endif %}
{%- endmacro -%}


{%- macro renderProperties(properties, containerClass, propertyClass, headingText, headings) -%}
{% set nonInternalProperties = properties | filterByPropertyValue('internal', undefined) %}
{% set hasTypes = properties | hasValues('type') %}
{% if nonInternalProperties.length -%}
<section class="{$ containerClass $}">
  <h2>{$ headingText $}</h2>
  <table class="is-full-width list-table property-table">
    <thead>
      <tr>
        <th>{$ headings[0] or 'Property' $}</th>
        {% if hasTypes %}<th>{$ headings[1] or 'Type' $}</th>{% endif %}
        <th>{$ headings[2] or 'Description' $}</th>
      </tr>
    </thead>
    <tbody>
    {% for property in nonInternalProperties %}
      <tr class="{$ propertyClass $}">
        <td><a id="{$ property.anchor $}"></a>{$ property.name $}</td>
        {% if hasTypes %}<td><label class="property-type-label"><code>{$ property.type | escape $}</code></label></td>{% endif %}
        <td>
          {%- if (property.isGetAccessor or property.isReadonly) and not property.isSetAccessor %}<span class='read-only-property'>Read-only.</span>{% endif %}
          {% if property.shortDescription %}{$ property.shortDescription | marked $}{% endif %}
          {$ (property.description or property.constructorParamDoc.description) | marked $}
          {% if property.constructorParamDoc %} <span class='from-constructor'>Declared in constructor.</span>{% endif %}
        </td>
    </tr>
    {% endfor %}
    </tbody>
  </table>
</section>
{%- endif -%}
{%- endmacro -%}
