/**
 * @type {Record<string, string[]>}
 */
globalThis['__versoes'] = (
    await import(
        './versoes.json?v=' + new Date().getTime(),
        // @ts-ignore
        {
            with: {
                type: 'json',
            },
        }
    )
).default;
let versaoJS = localStorage.getItem('__force_versao_govbrplataformajs');

let possuiVersao = false;
/**
 * @type {HTMLSelectElement}
 */
const select = document.querySelector('#versao_plataforma');
if (select) {
    __versoes['GOVBRPlataformaJS'].forEach((versao) => {
        const opt = document.createElement('option');
        opt.value = versao;
        opt.innerText = versao;
        select.append(opt);
        if (versao == versaoJS) {
            possuiVersao = true;
        }
    });
    select.addEventListener('change', () => {
        localStorage.setItem('__force_versao_govbrplataformajs', select.value);
        window.location.reload();
    });
}
if (!possuiVersao) {
    if (versaoJS) {
        localStorage.removeItem('__force_versao_govbrplataformajs');
    }
    versaoJS = __versoes['GOVBRPlataformaJS'][0];
}
if (select) {
    select.value = versaoJS;
}

await import('./plataforma/' + versaoJS + '/GOVBRPlataformaJS.mjs');

// @ts-ignore
if (!Array.isArray(globalThis['__GOVBRPlataformaJSInit'])) {
    /**
     * @type {[() => Promise<void>]}
     */
    globalThis['__GOVBRPlataformaJSInit'] = [globalThis['__GOVBRPlataformaJSInit'] || (async () => {})];
}
globalThis['__GOVBRPlataformaJSInit'].reduce(
    (prev, fn) => prev.then(async () => await fn()),
    // Primeiro monta a linha de títulos.
    Promise.resolve()
);
