import { loadQuerySuggestActions } from "@coveo/headless";
import { LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { getPartialSearchBoxSuggestionElement, renderQuerySuggestion } from "../../common/suggestions/query-suggestions.js";
import { dispatchSearchBoxSuggestionsEvent } from "../../common/suggestions/suggestions-events.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import search from "../../../images/search.js";
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 AtomicSearchBoxQuerySuggestions extends LitElement {
    connectedCallback() {
        try {
            super.connectedCallback();
        } catch (error) {
            this.error = error;
            return;
        }
        try {
            dispatchSearchBoxSuggestionsEvent((bindings)=>{
                this.bindings = bindings;
                return this.initialize();
            }, this, [
                'atomic-search-box'
            ]);
        } catch (error) {
            this.error = error;
        }
    }
    initialize() {
        const engine = this.bindings.engine;
        const { registerQuerySuggest, fetchQuerySuggestions } = loadQuerySuggestActions(engine);
        const numberOfQueries = this.bindings.numberOfQueries;
        const maxWithQuery = this.maxWithQuery;
        if (void 0 !== maxWithQuery && numberOfQueries < maxWithQuery) {
            const logger = this.bindings.engine.logger;
            logger.warn(`Query suggestions config mismatch: number-of-queries (${numberOfQueries}) < max-with-query (${maxWithQuery}). Update one to match the other.`);
        }
        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 = getPartialSearchBoxSuggestionElement(suggestion, this.bindings.i18n);
        const icon = this.icon ? this.icon : search;
        const hasQuery = '' !== this.bindings.searchBoxController.state.value;
        const hasMultipleKindOfSuggestions = this.bindings.getSuggestions().length > 1;
        return {
            ...partialItem,
            content: renderQuerySuggestion({
                icon,
                hasQuery,
                suggestion,
                hasMultipleKindOfSuggestions
            }),
            onSelect: ()=>{
                this.bindings.searchBoxController.selectSuggestion(suggestion.rawValue);
            }
        };
    }
    render() {
        return nothing;
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicSearchBoxQuerySuggestions.prototype, "error", void 0);
_ts_decorate([
    property(),
    _ts_metadata("design:type", String)
], AtomicSearchBoxQuerySuggestions.prototype, "icon", void 0);
_ts_decorate([
    property({
        type: Number,
        attribute: 'max-with-query',
        reflect: true
    }),
    _ts_metadata("design:type", Number)
], AtomicSearchBoxQuerySuggestions.prototype, "maxWithQuery", void 0);
_ts_decorate([
    property({
        type: Number,
        attribute: 'max-without-query',
        reflect: true
    }),
    _ts_metadata("design:type", Number)
], AtomicSearchBoxQuerySuggestions.prototype, "maxWithoutQuery", void 0);
_ts_decorate([
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicSearchBoxQuerySuggestions.prototype, "render", null);
AtomicSearchBoxQuerySuggestions = _ts_decorate([
    customElement('atomic-search-box-query-suggestions')
], AtomicSearchBoxQuerySuggestions);
export { AtomicSearchBoxQuerySuggestions };
