"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__, {
    AtomicCommerceInterface: ()=>AtomicCommerceInterface
});
const commerce_namespaceObject = require("@coveo/headless/commerce");
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 commerce_layout_cjs_namespaceObject = require("../atomic-commerce-layout/commerce-layout.cjs");
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 mobile_breakpoint_controller_cjs_namespaceObject = require("../../common/layout/mobile-breakpoint-controller.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 local_storage_utils_cjs_namespaceObject = require("../../../utils/local-storage-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 FirstRequestExecutedFlag = 'firstRequestExecuted';
class AtomicCommerceInterface extends (0, children_update_complete_mixin_cjs_namespaceObject.ChildrenUpdateCompleteMixin)(external_lit_namespaceObject.LitElement) {
    connectedCallback() {
        super.connectedCallback();
        this.store.setLoadingFlag(FirstRequestExecutedFlag);
        this.addEventListener('atomic/initializeComponent', this.handleInitialization);
        this.addEventListener('atomic/scrollToTop', this.scrollToTop);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        if ('function' == typeof this.unsubscribeUrlManager) this.unsubscribeUrlManager();
        if ('function' == typeof this.unsubscribeSummary) this.unsubscribeSummary();
        window.removeEventListener('hashchange', this.onHashChange);
        this.removeEventListener('atomic/initializeComponent', this.handleInitialization);
        this.removeEventListener('atomic/scrollToTop', this.scrollToTop);
    }
    initialize(options) {
        return this.internalInitialization(()=>this.initEngine(options));
    }
    initializeWithEngine(engine) {
        engine.dispatch((0, commerce_namespaceObject.loadConfigurationActions)(engine).updateAnalyticsConfiguration({
            trackingId: engine.configuration.analytics.trackingId,
            ...(0, analytics_config_cjs_namespaceObject.augmentAnalyticsConfigWithAtomicVersion)()
        }));
        return this.internalInitialization(()=>{
            this.engine = engine;
        });
    }
    async executeFirstRequest() {
        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);
        if ('search' === this.type) {
            const safeStorage = new local_storage_utils_cjs_namespaceObject.SafeStorage();
            const standaloneSearchBoxData = safeStorage.getParsedJSON(local_storage_utils_cjs_namespaceObject.StorageItems.STANDALONE_SEARCH_BOX_DATA, null);
            if (!standaloneSearchBoxData) return void this.searchOrListing.executeFirstSearch();
            safeStorage.removeItem(local_storage_utils_cjs_namespaceObject.StorageItems.STANDALONE_SEARCH_BOX_DATA);
            const { value } = standaloneSearchBoxData;
            this.engine.dispatch((0, commerce_namespaceObject.loadQueryActions)(this.engine).updateQuery({
                query: value
            }));
            this.searchOrListing.executeFirstSearch();
        } else this.searchOrListing.executeFirstRequest();
    }
    updateLocale(language, country, currency) {
        if (!this.interfaceController.engineIsCreated(this.engine) || !this.context) return;
        language && this.interfaceController.onLanguageChange(language);
        if (this.isNewLocale(language, country, currency)) {
            const { setContext } = (0, commerce_namespaceObject.loadContextActions)(this.engine);
            this.engine.dispatch(setContext({
                ...this.context.state,
                ...language && {
                    language
                },
                ...country && {
                    country
                },
                ...currency && {
                    currency
                }
            }));
        }
    }
    toggleAnalytics() {
        if (!this.interfaceController.engineIsCreated(this.engine)) return;
        this.interfaceController.onAnalyticsChange();
    }
    updateLanguage() {
        if (!this.interfaceController.engineIsCreated(this.engine) || !this.language || !this.context) return;
        this.engine.logger.warn('The `language` property will be removed in the next major version of Atomic (v4). Rather than using this property, set the initial language through the engine configuration when calling `initialize` or `initializeWithEngine`, and update the language as needed using the `updateLocale` method.');
        this.context.setLanguage(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, commerce_namespaceObject.buildCommerceEngine)({
                configuration: {
                    ...options,
                    analytics: analyticsConfig
                },
                loggerOptions: {
                    level: this.logLevel
                }
            });
        } catch (error) {
            this.error = error;
            throw error;
        }
    }
    get fragment() {
        return window.location.hash.slice(1);
    }
    initContext() {
        this.context = (0, commerce_namespaceObject.buildContext)(this.engine);
    }
    initLanguage() {
        if (!this.context || this.language) return;
        this.interfaceController.onLanguageChange(this.context.state.language);
    }
    initRequestStatus() {
        this.searchOrListing = 'product-listing' === this.type ? (0, commerce_namespaceObject.buildProductListing)(this.engine) : (0, commerce_namespaceObject.buildSearch)(this.engine);
    }
    initSummary() {
        this.summary = this.searchOrListing.summary();
        this.unsubscribeSummary = this.summary.subscribe(()=>{
            const { firstRequestExecuted, hasProducts, hasError } = this.summary.state;
            const hasNoProductsAfterInitialQuery = firstRequestExecuted && !hasError && !hasProducts;
            this.classList.toggle(commerce_layout_cjs_namespaceObject.noProductsSelector, hasNoProductsAfterInitialQuery);
            this.classList.toggle(commerce_layout_cjs_namespaceObject.errorSelector, hasError);
            this.classList.toggle(commerce_layout_cjs_namespaceObject.firstSearchExecutedSelector, firstRequestExecuted);
            if (firstRequestExecuted) this.store.unsetLoadingFlag(FirstRequestExecutedFlag);
        });
    }
    initUrlManager() {
        if (this.disableStateReflectionInUrl) return;
        if (!this.reflectStateInUrl) return;
        this.urlManager = this.searchOrListing.urlManager({
            initialState: {
                fragment: this.fragment
            }
        });
        this.unsubscribeUrlManager = this.urlManager.subscribe(()=>this.updateHash());
        window.addEventListener('hashchange', this.onHashChange);
    }
    async internalInitialization(initEngine) {
        await Promise.all([
            this.interfaceController.onInitialization(initEngine),
            this.i18Initialized
        ]);
        this.initContext();
        this.updateLanguage();
        this.bindings = this.getBindings();
        (0, init_queue_cjs_namespaceObject.markParentAsReady)(this);
        this.initRequestStatus();
        this.initSummary();
        this.initLanguage();
        await (0, initialization_common_utils_cjs_namespaceObject.waitForAtomicChildrenToBeDefined)(this);
        await this.getUpdateComplete();
        this.initUrlManager();
        this.initialized = true;
    }
    isNewLocale(language, country, currency) {
        if (!this.context) return false;
        return language && language !== this.context.state.language || country && country !== this.context.state.country || currency && currency !== this.context.state.currency;
    }
    scrollToTop() {
        const scrollContainerElement = document.querySelector(this.scrollContainer);
        if (!scrollContainerElement) return void console.warn(`Could not find the scroll container with the selector "${this.scrollContainer}". This will prevent UX interactions that require a scroll from working correctly. Please review the CSS selector in the scrollContainer option`);
        scrollContainerElement.scrollIntoView({
            behavior: 'smooth'
        });
    }
    updateHash() {
        const newFragment = this.urlManager.state.fragment;
        if (!this.summary.state.firstRequestExecuted) {
            history.replaceState(null, document.title, `#${newFragment}`);
            this.bindings.engine.logger.info(`History replaceState #${newFragment}`);
            return;
        }
        history.pushState(null, document.title, `#${newFragment}`);
        this.bindings.engine.logger.info(`History pushState #${newFragment}`);
    }
    constructor(){
        super(), this.bindings = {}, this.unsubscribeUrlManager = ()=>{}, this.unsubscribeSummary = ()=>{}, this.initialized = false, this.interfaceController = new interface_controller_cjs_namespaceObject.InterfaceController(this, 'CoveoAtomic', commerce_namespaceObject.VERSION), this.type = 'search', this.analytics = true, this.reflectStateInUrl = true, this.disableStateReflectionInUrl = false, this.scrollContainer = 'atomic-commerce-interface', this.languageAssetsPath = './lang', this.iconAssetsPath = './assets', this.handleInitialization = (event)=>{
            this.interfaceController.onComponentInitializing(event);
        }, this.onHashChange = ()=>{
            this.urlManager.synchronize(this.fragment);
        };
        this.store = (0, external_store_cjs_namespaceObject.createCommerceStore)(this.type);
        new mobile_breakpoint_controller_cjs_namespaceObject.MobileBreakpointController(this, this.store);
        const { promise, resolve } = Promise.withResolvers();
        this.i18Initialized = promise;
        this.i18n = external_i18next_default().createInstance(void 0, resolve);
    }
}
AtomicCommerceInterface.styles = [
    (0, external_lit_namespaceObject.css)`
      :host {
        height: inherit;
        width: inherit;
        & > slot {
          height: inherit;
        }
      }
    `
];
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    (0, context_namespaceObject.provide)({
        context: bindings_context_cjs_namespaceObject.bindingsContext
    }),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicCommerceInterface.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceInterface.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicCommerceInterface.prototype, "type", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        converter: boolean_converter_cjs_namespaceObject.booleanConverter,
        reflect: true
    })
], AtomicCommerceInterface.prototype, "analytics", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'log-level',
        reflect: true
    }),
    _ts_metadata("design:type", "u" < typeof LogLevel ? Object : LogLevel)
], AtomicCommerceInterface.prototype, "logLevel", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object,
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof i18n ? Object : i18n)
], AtomicCommerceInterface.prototype, "i18n", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicCommerceInterface.prototype, "language", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object,
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof CommerceEngine ? Object : CommerceEngine)
], AtomicCommerceInterface.prototype, "engine", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        attribute: 'reflect-state-in-url',
        reflect: true,
        converter: boolean_converter_cjs_namespaceObject.booleanConverter
    })
], AtomicCommerceInterface.prototype, "reflectStateInUrl", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        attribute: 'disable-state-reflection-in-url',
        reflect: true
    })
], AtomicCommerceInterface.prototype, "disableStateReflectionInUrl", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'scroll-container',
        reflect: true
    })
], AtomicCommerceInterface.prototype, "scrollContainer", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'language-assets-path',
        reflect: true
    })
], AtomicCommerceInterface.prototype, "languageAssetsPath", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        attribute: 'icon-assets-path',
        reflect: true
    })
], AtomicCommerceInterface.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)
], AtomicCommerceInterface.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)
], AtomicCommerceInterface.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)
], AtomicCommerceInterface.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)
], AtomicCommerceInterface.prototype, "render", null);
AtomicCommerceInterface = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-commerce-interface'),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicCommerceInterface);
exports.AtomicCommerceInterface = __webpack_exports__.AtomicCommerceInterface;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicCommerceInterface"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
