import { buildQueryTrigger, buildSearch } from "@coveo/headless/commerce";
import { LitElement, html, nothing } from "lit";
import { customElement, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { bindStateToController } from "../../../decorators/bind-state.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import { renderAutoCorrection } from "../../common/query-correction/auto-correction.js";
import { renderCorrection } from "../../common/query-correction/correction.js";
import { renderTriggerCorrection } from "../../common/query-correction/trigger-correction.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 AtomicCommerceDidYouMean extends LitElement {
    initialize() {
        if ('search' !== this.bindings.interfaceElement.type) this.error = new Error('atomic-commerce-did-you-mean is only usable with an atomic-commerce-interface of type "search"');
        this.didYouMean = buildSearch(this.bindings.engine).didYouMean();
        this.queryTrigger = buildQueryTrigger(this.bindings.engine);
    }
    get content() {
        const { hasQueryCorrection, wasAutomaticallyCorrected } = this.didYouMeanState;
        const hasTrigger = this.queryTriggerState.wasQueryModified;
        if (hasQueryCorrection && wasAutomaticallyCorrected) return renderAutoCorrection({
            props: {
                correctedTo: this.didYouMeanState.wasCorrectedTo,
                originalQuery: this.didYouMeanState.originalQuery,
                i18n: this.bindings.i18n
            }
        });
        if (hasQueryCorrection) return renderCorrection({
            props: {
                correctedQuery: this.didYouMeanState.queryCorrection.correctedQuery,
                i18n: this.bindings.i18n,
                onClick: ()=>this.didYouMean.applyCorrection()
            }
        });
        if (hasTrigger) return renderTriggerCorrection({
            props: {
                i18n: this.bindings.i18n,
                i18nKeyShowingItemsFor: 'showing-products-for',
                correctedQuery: this.queryTriggerState.newQuery,
                originalQuery: this.queryTriggerState.originalQuery,
                onClick: ()=>this.queryTrigger.undo()
            }
        });
        return nothing;
    }
    render() {
        const hasCorrection = this.didYouMeanState.hasQueryCorrection || this.queryTriggerState.wasQueryModified;
        return html`${when(hasCorrection, ()=>this.content)}`;
    }
}
_ts_decorate([
    bindStateToController('didYouMean'),
    state(),
    _ts_metadata("design:type", "u" < typeof DidYouMeanState ? Object : DidYouMeanState)
], AtomicCommerceDidYouMean.prototype, "didYouMeanState", void 0);
_ts_decorate([
    bindStateToController('queryTrigger'),
    state(),
    _ts_metadata("design:type", "u" < typeof QueryTriggerState ? Object : QueryTriggerState)
], AtomicCommerceDidYouMean.prototype, "queryTriggerState", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceDidYouMean.prototype, "error", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicCommerceDidYouMean.prototype, "render", null);
AtomicCommerceDidYouMean = _ts_decorate([
    customElement('atomic-commerce-did-you-mean'),
    withTailwindStyles,
    bindings()
], AtomicCommerceDidYouMean);
export { AtomicCommerceDidYouMean };
