import i18next_http_backend from "i18next-http-backend";
import { setCoveoGlobal } from "../../../global/environment.js";
import { loadDayjsLocale } from "../../../utils/dayjs-locales.js";
import { i18nBackendOptions, i18nTranslationNamespace, init18n } from "./i18n.js";
import "../atomic-aria-live/atomic-aria-live.js";
class InterfaceController {
    hostConnected() {
        this.i18nPromise = init18n(this.host);
        this.initAriaLive();
    }
    hostDisconnected() {
        this.removeAriaLive();
    }
    onComponentInitializing(event) {
        event.preventDefault();
        event.stopPropagation();
        if (this.host.engine) return void event.detail(this.host.bindings);
        this.hangingComponentsInitialization.push(event);
    }
    async onInitialization(initEngine) {
        if (this.host.engine) return void this.host.engine.logger.warn(`The ${this.interfaceTagname} component "initialize" has already been called.`);
        this.host.updateIconAssetsPath();
        initEngine();
        if (this.host.registerFieldsToInclude) this.host.registerFieldsToInclude();
        loadDayjsLocale(this.host.language || 'en');
        await this.i18nPromise;
        this.initComponents();
    }
    onAnalyticsChange() {
        const { engine, analytics } = this.host;
        if (!this.engineIsCreated(engine)) return;
        if (!analytics) return void engine.disableAnalytics();
        engine.enableAnalytics();
    }
    onLanguageChange(newLanguage) {
        const language = newLanguage ?? this.host.language;
        const languageWithFallback = language ?? 'en';
        const { i18n } = this.host;
        loadDayjsLocale(languageWithFallback);
        new i18next_http_backend(i18n.services, i18nBackendOptions(this.host)).read(languageWithFallback.split('-')[0], i18nTranslationNamespace, (_, data)=>{
            i18n.addResourceBundle(languageWithFallback.split('-')[0], i18nTranslationNamespace, data, true, false);
            i18n.changeLanguage(language);
        });
    }
    engineIsCreated(engine) {
        if (!engine) {
            console.error(`You have to call "initialize" on the ${this.interfaceTagname} component before modifying the props or calling other public methods.`, this.host);
            return false;
        }
        return true;
    }
    get interfaceTagname() {
        return this.host.tagName.toLowerCase();
    }
    initComponents() {
        this.hangingComponentsInitialization.forEach((event)=>event.detail(this.host.bindings));
    }
    initAriaLive() {
        if (Array.from(this.host.children).some((element)=>'ATOMIC-ARIA-LIVE' === element.tagName)) return;
        const ariaLive = document.createElement('atomic-aria-live');
        this.host.prepend(ariaLive);
    }
    removeAriaLive() {
        const ariaLive = this.host.querySelector('atomic-aria-live');
        if (ariaLive) ariaLive.remove();
    }
    constructor(host, globalVariableName, headlessVersion){
        this.hangingComponentsInitialization = [];
        this.host = host;
        this.host.addController(this);
        setCoveoGlobal(globalVariableName, headlessVersion);
    }
}
export { InterfaceController };
