"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__, {
    TimeframeFacetCommon: ()=>TimeframeFacetCommon
});
const external_lit_namespaceObject = require("lit");
const date_utils_cjs_namespaceObject = require("../../../utils/date-utils.cjs");
const field_utils_cjs_namespaceObject = require("../../../utils/field-utils.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
const external_facet_common_cjs_namespaceObject = require("./facet-common.cjs");
const facet_container_cjs_namespaceObject = require("./facet-container/facet-container.cjs");
const facet_header_cjs_namespaceObject = require("./facet-header/facet-header.cjs");
const facet_placeholder_cjs_namespaceObject = require("./facet-placeholder/facet-placeholder.cjs");
const facet_value_label_highlight_cjs_namespaceObject = require("./facet-value-label-highlight/facet-value-label-highlight.cjs");
const facet_value_link_cjs_namespaceObject = require("./facet-value-link/facet-value-link.cjs");
const facet_values_group_cjs_namespaceObject = require("./facet-values-group/facet-values-group.cjs");
const popover_type_cjs_namespaceObject = require("./popover/popover-type.cjs");
require("../atomic-facet-date-input/atomic-facet-date-input.cjs");
require("../atomic-timeframe/atomic-timeframe.cjs");
class TimeframeFacetCommon {
    get determineFacetId() {
        if (this.props.facetId) return this.props.facetId;
        if (this.props.bindings.store.state.dateFacets[this.props.field]) return (0, utils_cjs_namespaceObject.randomID)(`${this.props.field}_`);
        return this.props.field;
    }
    get enabled() {
        return this.facetForDateRange?.state.enabled ?? this.filter?.state.enabled ?? true;
    }
    get valuesToRender() {
        return this.facetForDateRange?.state.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() {
        return (0, external_facet_common_cjs_namespaceObject.shouldDisplayInputForFacetRange)({
            hasInput: this.props.withDatePicker,
            hasInputRange: this.hasInputRange,
            searchStatusState: this.props.getSearchStatusState(),
            facetValues: this.facetForDatePicker?.state?.values || []
        });
    }
    get hasValues() {
        if (this.facetForDatePicker?.state.values.length) return true;
        return !!this.valuesToRender.length;
    }
    get numberOfSelectedValues() {
        if (this.filter?.state?.range) return 1;
        return this.facetForDateRange?.state.values.filter(({ state })=>'selected' === state).length || 0;
    }
    get hasInputRange() {
        return !!this.filter?.state.range;
    }
    get currentValues() {
        return this.manualTimeframes.map(({ period, amount, unit })=>'past' === period ? this.props.buildDateRange({
                start: {
                    period,
                    unit,
                    amount
                },
                end: {
                    period: 'now'
                }
            }) : this.props.buildDateRange({
                start: {
                    period: 'now'
                },
                end: {
                    period,
                    unit,
                    amount
                }
            }));
    }
    disconnectedCallback() {
        if (this.props.host.isConnected) return;
        this.facetForDateRangeDependenciesManager?.stopWatching();
        this.facetForDatePickerDependenciesManager?.stopWatching();
        this.filterDependenciesManager?.stopWatching();
    }
    get isHidden() {
        return !this.shouldRenderFacet || !this.enabled;
    }
    registerFacetToStore() {
        const facetInfo = {
            label: ()=>this.props.bindings.i18n.t(this.props.label),
            facetId: this.facetId,
            element: this.props.host,
            isHidden: ()=>this.isHidden
        };
        this.props.bindings.store.registerFacet('dateFacets', {
            ...facetInfo,
            format: (value)=>this.formatFacetValue(value)
        });
        (0, popover_type_cjs_namespaceObject.initializePopover)(this.props.host, {
            ...facetInfo,
            hasValues: ()=>this.hasValues,
            numberOfActiveValues: ()=>this.numberOfSelectedValues
        });
        if (this.filter) this.props.bindings.store.state.dateFacets[this.filter.state.facetId] = this.props.bindings.store.state.dateFacets[this.facetId];
    }
    getManualTimeframes() {
        return Array.from(this.props.host.querySelectorAll('atomic-timeframe')).map(({ label, amount, unit, period })=>({
                label,
                amount,
                unit,
                period
            }));
    }
    formatFacetValue(facetValue) {
        try {
            const startDate = this.props.deserializeRelativeDate(facetValue.start);
            const relativeDate = 'past' === startDate.period ? startDate : this.props.deserializeRelativeDate(facetValue.end);
            const timeframe = this.getManualTimeframes().find((timeframe)=>timeframe.period === relativeDate.period && timeframe.unit === relativeDate.unit && timeframe.amount === relativeDate.amount);
            if (timeframe?.label) return (0, field_utils_cjs_namespaceObject.getFieldValueCaption)(this.props.field, timeframe.label, this.props.bindings.i18n);
            return this.props.bindings.i18n.t(`${relativeDate.period}-${relativeDate.unit}`, {
                count: relativeDate.amount
            });
        } catch (_error) {
            return this.props.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.props.bindings.i18n,
                onClick: ()=>this.facetForDateRange.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.props.bindings.i18n,
                label: this.props.label
            }
        })((0, external_lit_namespaceObject.html)`<ul class="mt-3" part="values">
      ${children}
    </ul>`);
    }
    renderHeader(isCollapsed, headerFocus, onToggleCollapse) {
        return (0, facet_header_cjs_namespaceObject.renderFacetHeader)({
            props: {
                i18n: this.props.bindings.i18n,
                label: this.props.label,
                onClearFilters: ()=>{
                    headerFocus.focusAfterSearch();
                    if (this.filter?.state.range) return void this.filter?.clear();
                    this.facetForDateRange?.deselectAll();
                },
                numberOfActiveValues: this.numberOfSelectedValues,
                isCollapsed,
                headingLevel: this.props.headingLevel,
                onToggleCollapse,
                headerRef: (el)=>headerFocus.setTarget(el)
            }
        });
    }
    renderDateInput() {
        return (0, external_lit_namespaceObject.html)`<atomic-facet-date-input
      .min=${this.props.min}
      .max=${this.props.max}
      .label=${this.props.label}
      .facetId=${this.filter.state.facetId}
      .inputRange=${this.filter.state.range}
      @atomic-date-input-apply=${(event)=>{
            this.filter.setRange({
                start: event.detail.start,
                end: event.detail.end
            });
        }}
    ></atomic-facet-date-input>`;
    }
    render({ hasError, firstSearchExecuted, isCollapsed, headerFocus, onToggleCollapse }) {
        if (hasError || !this.enabled) return external_lit_namespaceObject.nothing;
        if (!firstSearchExecuted) return (0, facet_placeholder_cjs_namespaceObject.renderFacetPlaceholder)({
            props: {
                numberOfValues: this.currentValues.length,
                isCollapsed
            }
        });
        if (!this.shouldRenderFacet) return external_lit_namespaceObject.nothing;
        return (0, facet_container_cjs_namespaceObject.renderFacetContainer)()((0, external_lit_namespaceObject.html)`${this.renderHeader(isCollapsed, headerFocus, onToggleCollapse)}
      ${!isCollapsed ? (0, external_lit_namespaceObject.html)`${this.shouldRenderValues ? this.renderValues() : external_lit_namespaceObject.nothing}
          ${this.shouldRenderInput ? this.renderDateInput() : external_lit_namespaceObject.nothing}` : external_lit_namespaceObject.nothing}`);
    }
    constructor(props){
        this.props = props;
        this.manualTimeframes = [];
        this.facetId = this.determineFacetId;
        this.props.setFacetId(this.facetId);
        this.manualTimeframes = this.getManualTimeframes();
        if (this.manualTimeframes.length > 0) this.facetForDateRange = this.props.initializeFacetForDateRange(this.currentValues);
        if (this.props.withDatePicker) {
            this.facetForDatePicker = this.props.initializeFacetForDatePicker();
            this.facetForDatePickerDependenciesManager = this.props.buildDependenciesManager(this.facetForDatePicker.state.facetId);
            this.filter = this.props.initializeFilter();
        }
        if (this.facetForDateRange) this.facetForDateRangeDependenciesManager = this.props.buildDependenciesManager(this.facetForDateRange?.state.facetId);
        if (this.filter) this.filterDependenciesManager = this.props.buildDependenciesManager(this.filter?.state.facetId);
        this.registerFacetToStore();
    }
}
exports.TimeframeFacetCommon = __webpack_exports__.TimeframeFacetCommon;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "TimeframeFacetCommon"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
