"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__, {
    AtomicNumericFacet: ()=>AtomicNumericFacet
});
const bueno_namespaceObject = require("@coveo/bueno");
const headless_namespaceObject = require("@coveo/headless");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const depends_on_cjs_namespaceObject = require("../../common/facets/depends-on.cjs");
const facet_common_cjs_namespaceObject = require("../../common/facets/facet-common.cjs");
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_placeholder_cjs_namespaceObject = require("../../common/facets/facet-placeholder/facet-placeholder.cjs");
const formatter_cjs_namespaceObject = require("../../common/facets/numeric-facet/formatter.cjs");
const value_link_cjs_namespaceObject = require("../../common/facets/numeric-facet/value-link.cjs");
const values_container_cjs_namespaceObject = require("../../common/facets/numeric-facet/values-container.cjs");
const numeric_facet_common_tw_css_cjs_namespaceObject = require("../../common/facets/numeric-facet-common.tw.css.cjs");
var numeric_facet_common_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(numeric_facet_common_tw_css_cjs_namespaceObject);
const popover_type_cjs_namespaceObject = require("../../common/facets/popover/popover-type.cjs");
const format_common_cjs_namespaceObject = require("../../common/formats/format-common.cjs");
const validate_props_controller_cjs_namespaceObject = require("../../common/validate-props-controller/validate-props-controller.cjs");
const array_converter_cjs_namespaceObject = require("../../../converters/array-converter.cjs");
const bind_state_cjs_namespaceObject = require("../../../decorators/bind-state.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 with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const accessibility_utils_cjs_namespaceObject = require("../../../utils/accessibility-utils.cjs");
const props_utils_cjs_namespaceObject = require("../../../utils/props-utils.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
require("../../common/atomic-facet-number-input/atomic-facet-number-input.cjs");
require("../../common/atomic-numeric-range/atomic-numeric-range.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 AtomicNumericFacet extends external_lit_namespaceObject.LitElement {
    initialize() {
        this.validateTabs();
        this.initializeTabManager();
        this.computeFacetId();
        this.initializeFacetForInput();
        this.initializeFacetForRange();
        this.initializeFilter();
        this.initializeSearchStatus();
        this.registerFacetToStore();
        this.registerNumberInputApplyListener();
    }
    connectedCallback() {
        super.connectedCallback();
        this.registerNumberFormatListener();
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        if (!this.isConnected) {
            this.filterDependenciesManager?.stopWatching();
            this.removeNumberFormatListener?.();
            this.removeNumberInputApplyListener?.();
            this.unsubscribeFacetForRange?.();
            this.unsubscribeFacetForInput?.();
            this.unsubscribeFilter?.();
        }
    }
    get focusTarget() {
        if (!this.headerFocus) this.headerFocus = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
        return this.headerFocus;
    }
    get numberOfSelectedValues() {
        if (this.filter?.state.range) return 1;
        return this.facetForRange?.state.values.filter(({ state })=>'selected' === state).length || 0;
    }
    get shouldRenderValues() {
        return !this.hasInputRange && this.numberOfValues > 0 && !!this.valuesToRender.length;
    }
    get hasInputRange() {
        return !!this.filter?.state.range;
    }
    get valuesToRender() {
        return this.facetForRange?.state.values.filter((value)=>value.numberOfResults || 'idle' !== value.state) || [];
    }
    get shouldRenderInput() {
        return (0, facet_common_cjs_namespaceObject.shouldDisplayInputForFacetRange)({
            hasInputRange: this.hasInputRange,
            searchStatusState: this.searchStatusState,
            facetValues: this.facetForInput?.state.values || [],
            hasInput: !!this.withInput
        });
    }
    get shouldRenderFacet() {
        return this.shouldRenderInput || this.shouldRenderValues;
    }
    get enabled() {
        return this.facetState?.enabled ?? this.filter?.state.enabled ?? true;
    }
    get isHidden() {
        return !this.shouldRenderFacet || !this.enabled;
    }
    get hasValues() {
        if (this.facetForInput?.state.values.length) return true;
        return !!this.valuesToRender.length;
    }
    validateTabs() {
        if (this.tabsIncluded.length > 0 && this.tabsExcluded.length > 0) console.warn('Values for both "tabs-included" and "tabs-excluded" have been provided. This could lead to unexpected behaviors.');
    }
    initializeSearchStatus() {
        this.searchStatus = (0, headless_namespaceObject.buildSearchStatus)(this.bindings.engine);
    }
    initializeTabManager() {
        this.tabManager = (0, headless_namespaceObject.buildTabManager)(this.bindings.engine);
    }
    initializeFacetForInput() {
        if (!this.withInput) return;
        this.facetForInput = (0, headless_namespaceObject.buildNumericFacet)(this.bindings.engine, {
            options: {
                numberOfValues: 1,
                generateAutomaticRanges: true,
                facetId: `${this.facetId}_input_range`,
                field: this.field,
                sortCriteria: this.sortCriteria,
                rangeAlgorithm: this.rangeAlgorithm,
                filterFacetCount: this.filterFacetCount,
                injectionDepth: this.injectionDepth,
                tabs: {
                    included: this.tabsIncluded,
                    excluded: this.tabsExcluded
                }
            }
        });
        this.facetForInputState = this.facetForInput.state;
        this.unsubscribeFacetForInput = this.facetForInput.subscribe(()=>{
            this.facetForInputState = this.facetForInput.state;
        });
        return this.facetForInput;
    }
    initializeFacetForRange() {
        if (this.numberOfValues <= 0) return;
        this.manualRanges = Array.from(this.querySelectorAll('atomic-numeric-range')).map((range)=>{
            const { start, end, endInclusive, label } = range;
            return {
                ...(0, headless_namespaceObject.buildNumericRange)({
                    start,
                    end,
                    endInclusive
                }),
                label
            };
        });
        this.facetForRange = (0, headless_namespaceObject.buildNumericFacet)(this.bindings.engine, {
            options: {
                facetId: this.facetId,
                field: this.field,
                numberOfValues: this.numberOfValues,
                sortCriteria: this.sortCriteria,
                rangeAlgorithm: this.rangeAlgorithm,
                currentValues: this.manualRanges,
                generateAutomaticRanges: !this.manualRanges.length,
                filterFacetCount: this.filterFacetCount,
                injectionDepth: this.injectionDepth,
                tabs: {
                    included: this.tabsIncluded,
                    excluded: this.tabsExcluded
                }
            }
        });
        this.facetState = this.facetForRange.state;
        this.unsubscribeFacetForRange = this.facetForRange.subscribe(()=>{
            this.facetState = this.facetForRange.state;
        });
        return this.facetForRange;
    }
    initializeFilter() {
        if (!this.withInput) return;
        this.filter = (0, headless_namespaceObject.buildNumericFilter)(this.bindings.engine, {
            options: {
                facetId: `${this.facetId}_input`,
                field: this.field,
                tabs: {
                    included: this.tabsIncluded,
                    excluded: this.tabsExcluded
                }
            }
        });
        this.filterDependenciesManager = this.initializeDependenciesManager(this.filter.state.facetId);
        this.filterState = this.filter.state;
        this.unsubscribeFilter = this.filter.subscribe(()=>{
            this.filterState = this.filter.state;
        });
    }
    initializeDependenciesManager(facetId) {
        return (0, headless_namespaceObject.buildFacetConditionsManager)(this.bindings.engine, {
            facetId,
            conditions: (0, depends_on_cjs_namespaceObject.parseDependsOn)(this.dependsOn || {})
        });
    }
    registerFacetToStore() {
        const facetInfo = {
            label: ()=>this.bindings.i18n.t(this.label),
            facetId: this.facetId,
            element: this,
            isHidden: ()=>this.isHidden
        };
        this.bindings.store.registerFacet('numericFacets', {
            ...facetInfo,
            format: (value)=>(0, formatter_cjs_namespaceObject.formatHumanReadable)({
                    facetValue: value,
                    logger: this.bindings.engine.logger,
                    i18n: this.bindings.i18n,
                    field: this.field,
                    manualRanges: this.manualRanges,
                    formatter: this.formatter
                })
        });
        (0, popover_type_cjs_namespaceObject.initializePopover)(this, {
            ...facetInfo,
            hasValues: ()=>this.hasValues,
            numberOfActiveValues: ()=>this.numberOfSelectedValues
        });
        if (this.filter) this.bindings.store.state.numericFacets[this.filter.state.facetId] = this.bindings.store.state.numericFacets[this.facetId];
    }
    registerNumberFormatListener() {
        const handleNumberFormat = (event)=>{
            event.preventDefault();
            event.stopPropagation();
            this.formatter = event.detail;
        };
        this.addEventListener('atomic/numberFormat', handleNumberFormat);
        this.removeNumberFormatListener = ()=>{
            this.removeEventListener('atomic/numberFormat', handleNumberFormat);
        };
    }
    registerNumberInputApplyListener() {
        const handleNumberInputApply = ()=>{
            if (this.facetId) this.bindings.engine.dispatch((0, headless_namespaceObject.loadNumericFacetSetActions)(this.bindings.engine).deselectAllNumericFacetValues(this.facetId));
        };
        this.addEventListener('atomic/numberInputApply', handleNumberInputApply);
        this.removeNumberInputApplyListener = ()=>{
            this.removeEventListener('atomic/numberInputApply', handleNumberInputApply);
        };
    }
    computeFacetId() {
        if (this.facetId) return;
        if (this.bindings.store.state.numericFacets[this.field]) {
            this.facetId = (0, utils_cjs_namespaceObject.randomID)(`${this.field}_`);
            return;
        }
        this.facetId = this.field;
    }
    handleClearFilters() {
        this.focusTarget.focusAfterSearch();
        if (this.filterState?.range) return void this.filter?.clear();
        this.facetForRange?.deselectAll();
    }
    handleToggleCollapse() {
        this.isCollapsed = !this.isCollapsed;
    }
    handleValueClick(value) {
        if ('link' === this.displayValuesAs) this.facetForRange.toggleSingleSelect(value);
        else this.facetForRange.toggleSelect(value);
    }
    renderValues() {
        const { displayValuesAs, field, manualRanges, label, bindings: { i18n, engine: { logger } }, formatter } = this;
        return (0, values_container_cjs_namespaceObject.renderNumericFacetValuesGroup)({
            props: {
                i18n,
                label
            }
        })((0, external_lit_namespaceObject.html)`${this.valuesToRender.map((value)=>(0, value_link_cjs_namespaceObject.renderNumericFacetValue)({
                props: {
                    formatter,
                    displayValuesAs,
                    facetValue: value,
                    field,
                    i18n,
                    logger,
                    manualRanges,
                    onClick: ()=>this.handleValueClick(value)
                }
            }))}`);
    }
    renderInput() {
        return (0, external_lit_namespaceObject.html)`<atomic-facet-number-input
      .type=${this.withInput}
      .bindings=${this.bindings}
      .label=${this.label}
      .filter=${this.filter}
      .filterState=${this.filter.state}
    ></atomic-facet-number-input>`;
    }
    render() {
        const { searchStatusState: { firstSearchExecuted, hasError } } = this;
        if (hasError || !this.enabled) return (0, external_lit_namespaceObject.html)``;
        if (!firstSearchExecuted) return (0, external_lit_namespaceObject.html)`${(0, facet_placeholder_cjs_namespaceObject.renderFacetPlaceholder)({
            props: {
                numberOfValues: this.numberOfValues,
                isCollapsed: this.isCollapsed
            }
        })}`;
        return (0, external_lit_namespaceObject.html)`${(0, when_js_namespaceObject.when)(this.shouldRenderFacet, ()=>(0, facet_container_cjs_namespaceObject.renderFacetContainer)()((0, external_lit_namespaceObject.html)`${(0, facet_header_cjs_namespaceObject.renderFacetHeader)({
                props: {
                    i18n: this.bindings.i18n,
                    label: this.label,
                    onClearFilters: ()=>this.handleClearFilters(),
                    numberOfActiveValues: this.numberOfSelectedValues,
                    isCollapsed: this.isCollapsed,
                    headingLevel: this.headingLevel,
                    onToggleCollapse: ()=>this.handleToggleCollapse(),
                    headerRef: (el)=>this.focusTarget.setTarget(el)
                }
            })}
        ${(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.renderInput())}`)}`))}`;
    }
    constructor(){
        super(), this.label = 'no-label', this.tabsIncluded = [], this.tabsExcluded = [], this.numberOfValues = 8, this.sortCriteria = 'ascending', this.rangeAlgorithm = 'equiprobable', this.displayValuesAs = 'checkbox', this.isCollapsed = false, this.headingLevel = 0, this.filterFacetCount = true, this.injectionDepth = 1000, this.manualRanges = [], this.formatter = format_common_cjs_namespaceObject.defaultNumberFormatter;
        new validate_props_controller_cjs_namespaceObject.ValidatePropsController(this, ()=>({
                displayValuesAs: this.displayValuesAs,
                withInput: this.withInput
            }), AtomicNumericFacet.propsSchema);
    }
}
AtomicNumericFacet.propsSchema = new bueno_namespaceObject.Schema({
    displayValuesAs: new bueno_namespaceObject.StringValue({
        constrainTo: [
            'checkbox',
            'link'
        ]
    }),
    withInput: new bueno_namespaceObject.StringValue({
        constrainTo: [
            'integer',
            'decimal'
        ]
    })
});
AtomicNumericFacet.styles = numeric_facet_common_tw_css_cjs_default();
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true,
        attribute: 'facet-id'
    }),
    _ts_metadata("design:type", String)
], AtomicNumericFacet.prototype, "facetId", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    })
], AtomicNumericFacet.prototype, "label", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicNumericFacet.prototype, "field", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Array,
        attribute: 'tabs-included',
        converter: array_converter_cjs_namespaceObject.arrayConverter
    }),
    _ts_metadata("design:type", Array)
], AtomicNumericFacet.prototype, "tabsIncluded", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Array,
        attribute: 'tabs-excluded',
        converter: array_converter_cjs_namespaceObject.arrayConverter
    }),
    _ts_metadata("design:type", Array)
], AtomicNumericFacet.prototype, "tabsExcluded", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        reflect: true,
        attribute: 'number-of-values'
    })
], AtomicNumericFacet.prototype, "numberOfValues", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true,
        attribute: 'with-input'
    }),
    _ts_metadata("design:type", "u" < typeof NumberInputType ? Object : NumberInputType)
], AtomicNumericFacet.prototype, "withInput", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true,
        attribute: 'sort-criteria'
    }),
    _ts_metadata("design:type", "u" < typeof RangeFacetSortCriterion ? Object : RangeFacetSortCriterion)
], AtomicNumericFacet.prototype, "sortCriteria", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true,
        attribute: 'range-algorithm'
    }),
    _ts_metadata("design:type", "u" < typeof RangeFacetRangeAlgorithm ? Object : RangeFacetRangeAlgorithm)
], AtomicNumericFacet.prototype, "rangeAlgorithm", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true,
        attribute: 'display-values-as'
    }),
    _ts_metadata("design:type", String)
], AtomicNumericFacet.prototype, "displayValuesAs", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        reflect: true,
        attribute: 'is-collapsed'
    })
], AtomicNumericFacet.prototype, "isCollapsed", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        reflect: true,
        attribute: 'heading-level'
    })
], AtomicNumericFacet.prototype, "headingLevel", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        reflect: true,
        attribute: 'filter-facet-count'
    })
], AtomicNumericFacet.prototype, "filterFacetCount", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        reflect: true,
        attribute: 'injection-depth'
    })
], AtomicNumericFacet.prototype, "injectionDepth", void 0);
_ts_decorate([
    (0, props_utils_cjs_namespaceObject.mapProperty)({
        attributePrefix: 'depends-on'
    }),
    _ts_metadata("design:type", "u" < typeof Record ? Object : Record)
], AtomicNumericFacet.prototype, "dependsOn", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicNumericFacet.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof NumericFacetState ? Object : NumericFacetState)
], AtomicNumericFacet.prototype, "facetState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof NumericFilterState ? Object : NumericFilterState)
], AtomicNumericFacet.prototype, "filterState", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('searchStatus'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof SearchStatusState ? Object : SearchStatusState)
], AtomicNumericFacet.prototype, "searchStatusState", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('tabManager'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof TabManagerState ? Object : TabManagerState)
], AtomicNumericFacet.prototype, "tabManagerState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicNumericFacet.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof NumericFacetState ? Object : NumericFacetState)
], AtomicNumericFacet.prototype, "facetForInputState", 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)
], AtomicNumericFacet.prototype, "render", null);
AtomicNumericFacet = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-numeric-facet'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicNumericFacet);
exports.AtomicNumericFacet = __webpack_exports__.AtomicNumericFacet;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicNumericFacet"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
