"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    InterfaceController: ()=>InterfaceController
});
const external_i18next_http_backend_namespaceObject = require("i18next-http-backend");
var external_i18next_http_backend_default = /*#__PURE__*/ __webpack_require__.n(external_i18next_http_backend_namespaceObject);
const environment_cjs_namespaceObject = require("../../../global/environment.cjs");
const dayjs_locales_cjs_namespaceObject = require("../../../utils/dayjs-locales.cjs");
const external_i18n_cjs_namespaceObject = require("./i18n.cjs");
require("../atomic-aria-live/atomic-aria-live.cjs");
class InterfaceController {
    hostConnected() {
        this.i18nPromise = (0, external_i18n_cjs_namespaceObject.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();
        (0, dayjs_locales_cjs_namespaceObject.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;
        (0, dayjs_locales_cjs_namespaceObject.loadDayjsLocale)(languageWithFallback);
        new (external_i18next_http_backend_default())(i18n.services, (0, external_i18n_cjs_namespaceObject.i18nBackendOptions)(this.host)).read(languageWithFallback.split('-')[0], external_i18n_cjs_namespaceObject.i18nTranslationNamespace, (_, data)=>{
            i18n.addResourceBundle(languageWithFallback.split('-')[0], external_i18n_cjs_namespaceObject.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);
        (0, environment_cjs_namespaceObject.setCoveoGlobal)(globalVariableName, headlessVersion);
    }
}
exports.InterfaceController = __webpack_exports__.InterfaceController;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "InterfaceController"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
