{{! ---------- metadata ---------- ~}}

FORMAT: 1A
{{#if host}}
HOST: {{host}}
{{/if}}

{{~! ---------- /metadata ---------- }}

{{! ---------- name + overview ---------- ~}}

# {{title}}

{{{description}}}

{{~! ---------- /name + overview ---------- }}

{{! ---------- groups ---------- ~}}

{{#each groupedEndpoints}}

{{#if_ne @key ''}}

# Group {{@key}}

{{/if_ne~}}

{{! ---------- endpoints ---------- ~}}
{{~#each this}}
{{! Only show endpoints that have at least one allowed operation ~}}
{{~#if operations}}
{{! XXX: if we add "name" properties to endpoints, we can use "## <identifier> [URI template]" here }}

{{~#MDHeading level=(APIBPGroupedNesting this 1)}}{{APIBPEndpointName this}} [{{{APIBPURLTemplate this}}}]{{/MDHeading}}

{{#if description~}}
{{{description}}}
{{/if}}

{{! ---------- model ---------- ~}}
{{~!
    XXX: reserved for "one exemplar representation of the resource"
~}}
{{~! ---------- /model ---------- ~}}

{{~! ---------- actions ---------- ~}}

{{~#each operations~}}

{{~!
    XXX: if we add "name" properties to operations, we can use "## <identifier> [HTTP request method]" here
~}}

{{~#MDHeading level=(APIBPGroupedNesting ../this 2)}}{{APIBPOperationName ../this method}} [{{method}}]{{/MDHeading}}

{{#if description~}}
{{{description}}}
{{~/if}}


{{#if requestSchemaElements}}
### Request attributes
{{> schemaElements requestSchemaElements}}
{{/if}}


{{#if responseSchemaElements}}
### Response attributes
{{> schemaElements responseSchemaElements}}
{{/if}}

{{~! ---------- parameters ---------- }}

{{#APIBPURLParameters parameters}}
+ Parameters
{{#each this}}
{{! XXX: good way to add object schemas here? should probably just use description and choose the longer
         form for parameters with object types}}
    + {{name}}: {{#truthy example}}`{{example}}`{{/truthy}} ({{#truthy schema}}{{schema.type}}, {{/truthy}}{{#if required}}required{{else}}optional{{/if}})
      {{~#truthy description}} - {{APIBPSanitizeParameterDescription description}}{{/truthy}}
    {{#truthy schema.enum}}
        {{#truthy schema.default}}
        + Default: {{schema.default}}
        {{/truthy}}
        + Members
        {{#each schema.enum}}
            + `{{this}}`
        {{/each}}
    {{/truthy}}

{{#if_eq schema.type "object"}}

{{#indent spaces=8}}
{{#MDCodeBlock}}
{{{MDRenderBody schema}}}
{{/MDCodeBlock}}
{{~/indent}}
{{~/if_eq}}
{{#if default}}
        + Default: `{{json default}}`
{{/if}}
{{/each}}
{{/APIBPURLParameters}}

{{~! ---------- /parameters ---------- ~}}

{{! ---------- payloads ---------- ~}}

{{#if (or parameters.body.length parameters.header.length)}}
{{! XXX: what if we can consume multiple media types? }}
+ Request ({{@root.consumes.[0]}})
{{#if parameters.header.length}}
{{~! XXX: not really sure what to do with headers values here. throwing in the type and description if they are defined}}
    + Headers
{{#each parameters.header}}
        {{name}}: {{#if schema}}{ {{~schema.type~}} }{{#if description}} - {{/if}}{{/if}}{{#if description}}{{description}}{{/if}}
{{~/each}}
{{~/if}}

{{#if parameters.body.0.schema}}

    + Body
{{#indent spaces=8}}
{{#MDCodeBlock}}
{{~#if parameters.body.0.example}}{{{MDRenderSchema parameters.body.0.example}}}{{else}}{{{MDRenderBody parameters.body.0.schema}}}{{/if~}}
{{/MDCodeBlock}}
{{/indent}}
    + Schema
{{#indent spaces=8}}
{{#MDCodeBlock}}
{{{MDRenderSchema parameters.body.0.schema}}}
{{/MDCodeBlock}}
{{/indent~}}

{{/if~}}
{{/if}}

{{#ifnotempty responses}}
{{#each responses}}
+ Response {{statusCode}} ({{@root.consumes.[0]}})
{{#ifnotempty headers}}
    + Headers
{{#each headers}}
        {{name}}: {{#if schema}}{ {{~schema.type~}} }{{#if description}} - {{/if}}{{/if}}{{#if description}}{{description}}{{/if}}
{{/each~}}
{{~/ifnotempty}}
    + Body
{{#indent spaces=8}}
{{#MDCodeBlock}}
{{~#if example}}{{MDRenderSchema example}}{{else}}{{MDRenderBody schema}}{{/if~}}
{{/MDCodeBlock}}
{{/indent}}
    + Schema
{{#indent spaces=8}}
{{#MDCodeBlock}}
{{{MDRenderSchema schema}}}
{{/MDCodeBlock~}}
{{/indent~}}
{{/each}}
{{/ifnotempty~}}

{{~! ---------- /payloads ---------- ~}}

{{~/each~}}

{{~! ---------- /actions ---------- ~}}

{{/if}}
{{~/each~}}

{{~! ---------- /endpoints ---------- }}

{{/each}}

{{~! ---------- /groups ---------- }}

