import { ResultTemplatesHelpers } from "@coveo/headless";
import { LitElement, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { makeDefinedConditions, makeMatchConditions } from "../../common/template-controller/template-utils.js";
import { createResultContextController } from "../result-template-component-utils/context/result-context-controller.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { LightDomMixin } from "../../../mixins/light-dom.js";
import { mapProperty } from "../../../utils/props-utils.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 AtomicFieldCondition extends LightDomMixin(LitElement) {
    initialize() {}
    get conditions() {
        return [
            ...makeDefinedConditions(this.ifDefined, this.ifNotDefined, ResultTemplatesHelpers),
            ...makeMatchConditions(this.mustMatch, this.mustNotMatch, ResultTemplatesHelpers)
        ];
    }
    getResult() {
        const item = this.resultController.item;
        if (!item) return;
        return 'result' in item ? item.result : item;
    }
    render() {
        const result = this.getResult();
        if (!result || !this.conditions.every((condition)=>condition(result))) {
            this.hidden = true;
            return html``;
        }
        return html`<slot></slot>`;
    }
    constructor(...args){
        super(...args), this.resultController = createResultContextController(this);
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicFieldCondition.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicFieldCondition.prototype, "error", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'if-defined'
    }),
    _ts_metadata("design:type", String)
], AtomicFieldCondition.prototype, "ifDefined", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'if-not-defined'
    }),
    _ts_metadata("design:type", String)
], AtomicFieldCondition.prototype, "ifNotDefined", void 0);
_ts_decorate([
    mapProperty({
        splitValues: true,
        attributePrefix: 'must-match'
    }),
    _ts_metadata("design:type", "u" < typeof Record ? Object : Record)
], AtomicFieldCondition.prototype, "mustMatch", void 0);
_ts_decorate([
    mapProperty({
        splitValues: true,
        attributePrefix: 'must-not-match'
    }),
    _ts_metadata("design:type", "u" < typeof Record ? Object : Record)
], AtomicFieldCondition.prototype, "mustNotMatch", void 0);
_ts_decorate([
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicFieldCondition.prototype, "render", null);
AtomicFieldCondition = _ts_decorate([
    customElement('atomic-field-condition'),
    bindings()
], AtomicFieldCondition);
export { AtomicFieldCondition };
