"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __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__, {
    AtomicCommerceTimeframeFacet: ()=>AtomicCommerceTimeframeFacet
});
const commerce_namespaceObject = require("@coveo/headless/commerce");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const facet_container_cjs_namespaceObject = require("../../common/facets/facet-container/facet-container.cjs");
const facet_header_cjs_namespaceObject = require("../../common/facets/facet-header/facet-header.cjs");
const facet_value_label_highlight_cjs_namespaceObject = require("../../common/facets/facet-value-label-highlight/facet-value-label-highlight.cjs");
const facet_value_link_cjs_namespaceObject = require("../../common/facets/facet-value-link/facet-value-link.cjs");
const facet_values_group_cjs_namespaceObject = require("../../common/facets/facet-values-group/facet-values-group.cjs");
const boolean_converter_cjs_namespaceObject = require("../../../converters/boolean-converter.cjs");
const bind_state_cjs_namespaceObject = require("../../../decorators/bind-state.cjs");
const binding_guard_cjs_namespaceObject = require("../../../decorators/binding-guard.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const accessibility_utils_cjs_namespaceObject = require("../../../utils/accessibility-utils.cjs");
const date_utils_cjs_namespaceObject = require("../../../utils/date-utils.cjs");
require("../../common/atomic-facet-date-input/atomic-facet-date-input.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const facet_common_cjs_namespaceObject = require("../../common/facets/facet-common.cjs");
const facet_common_tw_css_cjs_namespaceObject = require("../../common/facets/facet-common.tw.css.cjs");
var facet_common_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(facet_common_tw_css_cjs_namespaceObject);
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 AtomicCommerceTimeframeFacet extends external_lit_namespaceObject.LitElement {
    get displayName() {
        return this.facetState.displayName || 'no-label';
    }
    get focusTarget() {
        if (!this.headerFocus) this.headerFocus = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
        return this.headerFocus;
    }
    initialize() {
        this.validateFacet();
    }
    applyDateInput(event) {
        this.inputRange = {
            start: event.detail.start,
            end: event.detail.end
        };
    }
    get valuesToRender() {
        return this.facetState.values.filter((value)=>value.numberOfResults || 'idle' !== value.state) || [];
    }
    get shouldRenderValues() {
        return !this.hasInputRange && !!this.valuesToRender.length;
    }
    get shouldRenderFacet() {
        return this.shouldRenderInput || this.shouldRenderValues;
    }
    get shouldRenderInput() {
        const { firstRequestExecuted: firstSearchExecuted, hasError, isLoading, hasProducts: hasResults } = this.summaryState;
        return (0, facet_common_cjs_namespaceObject.shouldDisplayInputForFacetRange)({
            hasInputRange: this.hasInputRange,
            searchStatusState: {
                firstSearchExecuted,
                hasError,
                hasResults,
                isLoading
            },
            facetValues: this.facetState.values || [],
            hasInput: true
        });
    }
    get numberOfSelectedValues() {
        if (this.hasInputRange) return 1;
        return this.facetState.values.filter(({ state })=>'selected' === state).length || 0;
    }
    get hasInputRange() {
        return !!this.inputRange;
    }
    resetRange() {
        this.inputRange = void 0;
        this.facet.setRanges([]);
    }
    formatFacetValue(facetValue) {
        try {
            const startDate = (0, commerce_namespaceObject.deserializeRelativeDate)(facetValue.start);
            const relativeDate = 'past' === startDate.period ? startDate : (0, commerce_namespaceObject.deserializeRelativeDate)(facetValue.end);
            return this.bindings.i18n.t(`${relativeDate.period}-${relativeDate.unit}`, {
                count: relativeDate.amount
            });
        } catch (_error) {
            return this.bindings.i18n.t('to', {
                start: (0, date_utils_cjs_namespaceObject.parseDate)(facetValue.start).format('YYYY-MM-DD'),
                end: (0, date_utils_cjs_namespaceObject.parseDate)(facetValue.end).format('YYYY-MM-DD')
            });
        }
    }
    renderValues() {
        return this.renderValuesContainer(this.valuesToRender.map((value)=>this.renderValue(value)));
    }
    renderValue(facetValue) {
        const displayValue = this.formatFacetValue(facetValue);
        const isSelected = 'selected' === facetValue.state;
        const isExcluded = 'excluded' === facetValue.state;
        return (0, facet_value_link_cjs_namespaceObject.renderFacetValueLink)({
            props: {
                displayValue,
                isSelected,
                numberOfResults: facetValue.numberOfResults,
                i18n: this.bindings.i18n,
                onClick: ()=>this.facet.toggleSingleSelect(facetValue)
            }
        })((0, facet_value_label_highlight_cjs_namespaceObject.renderFacetValueLabelHighlight)({
            props: {
                displayValue,
                isSelected,
                isExcluded
            }
        }));
    }
    renderValuesContainer(children) {
        return (0, facet_values_group_cjs_namespaceObject.renderFacetValuesGroup)({
            props: {
                i18n: this.bindings.i18n,
                label: this.displayName
            }
        })((0, external_lit_namespaceObject.html)`<ul class="mt-3" part="values">
        ${children}
      </ul>`);
    }
    renderHeader() {
        return (0, facet_header_cjs_namespaceObject.renderFacetHeader)({
            props: {
                i18n: this.bindings.i18n,
                label: this.displayName,
                onClearFilters: ()=>{
                    this.focusTarget.focusAfterSearch();
                    if (this.hasInputRange) return void this.resetRange();
                    this.facet.deselectAll();
                },
                numberOfActiveValues: this.numberOfSelectedValues,
                isCollapsed: this.isCollapsed,
                headingLevel: 0,
                onToggleCollapse: ()=>{
                    this.isCollapsed = !this.isCollapsed;
                },
                headerRef: (el)=>this.focusTarget.setTarget(el)
            }
        });
    }
    renderDateInput() {
        return (0, external_lit_namespaceObject.html)`
      <atomic-facet-date-input
        .label=${this.displayName}
        .inputRange=${this.inputRange}
        .facetId=${this.facetState.facetId}
        @atomic-date-input-apply=${(event)=>{
            const { start, end, endInclusive } = event.detail;
            this.facet.setRanges([
                {
                    start,
                    end,
                    endInclusive,
                    state: 'selected'
                }
            ]);
            this.applyDateInput(event);
        }}
      ></atomic-facet-date-input>
    `;
    }
    render() {
        const { hasError, firstRequestExecuted } = this.summaryState;
        return (0, external_lit_namespaceObject.html)`${(0, when_js_namespaceObject.when)(!hasError && firstRequestExecuted && this.shouldRenderFacet, ()=>(0, facet_container_cjs_namespaceObject.renderFacetContainer)()((0, external_lit_namespaceObject.html)`${this.renderHeader()}
          ${(0, when_js_namespaceObject.when)(!this.isCollapsed, ()=>(0, external_lit_namespaceObject.html)`
              ${(0, when_js_namespaceObject.when)(this.shouldRenderValues, ()=>this.renderValues())}
              ${(0, when_js_namespaceObject.when)(this.shouldRenderInput, ()=>this.renderDateInput())}
            `)}`))}`;
    }
    updated(changedProps) {
        super.updated(changedProps);
        if (changedProps.has('facet')) this.validateFacet();
    }
    validateFacet() {
        if (!this.facet) this.error = new Error('The "facet" property is required for <atomic-commerce-timeframe-facet>.');
    }
    constructor(...args){
        super(...args), this.isCollapsed = false;
    }
}
AtomicCommerceTimeframeFacet.styles = facet_common_tw_css_cjs_default();
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof Summary ? Object : Summary)
], AtomicCommerceTimeframeFacet.prototype, "summary", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof DateFacet ? Object : DateFacet)
], AtomicCommerceTimeframeFacet.prototype, "facet", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        reflect: true,
        converter: boolean_converter_cjs_namespaceObject.booleanConverter,
        attribute: 'is-collapsed'
    })
], AtomicCommerceTimeframeFacet.prototype, "isCollapsed", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicCommerceTimeframeFacet.prototype, "field", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicCommerceTimeframeFacet.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceTimeframeFacet.prototype, "error", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('summary'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", Object)
], AtomicCommerceTimeframeFacet.prototype, "summaryState", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('facet'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof DateFacetState ? Object : DateFacetState)
], AtomicCommerceTimeframeFacet.prototype, "facetState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof DateFilterRange ? Object : DateFilterRange)
], AtomicCommerceTimeframeFacet.prototype, "inputRange", 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)
], AtomicCommerceTimeframeFacet.prototype, "render", null);
AtomicCommerceTimeframeFacet = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-commerce-timeframe-facet'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicCommerceTimeframeFacet);
exports.AtomicCommerceTimeframeFacet = __webpack_exports__.AtomicCommerceTimeframeFacet;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicCommerceTimeframeFacet"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
