import { Schema, StringValue } from "@coveo/bueno";
import { buildDidYouMean, buildQueryTrigger } from "@coveo/headless";
import { LitElement, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { ValidatePropsController } from "../../common/validate-props-controller/validate-props-controller.js";
import { booleanConverter } from "../../../converters/boolean-converter.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 AtomicDidYouMean extends LitElement {
    initialize() {
        this.didYouMean = buildDidYouMean(this.bindings.engine, {
            options: {
                automaticallyCorrectQuery: this.automaticallyCorrectQuery,
                queryCorrectionMode: this.queryCorrectionMode
            }
        });
        this.queryTrigger = buildQueryTrigger(this.bindings.engine);
    }
    updated(changedProperties) {
        if (changedProperties.has('queryCorrectionMode')) this.didYouMean?.updateQueryCorrectionMode(this.queryCorrectionMode);
    }
    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-results-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)}`;
    }
    constructor(){
        super(), this.automaticallyCorrectQuery = true, this.queryCorrectionMode = 'next';
        new ValidatePropsController(this, ()=>({
                queryCorrectionMode: this.queryCorrectionMode
            }), AtomicDidYouMean.propsSchema, false);
    }
}
AtomicDidYouMean.propsSchema = new Schema({
    queryCorrectionMode: new StringValue({
        constrainTo: [
            'legacy',
            'next'
        ]
    })
});
_ts_decorate([
    bindStateToController('didYouMean'),
    state(),
    _ts_metadata("design:type", "u" < typeof DidYouMeanState ? Object : DidYouMeanState)
], AtomicDidYouMean.prototype, "didYouMeanState", void 0);
_ts_decorate([
    bindStateToController('queryTrigger'),
    state(),
    _ts_metadata("design:type", "u" < typeof QueryTriggerState ? Object : QueryTriggerState)
], AtomicDidYouMean.prototype, "queryTriggerState", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicDidYouMean.prototype, "error", void 0);
_ts_decorate([
    property({
        reflect: true,
        type: Boolean,
        converter: booleanConverter,
        attribute: 'automatically-correct-query'
    })
], AtomicDidYouMean.prototype, "automaticallyCorrectQuery", void 0);
_ts_decorate([
    property({
        reflect: true,
        type: String,
        attribute: 'query-correction-mode'
    }),
    _ts_metadata("design:type", String)
], AtomicDidYouMean.prototype, "queryCorrectionMode", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicDidYouMean.prototype, "render", null);
AtomicDidYouMean = _ts_decorate([
    customElement('atomic-did-you-mean'),
    withTailwindStyles,
    bindings(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicDidYouMean);
export { AtomicDidYouMean };
