{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier }}{% endif %}{% else %}{% if property.type == 'object' and property.additionalProperties %}Map<String, dynamic>{% else %}{{property | typeName}}{% endif %}{% endif %}{% endmacro %}
part of '../../models.dart';

/// {{ definition.description }}
class {{ definition.name | caseUcfirst | overrideIdentifier }} implements Model {
{% for property in definition.properties %}
    /// {{ property.description }}
    final {% if not property.required %}{{_self.sub_schema(property)}}? {{ property.name | escapeKeyword }}{% else %}{{_self.sub_schema(property)}} {{ property.name | escapeKeyword }}{% endif %};
{% endfor %}
{% if definition.additionalProperties %}
    final Map<String, dynamic> data;
{% endif %}

    {{ definition.name | caseUcfirst | overrideIdentifier}}({% if definition.properties | length or definition.additionalProperties %}{{ '{' }}{% endif %}

{% for property in definition.properties %}
        {% if property.required %}required {% endif %}this.{{ property.name | escapeKeyword }},
{% endfor %}
{% if definition.additionalProperties %}
        required this.data,
{% endif %}
    {% if definition.properties | length or definition.additionalProperties %}{{ '}' }}{% endif %});

    factory {{ definition.name | caseUcfirst | overrideIdentifier}}.fromMap(Map<String, dynamic> map) {
        return {{ definition.name | caseUcfirst | overrideIdentifier }}(
{% for property in definition.properties %}
            {{ property.name | escapeKeyword }}:{{' '}}
            {%- if property.sub_schema -%}
                {%- if property.type == 'array' -%}
                    List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>.from(map['{{property.name | escapeDollarSign }}'].map((p) => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.fromMap(p)))
                {%- else -%}
                    {{property.sub_schema | caseUcfirst | overrideIdentifier}}.fromMap(map['{{property.name | escapeDollarSign }}'])
                {%- endif -%}
            {%- else -%}
                map['{{property.name | escapeDollarSign }}']
                {%- if property.type == "number" -%}
                    {%- if not property.required %}?{% endif %}.toDouble()
                {%- endif -%}
                {%- if property.type == "string" -%}
                    {%- if not property.required %}?{% endif %}.toString()
                {%- endif -%}
                {%- if property.type == "array" -%}
                    {% if property.required %} ?? []{% endif %}
                {%- endif -%}
            {%- endif -%},
{% endfor %}
{% if definition.additionalProperties %}
            data: map,
{% endif %}
        );
    }

    Map<String, dynamic> toMap() {
        return {
{% for property in definition.properties %}
            "{{ property.name | escapeDollarSign }}": {% if property.sub_schema %}{% if property.type == 'array' %}{{property.name | escapeKeyword}}.map((p) => p.toMap()).toList(){% else %}{{property.name | escapeKeyword}}.toMap(){% endif %}{% else %}{{property.name | escapeKeyword }}{% endif %},
{% endfor %}
{% if definition.additionalProperties %}
            "data": data,
{% endif %}
        };
    }
{% if definition.additionalProperties %}

    T convertTo<T>(T Function(Map) fromJson) => fromJson(data);
{% endif %}
{% for property in definition.properties %}
{% if property.sub_schema %}
{% for def in spec.definitions %}
{% if def.name == property.sub_schema and def.additionalProperties and property.type == 'array' %}

    List<T> convertTo<T>(T Function(Map) fromJson) =>
        {{property.name}}.map((d) => d.convertTo<T>(fromJson)).toList();
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
}
