
{{~for enum in __enums~}}
{{namespace_with_grace_begin enum.namespace}} 
{{~if enum.comment != '' ~}}
/**
 * {{enum.comment | html.escape}}
 */
{{~end~}}
export enum {{enum.name}} {
    {{~for item in enum.items ~}}
{{~if item.comment != '' ~}}
    /**
     * {{escape_comment item.comment}}
     */
{{~end~}}
    {{item.name}} = {{item.value}},
    {{~end~}}
}

{{namespace_with_grace_end enum.namespace}} 
{{~end~}}


{{~
func get_ref_name 
    ret (format_property_name __code_style $0.name) + '_ref'
end

func generate_resolve_field_ref
    field = $0
    fieldName = format_property_name __code_style field.name
    refTable = get_ref_table field
    if can_generate_ref field
        tableName = refTable.name
        if field.is_nullable
            ret 'this.'+(get_ref_name field) + ' = this.' + fieldName + '!= null ? tables.' + tableName + '.get(this.' + fieldName + ') : null'
        else
            ret 'this.'+(get_ref_name field) + ' = tables.' + tableName + '.get(this.' + fieldName + ')'
        end
    else
        if (is_field_bean_need_resolve_ref field)
            ret 'this.'+fieldName + '?.resolve(tables);'
        else if (is_field_array_like_need_resolve_ref field)
            ret 'for (let _e of ' + fieldName + ') { _e?.resolve(tables); }'
        else if (is_field_map_need_resolve_ref field)
            ret 'foreach (var [_, _e] of ' + fieldName ') { _e?.resolve(tables); }'
        else
            ret ''
        end
    end
end
~}}

{{~for bean in __beans~}}

{{namespace_with_grace_begin bean.namespace}}
{{~if bean.comment != '' ~}}
/**
 * {{escape_comment bean.comment}}
 */
{{~end~}}
export {{if bean.is_abstract_type}}abstract {{end}}class {{bean.name}}{{if bean.parent_def_type}} extends {{bean.parent_def_type.full_name}}{{end}} {
{{~if bean.is_abstract_type~}}
    static constructorFrom(_json_: any): {{bean.name}}{
        switch (_json_["$type"]) {
        {{~ for child in bean.hierarchy_not_abstract_children~}}
            case '{{impl_data_type child bean}}': return new {{child.full_name}}(_json_)
        {{~end~}}
            default: throw new Error()
        }
    }
{{~end~}}

    constructor(_json_: any) {
        {{~if bean.parent_def_type~}}
        super(_json_)
        {{~end~}}
        {{~ for field in bean.export_fields ~}}
        {{~if !field.ctype.is_nullable~}}
        if (_json_.{{field.name}} === undefined) { throw new Error() }
        {{~end~}}
        {{deserialize ('this.' + format_field_name __code_style field.name) ( '_json_.' + field.name) field.ctype}}
        {{~end~}}
    }

    {{~ for field in bean.export_fields ~}}
{{~if field.comment != '' ~}}
    /**
     * {{escape_comment field.comment}}
     */
{{~end~}}
    readonly {{format_field_name __code_style field.name}}: {{declaring_type_name field.ctype}}
    {{~if can_generate_ref field~}}
    {{get_ref_name field}}: {{declaring_type_name (get_ref_type field)}} | undefined
    {{~end~}}
    {{~end~}}

    resolve(tables:{{__name}})
    {
        {{~if bean.parent_def_type~}}
        super.resolve(tables)
        {{~end~}}
        {{~ for field in bean.export_fields ~}}
        {{~if can_generate_ref field~}}
        {{generate_resolve_field_ref field}}
        {{~end~}}
        {{~end~}}
    }
}

{{namespace_with_grace_end bean.namespace}}

{{~end~}}

{{~for table in __tables
    key_type = table.key_ttype
    value_type = table.value_ttype
~}}

{{namespace_with_grace_begin table.namespace}}
{{~if table.comment != '' ~}}
/**
 * {{escape_comment table.comment}}
 */
{{~end~}}
export class {{table.name}}{
    {{~if table.is_map_table ~}}
    private _dataMap: Map<{{declaring_type_name key_type}}, {{declaring_type_name value_type}}>
    private _dataList: {{declaring_type_name value_type}}[]
    constructor(_json_: any) {
        this._dataMap = new Map<{{declaring_type_name key_type}}, {{declaring_type_name value_type}}>()
        this._dataList = []
        for(var _json2_ of _json_) {
            let _v: {{declaring_type_name value_type}}
            {{deserialize '_v' '_json2_' value_type}}
            this._dataList.push(_v)
            this._dataMap.set(_v.{{format_field_name __code_style table.index_field.name}}, _v)
        }
    }

    getDataMap(): Map<{{declaring_type_name key_type}}, {{declaring_type_name value_type}}> { return this._dataMap; }
    getDataList(): {{declaring_type_name value_type}}[] { return this._dataList; }

    get(key: {{declaring_type_name key_type}}): {{declaring_type_name value_type}} | undefined { return this._dataMap.get(key); }

    resolve(tables:{{__name}})
    {
        for(let  data of this._dataList)
        {
            data.resolve(tables)
        }
    }
    {{~else if table.is_list_table ~}}
    private _dataList: {{declaring_type_name value_type}}[]
    
    constructor(_json_: any) {
        this._dataList = []
        for(var _json2_ of _json_) {
            let _v: {{declaring_type_name value_type}}
            {{deserialize '_v' '_json2_' value_type}}
            this._dataList.push(_v)
        }
    }

    getDataList(): {{declaring_type_name value_type}}[] { return this._dataList }

    get(index: number): {{declaring_type_name value_type}} | undefined { return this._dataList[index] }
    
    resolve(tables:{{__name}})
    {
        for(let  data of this._dataList)
        {
            data.resolve(tables)
        }
    }
    {{~else~}}

    private _data: {{declaring_type_name value_type}}
    constructor(_json_: any) {
        if (_json_.length != 1) throw new Error('table mode=one, but size != 1')
        {{deserialize 'this._data' '_json_[0]' value_type}}
    }

    getData(): {{declaring_type_name value_type}} { return this._data; }

    {{~ for field in value_type.def_bean.hierarchy_export_fields ~}}
{{~if field.comment != '' ~}}
    /**
     * {{escape_comment field.comment}}
     */
{{~end~}}
    get  {{format_field_name __code_style field.name}}(): {{declaring_type_name field.ctype}} { return this._data.{{format_field_name __code_style field.name}}; }
    {{~if can_generate_ref field~}}
    get  {{format_field_name __code_style field.name}}_ref(): {{declaring_type_name (get_ref_type field)}} | undefined { return this._data.{{format_field_name __code_style field.name}}_ref; }
    {{~end~}}
    {{~end~}}

    resolve(tables:{{__name}})
    {
        this._data.resolve(tables)
    }
    {{end}}
}
{{namespace_with_grace_end table.namespace}}

{{~end~}}


type JsonLoader = (file: string) => any

export class {{__name}} {
    {{~ for table in __tables ~}}
    private _{{table.name}}: {{table.full_name}}
{{~if table.comment != '' ~}}
    /**
     * {{escape_comment table.comment}}
     */
{{~end~}}
    get {{table.name}}(): {{table.full_name}}  { return this._{{table.name}};}
    {{~end~}}

    constructor(loader: JsonLoader) {
        {{~for table in __tables ~}}
        this._{{table.name}} = new {{table.full_name}}(loader('{{table.output_data_file}}'))
        {{~end~}}

        {{~for table in __tables ~}}
        this._{{table.name}}.resolve(this)
        {{~end~}}
    }
}