"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__, {
    AtomicSearchBoxRecentQueries: ()=>AtomicSearchBoxRecentQueries
});
const headless_namespaceObject = require("@coveo/headless");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const local_storage_utils_cjs_namespaceObject = require("../../../utils/local-storage-utils.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
const clock_cjs_namespaceObject = require("../../../images/clock.cjs");
var clock_cjs_default = /*#__PURE__*/ __webpack_require__.n(clock_cjs_namespaceObject);
const recent_queries_cjs_namespaceObject = require("../../common/suggestions/recent-queries.cjs");
const suggestions_events_cjs_namespaceObject = require("../../common/suggestions/suggestions-events.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);
}
class AtomicSearchBoxRecentQueries extends external_lit_namespaceObject.LitElement {
    connectedCallback() {
        super.connectedCallback();
        try {
            (0, suggestions_events_cjs_namespaceObject.dispatchSearchBoxSuggestionsEvent)((bindings)=>{
                this.bindings = bindings;
                return this.initialize();
            }, this, [
                'atomic-search-box'
            ]);
        } catch (error) {
            this.error = error;
        }
    }
    initialize() {
        this.storage = new local_storage_utils_cjs_namespaceObject.SafeStorage();
        this.recentQueriesList = (0, headless_namespaceObject.buildRecentQueriesList)(this.bindings.engine, {
            initialState: {
                queries: this.retrieveLocalStorage()
            },
            options: {
                maxLength: 1000,
                clearFilters: this.bindings.clearFilters
            }
        });
        this.recentQueriesList.subscribe(()=>this.updateLocalStorage());
        return {
            position: Array.from(this.parentNode.children).indexOf(this),
            renderItems: ()=>this.renderItems()
        };
    }
    retrieveLocalStorage() {
        return this.storage.getParsedJSON(local_storage_utils_cjs_namespaceObject.StorageItems.RECENT_QUERIES, []);
    }
    updateLocalStorage() {
        if (!this.recentQueriesList.state.analyticsEnabled) return this.disableFeature();
        return this.storage.setJSON(local_storage_utils_cjs_namespaceObject.StorageItems.RECENT_QUERIES, this.recentQueriesList.state.queries);
    }
    disableFeature() {
        this.warnUser();
        this.storage.removeItem(local_storage_utils_cjs_namespaceObject.StorageItems.RECENT_QUERIES);
    }
    renderItems() {
        if (!this.recentQueriesList.state.analyticsEnabled) return [];
        const query = this.bindings.searchBoxController.state.value;
        const hasQuery = '' !== query;
        const max = hasQuery ? this.maxWithQuery : this.maxWithoutQuery;
        const filteredQueries = this.recentQueriesList.state.queries.filter((recentQuery)=>recentQuery.toLowerCase().startsWith(query.toLowerCase())).slice(0, max);
        const suggestionElements = filteredQueries.map((value)=>this.renderItem(value));
        if (suggestionElements.length) suggestionElements.unshift(this.renderClear());
        return suggestionElements;
    }
    renderClear() {
        const partialItem = (0, recent_queries_cjs_namespaceObject.getPartialRecentQueryClearElement)(this.bindings.i18n);
        return {
            ...partialItem,
            content: (0, recent_queries_cjs_namespaceObject.renderRecentQueryClear)({
                i18n: this.bindings.i18n
            }),
            onSelect: ()=>{
                this.recentQueriesList.clear();
                this.bindings.triggerSuggestions();
            }
        };
    }
    renderItem(value) {
        const query = this.bindings.searchBoxController.state.value;
        const partialItem = (0, recent_queries_cjs_namespaceObject.getPartialRecentQueryElement)(value, this.bindings.i18n);
        return {
            ...partialItem,
            content: (0, recent_queries_cjs_namespaceObject.renderRecentQuery)({
                icon: this.icon || clock_cjs_default(),
                query,
                value
            }),
            onSelect: ()=>{
                if (this.bindings.isStandalone) {
                    this.bindings.searchBoxController.updateText(value);
                    this.bindings.searchBoxController.submit();
                    return;
                }
                this.recentQueriesList.executeRecentQuery(this.recentQueriesList.state.queries.indexOf(value));
            }
        };
    }
    render() {
        return external_lit_namespaceObject.nothing;
    }
    constructor(...args){
        super(...args), this.maxWithQuery = 3, this.warnUser = (0, utils_cjs_namespaceObject.once)(()=>this.bindings.engine.logger.warn('The recent queries feature is deactivated because analytics are disabled.'));
    }
}
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicSearchBoxRecentQueries.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)(),
    _ts_metadata("design:type", String)
], AtomicSearchBoxRecentQueries.prototype, "icon", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        attribute: 'max-with-query',
        reflect: true
    })
], AtomicSearchBoxRecentQueries.prototype, "maxWithQuery", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        attribute: 'max-without-query',
        reflect: true
    }),
    _ts_metadata("design:type", Number)
], AtomicSearchBoxRecentQueries.prototype, "maxWithoutQuery", void 0);
_ts_decorate([
    (0, error_guard_cjs_namespaceObject.errorGuard)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicSearchBoxRecentQueries.prototype, "render", null);
AtomicSearchBoxRecentQueries = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-search-box-recent-queries')
], AtomicSearchBoxRecentQueries);
exports.AtomicSearchBoxRecentQueries = __webpack_exports__.AtomicSearchBoxRecentQueries;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicSearchBoxRecentQueries"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
