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