"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__, {
    AtomicInsightSearchBox: ()=>AtomicInsightSearchBox
});
const insight_namespaceObject = require("@coveo/headless/insight");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const class_map_js_namespaceObject = require("lit/directives/class-map.js");
const ref_js_namespaceObject = require("lit/directives/ref.js");
const search_box_wrapper_cjs_namespaceObject = require("../../common/search-box/search-box-wrapper.cjs");
const search_text_area_cjs_namespaceObject = require("../../common/search-box/search-text-area.cjs");
const query_suggestions_cjs_namespaceObject = require("../../common/suggestions/query-suggestions.cjs");
const suggestion_manager_cjs_namespaceObject = require("../../common/suggestions/suggestion-manager.cjs");
const suggestions_utils_cjs_namespaceObject = require("../../common/suggestions/suggestions-utils.cjs");
const bind_state_cjs_namespaceObject = require("../../../decorators/bind-state.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const accessibility_utils_cjs_namespaceObject = require("../../../utils/accessibility-utils.cjs");
const device_utils_cjs_namespaceObject = require("../../../utils/device-utils.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
const search_slim_cjs_namespaceObject = require("../../../images/search-slim.cjs");
var search_slim_cjs_default = /*#__PURE__*/ __webpack_require__.n(search_slim_cjs_namespaceObject);
require("../../common/atomic-icon/atomic-icon.cjs");
const binding_guard_cjs_namespaceObject = require("../../../decorators/binding-guard.cjs");
const external_atomic_insight_search_box_tw_css_cjs_namespaceObject = require("./atomic-insight-search-box.tw.css.cjs");
var external_atomic_insight_search_box_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(external_atomic_insight_search_box_tw_css_cjs_namespaceObject);
require("@coveo/atomic-legacy/atomic-suggestion-renderer");
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 AtomicInsightSearchBox extends external_lit_namespaceObject.LitElement {
    initialize() {
        this.searchBoxId = (0, utils_cjs_namespaceObject.randomID)('atomic-search-box-');
        const searchBoxOptions = {
            id: this.searchBoxId,
            numberOfSuggestions: 0,
            highlightOptions: {
                notMatchDelimiters: {
                    open: '<span class="font-bold">',
                    close: '</span>'
                },
                correctionDelimiters: {
                    open: '<span class="font-normal">',
                    close: '</span>'
                }
            }
        };
        const { fetchQuerySuggestions, registerQuerySuggest } = (0, insight_namespaceObject.loadInsightSearchActions)(this.bindings.engine);
        this.searchBox = (0, insight_namespaceObject.buildSearchBox)(this.bindings.engine, {
            options: searchBoxOptions
        });
        this.bindings.engine.dispatch(registerQuerySuggest({
            id: this.searchBoxId,
            count: this.numberOfSuggestions
        }));
        this.suggestionManager = new suggestion_manager_cjs_namespaceObject.SuggestionManager({
            getNumberOfSuggestionsToDisplay: ()=>this.numberOfSuggestions,
            updateQuery: (query)=>this.searchBox.updateText(query),
            getSearchBoxValue: ()=>this.searchBoxState.value,
            getSuggestionTimeout: ()=>500,
            getSuggestionDelay: ()=>0,
            getHost: ()=>this,
            getLogger: ()=>this.bindings.engine.logger
        });
        this.suggestionManager.registerSuggestions({
            position: 0,
            renderItems: ()=>this.searchBox.state.suggestions.map((suggestion)=>this.renderSuggestionItem(suggestion)),
            onInput: ()=>this.bindings.engine.dispatch(fetchQuerySuggestions({
                    id: this.searchBoxId
                })),
            panel: 'left'
        });
    }
    onSubmit() {
        if (this.suggestionManager.activeDescendantElement) return void this.suggestionManager.clickOnActiveElement();
        this.searchBox.submit();
        this.suggestionManager.clearSuggestions();
    }
    async onKeyDown(e) {
        if (this.disableSearch) return;
        switch(e.key){
            case 'Enter':
                if (!e.shiftKey) this.onSubmit();
                break;
            case 'Escape':
                this.suggestionManager.clearSuggestions();
                break;
            case 'ArrowDown':
                e.preventDefault();
                await this.suggestionManager.focusNextValue();
                this.announceNewActiveSuggestionToScreenReader();
                break;
            case 'ArrowUp':
                e.preventDefault();
                await this.suggestionManager.focusPreviousValue();
                this.announceNewActiveSuggestionToScreenReader();
                break;
            case 'Tab':
                this.suggestionManager.clearSuggestions();
                break;
        }
    }
    triggerTextAreaChange(value) {
        if (!this.textAreaRef.value) return;
        this.textAreaRef.value.value = value;
        this.textAreaRef.value.dispatchEvent(new window.Event('change'));
    }
    renderSuggestion(item, index, lastIndex) {
        const id = `${this.searchBoxId}-suggestion-${item.key}`;
        const isSelected = id === this.suggestionManager.activeDescendant || this.suggestionManager.suggestedQuery === item.query;
        if (index === lastIndex && item.hideIfLast) return external_lit_namespaceObject.nothing;
        return (0, external_lit_namespaceObject.html)`<atomic-suggestion-renderer
      .i18n=${this.bindings.i18n}
      .id=${id}
      .suggestion=${item}
      .isSelected=${isSelected}
      .side=${'left'}
      .index=${index}
      .lastIndex=${lastIndex}
      .isDoubleList=${false}
      @click=${(e)=>{
            this.suggestionManager.onSuggestionClick(item, e);
        }}
      @mouseenter=${()=>{
            this.suggestionManager.onSuggestionMouseOver(item, 'left', id);
        }}
    ></atomic-suggestion-renderer>`;
    }
    renderSuggestionItem(suggestion) {
        const hasQuery = '' !== this.searchBox.state.value;
        const partialItem = (0, query_suggestions_cjs_namespaceObject.getPartialSearchBoxSuggestionElement)(suggestion, this.bindings.i18n);
        return {
            ...partialItem,
            content: (0, query_suggestions_cjs_namespaceObject.renderQuerySuggestion)({
                icon: search_slim_cjs_default(),
                hasQuery,
                suggestion,
                hasMultipleKindOfSuggestions: false,
                alwaysShowIcon: this.searchBoxState.suggestions.length > 1
            }),
            onSelect: ()=>{
                this.searchBox.selectSuggestion(suggestion.rawValue);
            }
        };
    }
    renderPanel(elements, setRef, getRef) {
        if (!elements.length) return external_lit_namespaceObject.nothing;
        return (0, external_lit_namespaceObject.html)`<div
      part="suggestions"
      ${(0, ref_js_namespaceObject.ref)(setRef)}
      class="flex grow basis-1/2 flex-col"
      @mousedown=${(e)=>{
            if (e.target === getRef()) e.preventDefault();
        }}
    >
      ${elements.map((suggestion, index)=>this.renderSuggestion(suggestion, index, elements.length - 1))}
    </div>`;
    }
    renderSuggestions() {
        if (!this.suggestionManager.hasSuggestions) {
            this.suggestionManager.updateActiveDescendant();
            return external_lit_namespaceObject.nothing;
        }
        const isVisible = this.suggestionManager.hasSuggestions && this.isExpanded;
        const classes = {
            'bg-background border-neutral absolute top-full left-0 z-10 flex w-full rounded-md border': true,
            hidden: !isVisible
        };
        return (0, external_lit_namespaceObject.html)`<div
      id="${this.searchBoxId}-popup"
      part="suggestions-wrapper"
      class=${(0, class_map_js_namespaceObject.classMap)(classes)}
      role="application"
      aria-label=${this.bindings.i18n.t('search-suggestions-single-list')}
      aria-activedescendant=${this.suggestionManager.activeDescendant}
    >
      ${this.renderPanel(this.suggestionManager.allSuggestionElements, (el)=>{
            this.suggestionManager.leftPanel = el;
        }, ()=>this.suggestionManager.leftPanel)}
    </div>`;
    }
    getSearchInputLabel() {
        if ((0, device_utils_cjs_namespaceObject.isMacOS)()) return this.bindings.i18n.t('search-box-with-suggestions-macos');
        if (!(0, device_utils_cjs_namespaceObject.hasKeyboard)()) return this.bindings.i18n.t('search-box-with-suggestions-keyboardless');
        return this.bindings.i18n.t('search-box-with-suggestions');
    }
    async onFocus() {
        if (this.isExpanded) return;
        this.isExpanded = true;
        await this.suggestionManager.triggerSuggestions();
        this.announceNewSuggestionsToScreenReader();
    }
    async onInput(value) {
        this.searchBox.updateText(value);
        this.isExpanded = true;
        await this.suggestionManager.triggerSuggestions();
        this.announceNewSuggestionsToScreenReader();
    }
    announceNewActiveSuggestionToScreenReader() {
        const ariaLabel = this.suggestionManager.activeDescendantElement?.ariaLabel;
        if ((0, device_utils_cjs_namespaceObject.isMacOS)() && ariaLabel) this.suggestionsAriaMessage.message = ariaLabel;
    }
    announceNewSuggestionsToScreenReader() {
        const numberOfSuggestionsToAnnounce = this.suggestionManager.allSuggestionElements.filter(suggestions_utils_cjs_namespaceObject.elementHasQuery).length;
        this.searchBoxAriaMessage.message = numberOfSuggestionsToAnnounce ? this.bindings.i18n.t(this.searchBoxState.value ? 'query-suggestions-available' : 'query-suggestions-available-no-query', {
            count: numberOfSuggestionsToAnnounce,
            query: this.searchBoxState.value
        }) : this.bindings.i18n.t('query-suggestions-unavailable');
    }
    render() {
        return (0, external_lit_namespaceObject.html)`${(0, search_box_wrapper_cjs_namespaceObject.renderSearchBoxWrapper)({
            props: {
                disabled: this.disableSearch,
                onFocusout: (event)=>{
                    if (!(0, utils_cjs_namespaceObject.isFocusingOut)(event)) return;
                    this.suggestionManager.clearSuggestions();
                    this.isExpanded = false;
                }
            }
        })((0, external_lit_namespaceObject.html)`
      <atomic-icon
        part="submit-icon"
        .icon=${search_slim_cjs_default()}
        class="my-auto mr-0 ml-4 h-4 w-4"
      ></atomic-icon>
      ${(0, search_text_area_cjs_namespaceObject.renderSearchBoxTextArea)({
            props: {
                textAreaRef: this.textAreaRef,
                loading: this.searchBoxState.isLoading,
                i18n: this.bindings.i18n,
                value: this.searchBoxState.value,
                ariaLabel: this.getSearchInputLabel(),
                title: this.bindings.i18n.t('search'),
                onClear: ()=>{
                    this.searchBox.clear();
                    this.triggerTextAreaChange('');
                },
                popup: {
                    id: `${this.searchBoxId}-popup`,
                    activeDescendant: this.suggestionManager.activeDescendant || '',
                    expanded: this.isExpanded && this.suggestionManager.hasSuggestions,
                    hasSuggestions: this.suggestionManager.hasSuggestions
                },
                onInput: (e)=>this.onInput(e.target.value),
                onFocus: ()=>this.onFocus(),
                onKeyDown: (e)=>this.onKeyDown(e)
            }
        })}
      ${this.renderSuggestions()}
    `)}`;
    }
    constructor(...args){
        super(...args), this.isExpanded = false, this.disableSearch = false, this.numberOfSuggestions = 5, this.textAreaRef = (0, ref_js_namespaceObject.createRef)(), this.searchBoxAriaMessage = new accessibility_utils_cjs_namespaceObject.AriaLiveRegionController(this, 'search-box'), this.suggestionsAriaMessage = new accessibility_utils_cjs_namespaceObject.AriaLiveRegionController(this, 'search-suggestions', true);
    }
}
AtomicInsightSearchBox.shadowRootOptions = {
    ...external_lit_namespaceObject.LitElement.shadowRootOptions,
    delegatesFocus: true
};
AtomicInsightSearchBox.styles = external_atomic_insight_search_box_tw_css_cjs_default();
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof InsightBindings ? Object : InsightBindings)
], AtomicInsightSearchBox.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicInsightSearchBox.prototype, "error", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('searchBox'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof InsightSearchBoxState ? Object : InsightSearchBoxState)
], AtomicInsightSearchBox.prototype, "searchBoxState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicInsightSearchBox.prototype, "isExpanded", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        reflect: true,
        attribute: 'disable-search'
    })
], AtomicInsightSearchBox.prototype, "disableSearch", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        reflect: true,
        attribute: 'number-of-suggestions'
    })
], AtomicInsightSearchBox.prototype, "numberOfSuggestions", void 0);
_ts_decorate([
    (0, error_guard_cjs_namespaceObject.errorGuard)(),
    (0, binding_guard_cjs_namespaceObject.bindingGuard)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicInsightSearchBox.prototype, "render", null);
AtomicInsightSearchBox = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-insight-search-box'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicInsightSearchBox);
exports.AtomicInsightSearchBox = __webpack_exports__.AtomicInsightSearchBox;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicInsightSearchBox"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
