import { HighlightUtils, ProductTemplatesHelpers } from "@coveo/headless/commerce";
import { LitElement, html, nothing } from "lit";
import { customElement, property as decorators_js_property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { createProductContextController } from "../product-template-component-utils/context/product-context-controller.js";
import { renderItemTextFallback } from "../../common/item-text/item-text-fallback.js";
import { renderItemTextHighlighted } from "../../common/item-text/item-text-highlighted.js";
import { booleanConverter } from "../../../converters/boolean-converter.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { getFieldValueCaption } from "../../../utils/field-utils.js";
import "../atomic-commerce-text/atomic-commerce-text.js";
import { getStringValueFromProductOrNull } from "../product-template-component-utils/product-utils.js";
import { LightDomMixin } from "../../../mixins/light-dom.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 AtomicProductText extends LightDomMixin(LitElement) {
    initialize() {
        if (!this.product && this.productController.item) this.product = this.productController.item;
    }
    render() {
        return html`
      ${when(this.product && this.field, ()=>this.renderProductTextValue(), ()=>nothing)}
    `;
    }
    get shouldRenderHighlights() {
        return !this.disableHighlight && this.shouldHighlight && this.isFieldSupportedForHighlighting();
    }
    isFieldSupportedForHighlighting() {
        return [
            'ec_name',
            'excerpt'
        ].includes(this.field);
    }
    get highlightKeywords() {
        if (!this.shouldRenderHighlights) return null;
        return ProductTemplatesHelpers.getProductProperty(this.product, 'ec_name' === this.field ? 'nameHighlights' : 'excerptHighlights');
    }
    renderFallback() {
        if (!this.default) return nothing;
        return renderItemTextFallback({
            props: {
                field: this.field,
                host: this,
                logger: this.bindings.engine.logger,
                defaultValue: this.default,
                item: this.product,
                getProperty: (result, property)=>ProductTemplatesHelpers.getProductProperty(result, property)
            }
        })(html`
      <atomic-commerce-text
        .value=${getFieldValueCaption(this.field, this.default, this.bindings.i18n)}
      ></atomic-commerce-text>
    `);
    }
    renderProductText(textValue) {
        const highlightKeywords = this.highlightKeywords;
        return this.shouldRenderHighlights && highlightKeywords && highlightKeywords.length > 0 ? renderItemTextHighlighted({
            props: {
                textValue,
                highlightKeywords,
                highlightString: HighlightUtils.highlightString,
                onError: (error)=>{
                    this.error = error;
                }
            }
        }) : html`
          <atomic-commerce-text
            .value=${getFieldValueCaption(this.field, textValue, this.bindings.i18n)}
          ></atomic-commerce-text>
        `;
    }
    renderProductTextValue() {
        const productValueAsString = getStringValueFromProductOrNull(this.product, this.field);
        if (null === productValueAsString) return this.renderFallback();
        return this.renderProductText(`${productValueAsString}`);
    }
    constructor(...args){
        super(...args), this.shouldHighlight = true, this.disableHighlight = false, this.productController = createProductContextController(this);
    }
}
_ts_decorate([
    decorators_js_property({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductText.prototype, "field", void 0);
_ts_decorate([
    decorators_js_property({
        type: Boolean,
        attribute: 'should-highlight',
        reflect: true,
        converter: booleanConverter
    })
], AtomicProductText.prototype, "shouldHighlight", void 0);
_ts_decorate([
    decorators_js_property({
        type: Boolean,
        reflect: true,
        useDefault: true,
        attribute: 'no-highlight'
    }),
    _ts_metadata("design:type", Boolean)
], AtomicProductText.prototype, "disableHighlight", void 0);
_ts_decorate([
    decorators_js_property({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductText.prototype, "default", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Product ? Object : Product)
], AtomicProductText.prototype, "product", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicProductText.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicProductText.prototype, "error", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicProductText.prototype, "render", null);
AtomicProductText = _ts_decorate([
    customElement('atomic-product-text'),
    bindings()
], AtomicProductText);
export { AtomicProductText };
