﻿{%- assign StringToDateOnlyCode = StringToDateCode -%}
{%- if UseJsDate -%}
    {%- assign StringToDateOnlyCode = 'parseDateOnly' -%}
{%-  endif -%}
{% if IsNewableObject -%}
{%   if CheckNewableObject -%}
{{ Variable }} = {{ Value }} ? {{ Type }}.fromJS({{ Value }}{% if HandleReferences -%}, _mappings{% endif %}) : {% if HasDefaultValue %}{{ DefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{%   else -%}
{{ Variable }} = {{ Type }}.fromJS({{ Value }}{% if HandleReferences -%}, _mappings{% endif %});
{%   endif -%}
{% elsif IsArray -%}
if (Array.isArray({{ Value }})) {
    {{ Variable }} = [] as any;
    for (let item of {{ Value }})
{%   if IsArrayItemNewableObject -%}
        {{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}.push({{ ArrayItemType }}.fromJS(item{% if HandleReferences %}, _mappings{% endif %}));
{%   else -%}
{%     if IsArrayItemDate -%}
        {{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}.push({{ StringToDateOnlyCode }}(item));
{%     elsif IsArrayItemDateTime -%}
        {{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}.push({{ StringToDateCode }}(item));
{%     else -%}
        {{ Variable }}{% if RequiresStrictPropertyInitialization %}!{% endif %}.push(item);
{%     endif -%}
{%   endif -%}
}
{% if NullValue != "undefined" %}else {
    {{ Variable }} = <any>{{ NullValue }};
}
{% endif -%}
{% elsif IsDictionary -%}
if ({{ Value }}) {
    {{ Variable }} = {} as any;
    for (let key in {{ Value }}) {
        if ({{ Value }}.hasOwnProperty(key))
{%     if IsDictionaryValueNewableObject -%}
            (<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ DictionaryValueType }}.fromJS({{ Value }}[key]{% if HandleReferences %}, _mappings{% endif %}) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{%     elsif IsDictionaryValueNewableArray -%}
            (<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ Value }}[key].map((i: any) => {{ DictionaryValueArrayItemType }}.fromJS(i{% if HandleReferences %}, _mappings{% endif %})) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{%     elsif IsDictionaryValueDate -%}
            (<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ StringToDateOnlyCode }}({{ Value }}[key].toString()) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{%     elsif IsDictionaryValueDateTime -%}
            (<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] ? {{ StringToDateCode }}({{ Value }}[key].toString()) : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{%     else -%}
{%         if HasDictionaryValueDefaultValue or NullValue != "undefined" -%}
            (<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key] !== undefined ? {{ Value }}[key] : {% if HasDictionaryValueDefaultValue %}{{ DictionaryValueDefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{%         else -%}
            (<any>{{ Variable }}){% if RequiresStrictPropertyInitialization %}!{% endif %}[key] = {{ Value }}[key];
{%         endif -%}
{%     endif -%}
    }
}
{% if NullValue != "undefined" %}else {
    {{ Variable }} = <any>{{ NullValue }};
}
{% endif -%}
{% else -%}
    {% if IsDate -%}
{{ Variable }} = {{ Value }} ? {{ StringToDateOnlyCode }}({{ Value }}.toString()) : {% if HasDefaultValue %}{{ StringToDateOnlyCode }}({{ DefaultValue }}){% else %}<any>{{ NullValue }}{% endif %};
    {% elsif IsDateTime -%}
{{ Variable }} = {{ Value }} ? {{ StringToDateCode }}({{ Value }}.toString()) : {% if HasDefaultValue %}{{ StringToDateCode }}({{ DefaultValue }}){% else %}<any>{{ NullValue }}{% endif %};
    {% else -%}
{%       if HasDefaultValue or NullValue != "undefined" -%}
{{ Variable }} = {{ Value }} !== undefined ? {{ Value }} : {% if HasDefaultValue %}{{ DefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{%       else -%}
{{ Variable }} = {{ Value }};
{%       endif -%}
    {% endif -%}
{% endif -%}
