"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__, {
    AtomicCommerceFacet: ()=>AtomicCommerceFacet
});
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_search_input_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search-input.cjs");
const facet_search_input_guard_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search-input-guard.cjs");
const facet_search_matches_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search-matches.cjs");
const facet_show_more_less_cjs_namespaceObject = require("../../common/facets/facet-show-more-less/facet-show-more-less.cjs");
const facet_value_cjs_namespaceObject = require("../../common/facets/facet-value/facet-value.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 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 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);
const facet_search_tw_css_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search.tw.css.cjs");
var facet_search_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(facet_search_tw_css_cjs_namespaceObject);
const facet_search_aria_live_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search-aria-live.cjs");
const facet_search_utils_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search-utils.cjs");
const facet_value_box_tw_css_cjs_namespaceObject = require("../../common/facets/facet-value-box/facet-value-box.tw.css.cjs");
var facet_value_box_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(facet_value_box_tw_css_cjs_namespaceObject);
const facet_value_checkbox_tw_css_cjs_namespaceObject = require("../../common/facets/facet-value-checkbox/facet-value-checkbox.tw.css.cjs");
var facet_value_checkbox_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(facet_value_checkbox_tw_css_cjs_namespaceObject);
const facet_value_exclude_tw_css_cjs_namespaceObject = require("../../common/facets/facet-value-exclude/facet-value-exclude.tw.css.cjs");
var facet_value_exclude_tw_css_cjs_default = /*#__PURE__*/ __webpack_require__.n(facet_value_exclude_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 AtomicCommerceFacet extends external_lit_namespaceObject.LitElement {
    initialize() {
        this.validateFacet();
        this.initFocusTargets();
        this.ensureSubscribed();
        this.facet && this.initAriaLive();
    }
    disconnectedCallback() {
        this.unsubscribeFacetController?.();
        this.unsubscribeFacetController = void 0;
        super.disconnectedCallback();
    }
    shouldUpdate(changedProperties) {
        const prev = changedProperties.get('facetState');
        const next = this.facetState;
        if (this.isFacetState(prev, 'facetState') && this.isFacetState(next, 'facetState')) return (0, facet_search_utils_cjs_namespaceObject.shouldUpdateFacetSearchComponent)(next.facetSearch, prev.facetSearch);
        return true;
    }
    renderHeader() {
        return (0, facet_header_cjs_namespaceObject.renderFacetHeader)({
            props: {
                i18n: this.bindings.i18n,
                label: this.displayName,
                numberOfActiveValues: this.activeValues.length,
                isCollapsed: this.isCollapsed,
                headingLevel: 0,
                onToggleCollapse: ()=>{
                    this.isCollapsed = !this.isCollapsed;
                },
                onClearFilters: ()=>{
                    this.focusTargets.header.focusAfterSearch();
                    this.facet.deselectAll();
                },
                headerRef: (el)=>this.focusTargets.header.setTarget(el)
            }
        });
    }
    renderBody() {
        if (this.isCollapsed) return external_lit_namespaceObject.nothing;
        return (0, external_lit_namespaceObject.html)`
      ${(0, facet_search_input_guard_cjs_namespaceObject.facetSearchInputGuard)({
            canShowMoreValues: this.facetState.canShowMoreValues,
            numberOfDisplayedValues: this.facetState.values.length,
            withSearch: true
        }, ()=>(0, external_lit_namespaceObject.html)`${(0, facet_search_input_cjs_namespaceObject.renderFacetSearchInput)({
                props: {
                    i18n: this.bindings.i18n,
                    label: this.displayName,
                    query: this.facetState.facetSearch.query,
                    onClear: ()=>this.facet.facetSearch.clear(),
                    onChange: (value)=>{
                        if ('' === value) return void this.facet.facetSearch.clear();
                        this.facet.facetSearch.updateText(value);
                        this.facet.facetSearch.search();
                    }
                }
            })}`)}
      ${(0, facet_search_utils_cjs_namespaceObject.shouldDisplaySearchResults)(this.facetState.facetSearch) ? [
            this.renderSearchResults(),
            this.renderMatches()
        ] : [
            this.renderValues(),
            this.renderShowMoreLess()
        ]}
    `;
    }
    renderValuesContainer(children, query) {
        return (0, facet_values_group_cjs_namespaceObject.renderFacetValuesGroup)({
            props: {
                i18n: this.bindings.i18n,
                label: this.displayName,
                query
            }
        })((0, external_lit_namespaceObject.html)`<ul part="values" class="mt-3">
        ${children}
      </ul>`);
    }
    renderSearchResults() {
        return this.renderValuesContainer(this.facetState.facetSearch.values.map((value)=>(0, external_lit_namespaceObject.html)`${(0, facet_value_cjs_namespaceObject.renderFacetValue)({
                props: {
                    ...this.facetValueProps,
                    facetState: 'idle',
                    facetCount: value.count,
                    onExclude: ()=>this.facet.facetSearch.exclude(value),
                    onSelect: ()=>this.facet.facetSearch.select(value),
                    facetValue: value.rawValue
                }
            })}`));
    }
    renderValues() {
        return this.renderValuesContainer(this.facetState.values.map((value, i)=>{
            const shouldFocusAfterInteraction = 0 === i;
            return (0, facet_value_cjs_namespaceObject.renderFacetValue)({
                props: {
                    ...this.facetValueProps,
                    facetCount: value.numberOfResults,
                    onExclude: ()=>this.facet.toggleExclude(value),
                    onSelect: ()=>this.facet.toggleSelect(value),
                    facetValue: value.value,
                    facetState: value.state,
                    setRef: (btn)=>{
                        if (shouldFocusAfterInteraction) this.showLessFocus?.setTarget(btn);
                        if (shouldFocusAfterInteraction) this.showMoreFocus?.setTarget(btn);
                    }
                }
            });
        }));
    }
    renderShowMoreLess() {
        return (0, external_lit_namespaceObject.html)`
      ${(0, facet_show_more_less_cjs_namespaceObject.renderFacetShowMoreLess)({
            props: {
                label: this.displayName,
                i18n: this.bindings.i18n,
                canShowLessValues: this.facetState.canShowLessValues,
                canShowMoreValues: this.facetState.canShowMoreValues,
                onShowLess: ()=>{
                    this.focusTargets.showLess.focusAfterSearch();
                    this.facet.showLessValues();
                },
                onShowMore: ()=>{
                    this.focusTargets.showMore.focusAfterSearch();
                    this.facet.showMoreValues();
                }
            }
        })}
    `;
    }
    renderMatches() {
        return (0, facet_search_matches_cjs_namespaceObject.renderFacetSearchMatches)({
            props: {
                i18n: this.bindings.i18n,
                query: this.facetState.facetSearch.query,
                numberOfMatches: this.facetState.facetSearch.values.length,
                hasMoreMatches: this.facetState.facetSearch.moreValuesAvailable,
                showMoreMatches: ()=>this.facet.facetSearch.showMoreResults()
            }
        });
    }
    get activeValues() {
        return this.facetState.values.filter(({ state })=>'idle' !== state);
    }
    get displayName() {
        return this.facetState.displayName || 'no-label';
    }
    get facetValueProps() {
        return {
            facetSearchQuery: this.facetState.facetSearch.query,
            displayValuesAs: 'checkbox',
            enableExclusion: false,
            field: this.facetState.field,
            i18n: this.bindings.i18n
        };
    }
    initFocusTargets() {
        if (!this.showLessFocus) this.showLessFocus = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
        if (!this.showMoreFocus) this.showMoreFocus = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
        if (!this.headerFocus) this.headerFocus = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
    }
    initAriaLive() {
        (0, facet_search_aria_live_cjs_namespaceObject.announceFacetSearchResultsWithAriaLive)(this.facet, this.displayName, (msg)=>{
            this.ariaLiveRegion.message = msg;
        }, this.bindings.i18n);
    }
    get focusTargets() {
        return {
            showLess: this.showLessFocus,
            showMore: this.showMoreFocus,
            header: this.headerFocus
        };
    }
    isFacetState(state, propName) {
        return 'facetState' === propName && 'string' == typeof state?.facetId;
    }
    ensureSubscribed() {
        if (this.unsubscribeFacetController) return;
        this.unsubscribeFacetController = this.facet?.subscribe(()=>{
            this.facetState = this.facet.state;
        });
    }
    render() {
        const { hasError, firstRequestExecuted } = this.summaryState;
        return (0, external_lit_namespaceObject.html)`
      ${(0, when_js_namespaceObject.when)(!hasError && firstRequestExecuted && this.facetState.values.length > 0, ()=>(0, facet_container_cjs_namespaceObject.renderFacetContainer)()((0, external_lit_namespaceObject.html)`${this.renderHeader()} ${this.renderBody()}`))}
    `;
    }
    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-facet>.');
    }
    constructor(...args){
        super(...args), this.isCollapsed = false, this.ariaLiveRegion = new accessibility_utils_cjs_namespaceObject.AriaLiveRegionController(this, 'facet-search');
    }
}
AtomicCommerceFacet.styles = [
    facet_value_checkbox_tw_css_cjs_default(),
    facet_search_tw_css_cjs_default(),
    facet_common_tw_css_cjs_default(),
    facet_value_exclude_tw_css_cjs_default(),
    facet_value_box_tw_css_cjs_default()
];
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof Summary ? Object : Summary)
], AtomicCommerceFacet.prototype, "summary", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof RegularFacet ? Object : RegularFacet)
], AtomicCommerceFacet.prototype, "facet", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        reflect: true,
        converter: boolean_converter_cjs_namespaceObject.booleanConverter,
        attribute: 'is-collapsed'
    })
], AtomicCommerceFacet.prototype, "isCollapsed", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicCommerceFacet.prototype, "field", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicCommerceFacet.prototype, "bindings", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('summary'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", Object)
], AtomicCommerceFacet.prototype, "summaryState", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('facet'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof RegularFacetState ? Object : RegularFacetState)
], AtomicCommerceFacet.prototype, "facetState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceFacet.prototype, "error", 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)
], AtomicCommerceFacet.prototype, "render", null);
AtomicCommerceFacet = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-commerce-facet'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicCommerceFacet);
exports.AtomicCommerceFacet = __webpack_exports__.AtomicCommerceFacet;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicCommerceFacet"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
