{{/*

  Finds and returns all nested objects, given:

  * `this_object`: a JSON schema object

  Given a schema object, this template finds all nested objects under that
  schema.

  It "cleans" each object by copying only the parts of the objects that
  the renderer needs, and adds the result to an array, `additional_objects`.

  Finally it returns the array of all the objects it found.

  Note that the returned array may contain duplicate objects.

*/}}

{{ $this_object := partial "json-schema/resolve-allof" . }}
{{ $additional_objects := slice }}

{{ if eq $this_object.type "object" }}

    {{/*
      Add the object we were passed into the $additional_objects array
    */}}
    {{ $additional_objects = $additional_objects | append (partial "clean-object" $this_object) }}

    {{/*
      Add any nested objects referenced in this object's `additionalProperties`
    */}}
    {{ if $this_object.additionalProperties }}
        {{ if reflect.IsMap $this_object.additionalProperties }}
            {{ $additional_objects = $additional_objects | append (partial "clean-object" $this_object.additionalProperties) }}

            {{ range $key, $property := $this_object.additionalProperties.properties }}
                {{ $additional_objects = partial "get-additional-objects" (dict "this_object" $property "additional_objects" $additional_objects) }}
            {{ end }}

        {{ end }}
    {{ end }}

    {{/*
      Add any nested objects referenced in this object's `properties`
    */}}
    {{ range $key, $property := $this_object.properties}}
        {{ $additional_objects = partial "get-additional-objects" (dict "this_object" $property "additional_objects" $additional_objects) }}
    {{ end }}

{{ end }}

{{ if eq $this_object.type "array" }}
    {{/*
      Add any nested objects referenced in this object's `items`
    */}}
    {{ if reflect.IsSlice $this_object.items}}
        {{ range $this_object.items }}
            {{ $additional_objects = partial "get-additional-objects" (dict "this_object" . "additional_objects" $additional_objects) }}
        {{ end }}
    {{ else }}
        {{ $additional_objects = partial "get-additional-objects" (dict "this_object" $this_object.items "additional_objects" $additional_objects) }}
    {{ end }}
{{ end }}

{{ return $additional_objects }}


{{/*
  This actually makes the recursive call and adds the returned objects to the array
*/}}
{{ define "partials/get-additional-objects" }}
    {{ $additional_objects := .additional_objects }}

    {{ $this_object := partial "json-schema/resolve-allof" .this_object }}
    {{ $more_objects := partial "json-schema/resolve-additional-types" $this_object }}
    {{/*
      As far as I know we don't have something like Array.concat(), so add them one at a time
    */}}
    {{ range $more_objects}}
        {{ $additional_objects = $additional_objects | append (partial "clean-object" .) }}
    {{ end }}
    {{ return $additional_objects }}
{{ end }}

{{/*
  Only copy the bits of the object that we actually care about.
  This is needed for uniqify to work - otherwise objects that are the same
  but with (for example) different examples will be considered different.
*/}}
{{ define "partials/clean-object" }}
    {{ return (dict "title" .title "properties" .properties "required" .required "enum" .enum) }}
{{ end }}
