import { isUndefined } from "@coveo/bueno";
import { LitElement, css, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { ref as ref_js_ref } from "lit/directives/ref.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 { buildCustomEvent } from "../../../utils/event-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 AtomicCommerceFacetNumberInput extends LightDomMixin(LitElement) {
    initialize() {
        this.start = this.range?.start;
        this.end = this.range?.end;
    }
    apply() {
        if (!this.startRef?.validity.valid || !this.endRef?.validity.valid) return;
        this.dispatchEvent(buildCustomEvent('atomic-number-input-apply', {
            start: this.start,
            end: this.end
        }));
    }
    get absoluteMinimum() {
        const { field } = this.facet.state;
        const isPriceField = [
            'ec_price',
            'ec_promo_price'
        ].includes(field);
        return isPriceField ? 0 : Number.MIN_SAFE_INTEGER;
    }
    get minimumInputValue() {
        return isUndefined(this.start) ? this.absoluteMinimum : this.start;
    }
    get maximumInputValue() {
        return isUndefined(this.end) ? Number.MAX_SAFE_INTEGER : this.end;
    }
    get startValue() {
        return this.range?.start !== void 0 ? String(this.range.start) : '';
    }
    get endValue() {
        return this.range?.end !== void 0 ? String(this.range.end) : '';
    }
    render() {
        const { facetId } = this.facet.state;
        const label = this.bindings.i18n.t(this.label);
        const minText = this.bindings.i18n.t('min');
        const maxText = this.bindings.i18n.t('max');
        const minAria = this.bindings.i18n.t('number-input-minimum', {
            label
        });
        const maxAria = this.bindings.i18n.t('number-input-maximum', {
            label
        });
        const apply = this.bindings.i18n.t('apply');
        const applyAria = this.bindings.i18n.t('number-input-apply', {
            label
        });
        const inputClasses = 'p-2.5 input-primary placeholder-neutral-dark min-w-0 mr-1';
        const labelClasses = 'text-neutral-dark text-sm';
        const step = 'any';
        return html`
      <form
        class="mt-4 gap-y-0.5 px-2"
        part="input-form"
        @submit=${(e)=>{
            e.preventDefault();
            this.apply();
            return false;
        }}
      >
        <label part="label-start" class=${labelClasses} for="${facetId}_start">
          ${minText}
        </label>
        <input
          part="input-start"
          id="${facetId}_start"
          type="number"
          step=${step}
          .value=${this.startValue}
          class=${inputClasses}
          aria-label=${minAria}
          required
          min=${this.absoluteMinimum}
          max=${this.maximumInputValue}
          @input=${(e)=>{
            this.start = e.target.valueAsNumber;
        }}
          ${ref_js_ref((ref)=>{
            this.startRef = ref;
        })}
        />
        <label part="label-end" class=${labelClasses} for="${facetId}_end">
          ${maxText}
        </label>
        <input
          part="input-end"
          id="${facetId}_end"
          type="number"
          step=${step}
          .value=${this.endValue}
          class=${inputClasses}
          aria-label=${maxAria}
          required
          min=${this.minimumInputValue}
          max=${Number.MAX_SAFE_INTEGER}
          @input=${(e)=>{
            this.end = e.target.valueAsNumber;
        }}
          ${ref_js_ref((ref)=>{
            this.endRef = ref;
        })}
        />
        <button
          style="outline-primary"
          type="submit"
          part="input-apply-button"
          class="flex-none truncate p-2.5"
          aria-label=${applyAria}
        >
          ${apply}
        </button>
      </form>
    `;
    }
}
AtomicCommerceFacetNumberInput.styles = css`[part=input-form]{display:grid;grid-template-areas:"label-start label-end ." "input-start input-end apply-button";grid-template-columns:1fr 1fr auto}[part=label-start]{grid-area:label-start}[part=label-end]{grid-area:label-end}[part=input-start]{grid-area:input-start}[part=input-end]{grid-area:input-end}[part=input-apply-button]{grid-area:apply-button}`;
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicCommerceFacetNumberInput.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceFacetNumberInput.prototype, "error", void 0);
_ts_decorate([
    property({
        type: String
    }),
    _ts_metadata("design:type", String)
], AtomicCommerceFacetNumberInput.prototype, "label", void 0);
_ts_decorate([
    property({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof Range ? Object : Range)
], AtomicCommerceFacetNumberInput.prototype, "range", void 0);
_ts_decorate([
    property({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof NumericFacet ? Object : NumericFacet)
], AtomicCommerceFacetNumberInput.prototype, "facet", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Number)
], AtomicCommerceFacetNumberInput.prototype, "start", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", Number)
], AtomicCommerceFacetNumberInput.prototype, "end", void 0);
_ts_decorate([
    bindingGuard(),
    errorGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicCommerceFacetNumberInput.prototype, "render", null);
AtomicCommerceFacetNumberInput = _ts_decorate([
    customElement('atomic-commerce-facet-number-input'),
    bindings()
], AtomicCommerceFacetNumberInput);
export { AtomicCommerceFacetNumberInput };
