import { LitElement, html } from "lit";
import { customElement, 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 { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { errorGuard } from "../../../decorators/error-guard.js";
import { LightDomMixin } from "../../../mixins/light-dom.js";
import { defaultNumberFormatter } 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 AtomicProductNumericFieldValue extends LightDomMixin(LitElement) {
    initialize() {
        this.addEventListener('atomic/numberFormat', this.setFormat);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.removeEventListener('atomic/numberFormat', this.setFormat);
    }
    get value() {
        const product = this.productController.item;
        if (!product) return null;
        const value = parseValue(product, this.field);
        if (null === value) return null;
        return this.formatter(value, this.bindings.i18n.languages);
    }
    render() {
        return html`${when(null !== this.value, ()=>html`${this.value}`)}`;
    }
    constructor(...args){
        super(...args), this.productController = createProductContextController(this), this.formatter = defaultNumberFormatter, this.setFormat = (event)=>{
            const customEvent = event;
            customEvent.preventDefault();
            customEvent.stopPropagation();
            this.formatter = customEvent.detail;
        };
    }
}
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicProductNumericFieldValue.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicProductNumericFieldValue.prototype, "error", void 0);
_ts_decorate([
    property({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductNumericFieldValue.prototype, "field", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof NumberFormatter ? Object : NumberFormatter)
], AtomicProductNumericFieldValue.prototype, "formatter", void 0);
_ts_decorate([
    errorGuard(),
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicProductNumericFieldValue.prototype, "render", null);
AtomicProductNumericFieldValue = _ts_decorate([
    customElement('atomic-product-numeric-field-value'),
    bindings()
], AtomicProductNumericFieldValue);
export { AtomicProductNumericFieldValue };
