import { buildContext } from "@coveo/headless/commerce";
import { LitElement, css, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { createProductContextController } from "../product-template-component-utils/context/product-context-controller.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 { multiClassMap, tw } from "../../../directives/multi-class-map.js";
import { LightDomMixin } from "../../../mixins/light-dom.js";
import { defaultCurrencyFormatter } from "../../common/formats/format-common.js";
import { parseValue } from "../product-template-component-utils/product-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 AtomicProductPrice extends LightDomMixin(LitElement) {
    initialize() {
        this.context = buildContext(this.bindings.engine);
    }
    get product() {
        return this.productController.item;
    }
    formatValue(value) {
        try {
            const { currency } = this.contextState;
            const formatter = defaultCurrencyFormatter(currency);
            return formatter(value, this.bindings.i18n.languages);
        } catch (error) {
            this.error = error;
            return value.toString();
        }
    }
    getFormattedValue(field) {
        const value = parseValue(this.product, field);
        if (null !== value) return this.formatValue(value);
    }
    get hasPromotionalPrice() {
        return this.product && null !== this.product.ec_promo_price && null !== this.product.ec_price && this.product.ec_promo_price < this.product.ec_price;
    }
    render() {
        const hasPromo = this.hasPromotionalPrice;
        const priceClasses = tw({
            'truncate break-keep text-2xl leading-[1.5]': true,
            'text-error': hasPromo
        });
        const promoClasses = tw({
            'original-price content-center truncate text-xl break-keep line-through leading-none': true,
            invisible: !hasPromo
        });
        return html`
      <div class="flex flex-wrap gap-1">
        <div class=${multiClassMap(priceClasses)}>
          ${this.getFormattedValue(hasPromo ? 'ec_promo_price' : 'ec_price')}
        </div>
        <div class=${multiClassMap(promoClasses)}>
          ${hasPromo ? this.getFormattedValue('ec_price') : '\u200B'}
        </div>
      </div>
    `;
    }
    constructor(...args){
        super(...args), this.productController = createProductContextController(this);
    }
}
AtomicProductPrice.styles = css`atomic-product-price.display-grid div{flex-direction:column}`;
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicProductPrice.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicProductPrice.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('context'),
    state(),
    _ts_metadata("design:type", "u" < typeof ContextState ? Object : ContextState)
], AtomicProductPrice.prototype, "contextState", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicProductPrice.prototype, "render", null);
AtomicProductPrice = _ts_decorate([
    customElement('atomic-product-price'),
    bindings()
], AtomicProductPrice);
export { AtomicProductPrice };
