// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

{% if not for_bindings_internals and
   (interfaces|length > 0 or structs|length > 0 or unions|length > 0
    or enums|length > 0) -%}
import {mojo} from '{{bindings_library_path}}';
{%- endif %}
{%- if webui_factory %}
{%-   if webui_factory.has_callback_router %}
import {MojoBrowserProxyFactory} from '{{mojo_browser_proxy_import}}';
import type {MojoBrowserProxy} from '{{mojo_browser_proxy_import}}';
{%-   else %}
import {MojoHandlerProxyFactory} from '{{mojo_browser_proxy_import}}';
import type {MojoHandlerProxy} from '{{mojo_browser_proxy_import}}';
{%-   endif %}
{%- endif %}

{% for path, kinds in js_module_imports.items()|sort -%}
import {
{%- for kind in kinds|sort %}
{%-   for item in kind|imports_for_kind %}
  {{item.name}} as {{item.alias}}
{%-     if not loop.last -%},{% endif -%}
{%-   endfor %}
{%- if not loop.last -%},{% endif -%}
{%- endfor %}
} from '{{path}}';
{% endfor -%}

{%- if typemapped_structs|length > 0 %}
{%-  for struct in typemapped_structs.keys() %}
import {
  {{struct.name}}DataView,
  {{struct.name}}TypeMapper,
} from './{{converters_filename}}';
{%-  endfor %}
{%- endif %}

{% for path, types in typemap_imports.items()|sort -%}
import type {
{%- for type in types|sort %}
  {{type}}
{%- if not loop.last -%},{% endif -%}
{%- endfor %}
} from '{{path}}';
{% endfor %}

{% for typemap in typemapped_structs.values() -%}
import { {{typemap.converter}} } from './{{typemap|converter_import}}';
{% endfor -%}

{#--- Constants #}
{%- for constant in module.constants %}
export const {{constant.name}}: {{constant.kind|ts_type_maybe_nullable}} = {{constant|constant_value}};

{% endfor -%}

{#--- Enums #}
{%- from "enum_definition.tmpl" import enum_def with context %}
{%- for enum in enums %}
{{enum_def(enum)}}
{%  endfor -%}

{#--- Interfaces #}
{%- for interface in interfaces %}
{%   include "interface_definition.tmpl" %}
{%- endfor %}

{#--- Struct and Union forward declarations #}
{%  for struct in structs %}
export const {{struct.name}}Spec: { $: mojo.internal.MojomType } =
    { $: {} as unknown as mojo.internal.MojomType };
{% endfor %}
{%- for union in unions %}
export const {{union.name}}Spec: { $: mojo.internal.MojomType } =
    { $: {} as unknown as mojo.internal.MojomType };
{% endfor %}

{#--- Struct definitions #}
{%  for struct in structs %}
{%-   include "struct_definition.tmpl" %}
{% endfor -%}

{#--- Union definitions #}
{%  for union in unions %}
{%-   include "union_definition.tmpl" %}
{% endfor %}

{%- if webui_factory %}
{#--- WebUI Browser Proxy definition #}
{%- include "webui_browser_proxy_definition.tmpl" %}
{%- endif %}
