"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__, {
    AtomicRecsInterface: ()=>AtomicRecsInterface
});
const headless_namespaceObject = require("@coveo/headless");
const recommendation_namespaceObject = require("@coveo/headless/recommendation");
const context_namespaceObject = require("@lit/context");
const external_i18next_namespaceObject = require("i18next");
var external_i18next_default = /*#__PURE__*/ __webpack_require__.n(external_i18next_namespaceObject);
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const bindings_context_cjs_namespaceObject = require("../../common/context/bindings-context.cjs");
const analytics_config_cjs_namespaceObject = require("../../common/interface/analytics-config.cjs");
const interface_controller_cjs_namespaceObject = require("../../common/interface/interface-controller.cjs");
const array_converter_cjs_namespaceObject = require("../../../converters/array-converter.cjs");
const boolean_converter_cjs_namespaceObject = require("../../../converters/boolean-converter.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const watch_cjs_namespaceObject = require("../../../decorators/watch.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const children_update_complete_mixin_cjs_namespaceObject = require("../../../mixins/children-update-complete-mixin.cjs");
const init_queue_cjs_namespaceObject = require("../../../utils/init-queue.cjs");
const initialization_common_utils_cjs_namespaceObject = require("../../../utils/initialization-common-utils.cjs");
const external_analytics_config_cjs_namespaceObject = require("./analytics-config.cjs");
const external_store_cjs_namespaceObject = require("./store.cjs");
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
    else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
const FirstRecommendationExecutedFlag = 'firstRecommendationExecuted';
const engineConfigurationConflictError = (configurationName)=>`A ${configurationName} is configured on the recommendation interface element, but the recommendation interface was initialized with an engine. You should only configure the ${configurationName} in the target engine.`;
class AtomicRecsInterface extends (0, children_update_complete_mixin_cjs_namespaceObject.ChildrenUpdateCompleteMixin)(external_lit_namespaceObject.LitElement) {
    connectedCallback() {
        super.connectedCallback();
        this.store.setLoadingFlag(FirstRecommendationExecutedFlag);
        this.initFieldsToInclude();
        this.addEventListener('atomic/initializeComponent', this.handleInitialization);
    }
    willUpdate(changedProperties) {
        super.willUpdate(changedProperties);
        if (changedProperties.has('fieldsToInclude')) this.updateFieldsToInclude();
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.removeEventListener('atomic/initializeComponent', this.handleInitialization);
    }
    initialize(options) {
        return this.internalInitialization(()=>this.initEngine(options));
    }
    initializeWithRecommendationEngine(engine) {
        if (this.pipeline && this.pipeline !== engine.state.pipeline) console.warn(engineConfigurationConflictError('query pipeline'));
        if (this.searchHub && this.searchHub !== engine.state.searchHub) console.warn(engineConfigurationConflictError('search hub'));
        engine.dispatch((0, headless_namespaceObject.loadConfigurationActions)(engine).updateAnalyticsConfiguration({
            ...(0, analytics_config_cjs_namespaceObject.augmentAnalyticsConfigWithAtomicVersion)()
        }));
        return this.internalInitialization(()=>{
            this.engine = engine;
        });
    }
    async getRecommendations() {
        if (!this.interfaceController.engineIsCreated(this.engine)) return;
        if (!this.initialized) return void console.error('You have to wait until the "initialize" promise is fulfilled before executing a request.', this);
        this.engine.dispatch((0, recommendation_namespaceObject.loadRecommendationActions)(this.engine).getRecommendations());
    }
    registerFieldsToInclude() {
        this.engine?.dispatch((0, recommendation_namespaceObject.loadFieldActions)(this.engine).registerFieldsToInclude(this.store.state.fieldsToInclude));
    }
    toggleAnalytics() {
        if (!this.interfaceController.engineIsCreated(this.engine)) return;
        this.interfaceController.onAnalyticsChange();
    }
    updateLanguage() {
        if (!this.interfaceController.engineIsCreated(this.engine) || !this.language) return;
        const { updateSearchConfiguration } = (0, recommendation_namespaceObject.loadSearchConfigurationActions)(this.engine);
        this.engine.dispatch(updateSearchConfiguration({
            locale: this.language
        }));
        return this.interfaceController.onLanguageChange();
    }
    updateIconAssetsPath() {
        this.store.state.iconAssetsPath = this.iconAssetsPath;
    }
    render() {
        return (0, external_lit_namespaceObject.html)`<slot></slot>`;
    }
    getBindings() {
        return {
            engine: this.engine,
            i18n: this.i18n,
            store: this.store,
            interfaceElement: this
        };
    }
    initEngine(options) {
        const analyticsConfig = (0, external_analytics_config_cjs_namespaceObject.getAnalyticsConfig)(options, this.analytics);
        try {
            this.engine = (0, recommendation_namespaceObject.buildRecommendationEngine)({
                configuration: {
                    pipeline: this.pipeline,
                    searchHub: this.searchHub ?? 'default',
                    locale: this.language,
                    timezone: this.timezone,
                    analytics: analyticsConfig,
                    ...options
                },
                loggerOptions: {
                    level: this.logLevel
                }
            });
        } catch (error) {
            this.error = error;
            throw error;
        }
    }
    initFieldsToInclude() {
        const fields = recommendation_namespaceObject.EcommerceDefaultFieldsToInclude.concat(this.fieldsToInclude);
        this.store.addFieldsToInclude(fields);
    }
    updateFieldsToInclude() {
        this.store.state.fieldsToInclude = [];
        this.initFieldsToInclude();
        this.registerFieldsToInclude();
    }
    async internalInitialization(initEngine) {
        await Promise.all([
            this.interfaceController.onInitialization(initEngine),
            this.i18Initialized
        ]);
        this.updateLanguage();
        this.bindings = this.getBindings();
        (0, init_queue_cjs_namespaceObject.markParentAsReady)(this);
        this.pipeline = this.engine.state.pipeline;
        this.searchHub = this.engine.state.searchHub;
        this.store.unsetLoadingFlag(FirstRecommendationExecutedFlag);
        await (0, initialization_common_utils_cjs_namespaceObject.waitForAtomicChildrenToBeDefined)(this);
        await this.getUpdateComplete();
        this.initialized = true;
    }
    constructor(){
        super(), this.bindings = {}, this.initialized = false, this.interfaceController = new interface_controller_cjs_namespaceObject.InterfaceController(this, 'CoveoAtomicRecs', headless_namespaceObject.VERSION), this.fieldsToInclude = [], this.analytics = true, this.language = 'en', this.languageAssetsPath = './lang', this.iconAssetsPath = './assets', this.handleInitialization = (event)=>{
            this.interfaceController.onComponentInitializing(event);
        };
        this.store = (0, external_store_cjs_namespaceObject.createRecsStore)();
        const { promise, resolve } = Promise.withResolvers();
        this.i18Initialized = promise;
        this.i18n = external_i18next_default().createInstance(void 0, resolve);
    }
}
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    (0, context_namespaceObject.provide)({
        context: bindings_context_cjs_namespaceObject.bindingsContext
    }),
    _ts_metadata("design:type", "u" < typeof RecsBindings ? Object : RecsBindings)
], AtomicRecsInterface.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicRecsInterface.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Array,
        attribute: 'fields-to-include',
        converter: array_converter_cjs_namespaceObject.arrayConverter
    }),
    _ts_metadata("design:type", Array)
], AtomicRecsInterface.prototype, "fieldsToInclude", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicRecsInterface.prototype, "pipeline", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'search-hub',
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicRecsInterface.prototype, "searchHub", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        converter: boolean_converter_cjs_namespaceObject.booleanConverter,
        reflect: true
    })
], AtomicRecsInterface.prototype, "analytics", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicRecsInterface.prototype, "timezone", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'log-level',
        reflect: true
    }),
    _ts_metadata("design:type", "u" < typeof RecsLogLevel ? Object : RecsLogLevel)
], AtomicRecsInterface.prototype, "logLevel", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object,
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof i18n ? Object : i18n)
], AtomicRecsInterface.prototype, "i18n", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    })
], AtomicRecsInterface.prototype, "language", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object,
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof RecommendationEngine ? Object : RecommendationEngine)
], AtomicRecsInterface.prototype, "engine", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'language-assets-path',
        reflect: true
    })
], AtomicRecsInterface.prototype, "languageAssetsPath", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'icon-assets-path',
        reflect: true
    })
], AtomicRecsInterface.prototype, "iconAssetsPath", void 0);
_ts_decorate([
    (0, watch_cjs_namespaceObject.watch)('analytics'),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicRecsInterface.prototype, "toggleAnalytics", null);
_ts_decorate([
    (0, watch_cjs_namespaceObject.watch)('language'),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicRecsInterface.prototype, "updateLanguage", null);
_ts_decorate([
    (0, watch_cjs_namespaceObject.watch)('iconAssetsPath'),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicRecsInterface.prototype, "updateIconAssetsPath", null);
_ts_decorate([
    (0, error_guard_cjs_namespaceObject.errorGuard)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicRecsInterface.prototype, "render", null);
AtomicRecsInterface = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-recs-interface'),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicRecsInterface);
exports.AtomicRecsInterface = __webpack_exports__.AtomicRecsInterface;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicRecsInterface"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
