"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    AtomicFacetDateInput: ()=>AtomicFacetDateInput
});
const headless_namespaceObject = require("@coveo/headless");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const if_defined_js_namespaceObject = require("lit/directives/if-defined.js");
const ref_js_namespaceObject = require("lit/directives/ref.js");
const external_button_cjs_namespaceObject = require("../button.cjs");
const binding_guard_cjs_namespaceObject = require("../../../decorators/binding-guard.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const light_dom_cjs_namespaceObject = require("../../../mixins/light-dom.cjs");
const date_utils_cjs_namespaceObject = require("../../../utils/date-utils.cjs");
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 AtomicFacetDateInput extends (0, light_dom_cjs_namespaceObject.LightDomMixin)(external_lit_namespaceObject.LitElement) {
    initialize() {}
    connectedCallback() {
        super.connectedCallback();
        this.validateParentComponent();
        this.start = this.inputRange ? (0, date_utils_cjs_namespaceObject.parseDate)(this.inputRange.start).toDate() : void 0;
        this.end = this.inputRange ? (0, date_utils_cjs_namespaceObject.parseDate)(this.inputRange.end).toDate() : void 0;
    }
    validateParentComponent() {
        const validParents = [
            'atomic-commerce-timeframe-facet',
            'atomic-insight-timeframe-facet',
            'atomic-timeframe-facet'
        ];
        const parentElement = this.parentElement;
        const shadowHost = this.getRootNode() instanceof ShadowRoot ? this.getRootNode().host : null;
        const hostElements = [
            parentElement,
            shadowHost
        ].filter(Boolean);
        const isValidParent = hostElements.some(({ tagName })=>validParents.includes(tagName.toLowerCase()));
        if (!isValidParent) {
            const error = new Error('atomic-facet-date-input is an internal component and should only be used within a timeframe facet.');
            this.error = error;
        }
    }
    updated(_changedProperties) {
        super.updated(_changedProperties);
        const startInput = this.startRef.value;
        const endInput = this.endRef.value;
        if (startInput && endInput && !startInput.value && !endInput.value) {
            startInput.min = this.min || this.formattedDateValue('1401-01-01');
            endInput.max = this.max || '';
            startInput.max = this.max || '';
            endInput.min = this.min || '';
        }
    }
    apply() {
        const startInput = this.startRef.value;
        const endInput = this.endRef.value;
        if (!startInput?.validity.valid || !endInput?.validity.valid) return;
        const rangeRequest = (0, headless_namespaceObject.buildDateRange)({
            start: this.start,
            end: new Date(this.end.getTime()).setHours(23, 59, 59, 999)
        });
        const eventDict = {
            detail: {
                start: rangeRequest.start,
                end: rangeRequest.end,
                endInclusive: rangeRequest.endInclusive
            },
            bubbles: true,
            composed: true,
            cancelable: true
        };
        this.dispatchEvent(new CustomEvent('atomic-date-input-apply', eventDict));
        this.dispatchEvent(new CustomEvent('atomic/dateInputApply', eventDict));
    }
    formattedDateValue(date) {
        if (!date) return '';
        return (0, date_utils_cjs_namespaceObject.parseDate)(date).format('YYYY-MM-DD');
    }
    render() {
        const label = this.bindings.i18n.t(this.label);
        const startLabel = this.bindings.i18n.t('start');
        const endLabel = this.bindings.i18n.t('end');
        const startAria = this.bindings.i18n.t('date-input-start', {
            label
        });
        const endAria = this.bindings.i18n.t('date-input-end', {
            label
        });
        const apply = this.bindings.i18n.t('apply');
        const applyAria = this.bindings.i18n.t('date-input-apply', {
            label
        });
        const inputClasses = 'input-primary p-2.5';
        const labelClasses = 'text-neutral-dark self-center';
        const placeholder = this.bindings.i18n.t('date-format-placeholder');
        const pattern = '^(1[4-9]\\d{2}|2\\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$';
        return (0, external_lit_namespaceObject.html)`
      <form
        class="mt-4 grid grid-cols-[min-content_1fr] gap-2 px-2"
        @submit=${(e)=>{
            e.preventDefault();
            this.apply();
            return false;
        }}
      >
        <label
          part="input-label"
          class=${labelClasses}
          for=${`${this.facetId}_start`}
        >
          ${startLabel}:
        </label>
        <input
          id=${`${this.facetId}_start`}
          part="input-start"
          type="date"
          ${(0, ref_js_namespaceObject.ref)(this.startRef)}
          class=${inputClasses}
          aria-label=${startAria}
          placeholder=${placeholder}
          pattern=${pattern}
          required
          min=${(0, if_defined_js_namespaceObject.ifDefined)(this.min || this.formattedDateValue('1401-01-01'))}
          max=${(0, if_defined_js_namespaceObject.ifDefined)(this.end ? this.formattedDateValue(this.end) : this.max)}
          .value=${this.formattedDateValue(this.inputRange?.start)}
          @input=${(e)=>{
            this.start = (0, date_utils_cjs_namespaceObject.parseDate)(e.target.value).toDate();
        }}
        />
        <label
          part="input-label"
          class=${labelClasses}
          for=${`${this.facetId}_end`}
        >
          ${endLabel}:
        </label>
        <input
          id=${`${this.facetId}_end`}
          part="input-end"
          type="date"
          ${(0, ref_js_namespaceObject.ref)(this.endRef)}
          class=${inputClasses}
          aria-label=${endAria}
          placeholder=${placeholder}
          pattern=${pattern}
          required
          min=${(0, if_defined_js_namespaceObject.ifDefined)(this.formattedDateValue(this.start) || this.min)}
          max=${(0, if_defined_js_namespaceObject.ifDefined)(this.max)}
          .value=${this.formattedDateValue(this.inputRange?.end)}
          @input=${(e)=>{
            this.end = (0, date_utils_cjs_namespaceObject.parseDate)(e.target.value).toDate();
        }}
        />
        ${(0, external_button_cjs_namespaceObject.renderButton)({
            props: {
                style: 'outline-primary',
                type: 'submit',
                part: 'input-apply-button',
                class: 'col-span-2 truncate p-2.5',
                ariaLabel: applyAria,
                text: apply
            }
        })((0, external_lit_namespaceObject.html)``)}
      </form>
    `;
    }
    constructor(...args){
        super(...args), this.startRef = (0, ref_js_namespaceObject.createRef)(), this.endRef = (0, ref_js_namespaceObject.createRef)(), this.max = '9999-12-31';
    }
}
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Date ? Object : Date)
], AtomicFacetDateInput.prototype, "start", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Date ? Object : Date)
], AtomicFacetDateInput.prototype, "end", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", Object)
], AtomicFacetDateInput.prototype, "inputRange", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)(),
    _ts_metadata("design:type", String)
], AtomicFacetDateInput.prototype, "facetId", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)(),
    _ts_metadata("design:type", String)
], AtomicFacetDateInput.prototype, "label", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)(),
    _ts_metadata("design:type", String)
], AtomicFacetDateInput.prototype, "min", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)(),
    _ts_metadata("design:type", String)
], AtomicFacetDateInput.prototype, "max", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicFacetDateInput.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof AnyBindings ? Object : AnyBindings)
], AtomicFacetDateInput.prototype, "bindings", void 0);
_ts_decorate([
    (0, binding_guard_cjs_namespaceObject.bindingGuard)(),
    (0, error_guard_cjs_namespaceObject.errorGuard)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicFacetDateInput.prototype, "render", null);
AtomicFacetDateInput = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-facet-date-input'),
    (0, bindings_cjs_namespaceObject.bindings)()
], AtomicFacetDateInput);
exports.AtomicFacetDateInput = __webpack_exports__.AtomicFacetDateInput;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicFacetDateInput"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
