"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__, {
    AtomicCommerceSearchBoxQuerySuggestions: ()=>AtomicCommerceSearchBoxQuerySuggestions
});
const commerce_namespaceObject = require("@coveo/headless/commerce");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const query_suggestions_cjs_namespaceObject = require("../../common/suggestions/query-suggestions.cjs");
const suggestions_events_cjs_namespaceObject = require("../../common/suggestions/suggestions-events.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const search_cjs_namespaceObject = require("../../../images/search.cjs");
var search_cjs_default = /*#__PURE__*/ __webpack_require__.n(search_cjs_namespaceObject);
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 AtomicCommerceSearchBoxQuerySuggestions extends external_lit_namespaceObject.LitElement {
    connectedCallback() {
        super.connectedCallback();
        try {
            (0, suggestions_events_cjs_namespaceObject.dispatchSearchBoxSuggestionsEvent)((bindings)=>{
                this.bindings = bindings;
                return this.initialize();
            }, this, [
                'atomic-commerce-search-box'
            ]);
        } catch (error) {
            this.error = error;
        }
    }
    initialize() {
        const engine = this.bindings.engine;
        const { registerQuerySuggest, fetchQuerySuggestions } = (0, commerce_namespaceObject.loadQuerySuggestActions)(engine);
        const numberOfQueries = this.bindings.numberOfQueries;
        const maxWithQuery = this.maxWithQuery;
        if (numberOfQueries < maxWithQuery) {
            const logger = this.bindings.engine.logger;
            logger.warn(`Query suggestions configuration mismatch: atomic-commerce-search-box has number-of-queries="${numberOfQueries}" but atomic-commerce-search-box-query-suggestions has max-with-query="${maxWithQuery}". This may cause inconsistent behavior where the search box requests ${numberOfQueries} suggestions but the component tries to display up to ${maxWithQuery}. Consider updating max-with-query to ${numberOfQueries} or increasing number-of-queries to ${maxWithQuery}.`);
        }
        engine.dispatch(registerQuerySuggest({
            id: this.bindings.id,
            count: this.bindings.numberOfQueries
        }));
        return {
            position: Array.from(this.parentNode.children).indexOf(this),
            onInput: ()=>engine.dispatch(fetchQuerySuggestions({
                    id: this.bindings.id
                })),
            renderItems: ()=>this.renderItems()
        };
    }
    renderItems() {
        const hasQuery = '' !== this.bindings.searchBoxController.state.value;
        const max = hasQuery ? this.maxWithQuery : this.maxWithoutQuery;
        return this.bindings.searchBoxController.state.suggestions.slice(0, max).map((suggestion)=>this.renderItem(suggestion));
    }
    renderItem(suggestion) {
        const partialItem = (0, query_suggestions_cjs_namespaceObject.getPartialSearchBoxSuggestionElement)(suggestion, this.bindings.i18n);
        const icon = this.icon ? this.icon : search_cjs_default();
        const hasQuery = '' !== this.bindings.searchBoxController.state.value;
        const hasMultipleKindOfSuggestions = this.bindings.getSuggestions().length > 1;
        return {
            ...partialItem,
            content: (0, query_suggestions_cjs_namespaceObject.renderQuerySuggestion)({
                icon,
                hasQuery,
                suggestion,
                hasMultipleKindOfSuggestions
            }),
            onSelect: ()=>{
                this.bindings.searchBoxController.selectSuggestion(suggestion.rawValue);
            }
        };
    }
    render() {
        return external_lit_namespaceObject.nothing;
    }
    constructor(...args){
        super(...args), this.maxWithQuery = 3;
    }
}
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceSearchBoxQuerySuggestions.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)(),
    _ts_metadata("design:type", String)
], AtomicCommerceSearchBoxQuerySuggestions.prototype, "icon", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        attribute: 'max-with-query',
        reflect: true
    })
], AtomicCommerceSearchBoxQuerySuggestions.prototype, "maxWithQuery", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        attribute: 'max-without-query',
        reflect: true
    }),
    _ts_metadata("design:type", Number)
], AtomicCommerceSearchBoxQuerySuggestions.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)
], AtomicCommerceSearchBoxQuerySuggestions.prototype, "render", null);
AtomicCommerceSearchBoxQuerySuggestions = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-commerce-search-box-query-suggestions')
], AtomicCommerceSearchBoxQuerySuggestions);
exports.AtomicCommerceSearchBoxQuerySuggestions = __webpack_exports__.AtomicCommerceSearchBoxQuerySuggestions;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicCommerceSearchBoxQuerySuggestions"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
