/*
  Do not manually edit this file, it was auto-generated by djelm
  https://github.com/Confidenceman02/djelm
*/
{{cookiecutter.lists.imports | join("\n")}}

const singletonKey = Symbol("{{cookiecutter.program_name}}")
const instances = new Map()

const views = {
  djelm{{cookiecutter.view_name}}: async (el: HTMLElement, data: any) => {
    //@ts-ignore
    const { Elm } = await import("../../../src/{{cookiecutter.base_path}}{{cookiecutter.program_name}}.elm")
    const settings = JSON.parse(el.dataset.settings || "{}")

    const app = Elm.{{cookiecutter.base_name}}{{cookiecutter.program_name}}.init({
        node: el,
        flags: data,
    });
    manageInstance(settings, app)
    {% if cookiecutter.lists.extras %}
    {{cookiecutter.lists.extras | join("\n")}}
    {% endif %}
    return {
        // Called whenever the value of the `djelm` attribute changes
        update: (newData, oldData) => {},
        // Called when the element (or its djelm attribute) is removed from the DOM
        destroy: () => {},
    };
  }
}

function manageInstance(settings: {singleton?: boolean, name?: string}, app: object): void {
  if (settings.singleton) {
    const instance = instances.get(settings.name || singletonKey)
    if (instance) instance.die()
  }
  instances.set(settings.name || singletonKey, app)
}

defo({ views, prefix: "{{cookiecutter.scope}}" })
