"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__, {
    AtomicCommerceCategoryFacet: ()=>AtomicCommerceCategoryFacet
});
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const map_js_namespaceObject = require("lit/directives/map.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_aria_live_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search-aria-live.cjs");
const facet_search_input_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search-input.cjs");
const facet_search_matches_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search-matches.cjs");
const facet_search_utils_cjs_namespaceObject = require("../../common/facets/facet-search/facet-search-utils.cjs");
const facet_show_more_less_cjs_namespaceObject = require("../../common/facets/facet-show-more-less/facet-show-more-less.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 field_utils_cjs_namespaceObject = require("../../../utils/field-utils.cjs");
const all_categories_button_cjs_namespaceObject = require("../../common/facets/category-facet/all-categories-button.cjs");
const children_as_tree_container_cjs_namespaceObject = require("../../common/facets/category-facet/children-as-tree-container.cjs");
const parent_as_tree_container_cjs_namespaceObject = require("../../common/facets/category-facet/parent-as-tree-container.cjs");
const parent_button_cjs_namespaceObject = require("../../common/facets/category-facet/parent-button.cjs");
const parent_value_link_cjs_namespaceObject = require("../../common/facets/category-facet/parent-value-link.cjs");
const search_results_container_cjs_namespaceObject = require("../../common/facets/category-facet/search-results-container.cjs");
const search_value_cjs_namespaceObject = require("../../common/facets/category-facet/search-value.cjs");
const value_as_tree_container_cjs_namespaceObject = require("../../common/facets/category-facet/value-as-tree-container.cjs");
const value_link_cjs_namespaceObject = require("../../common/facets/category-facet/value-link.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);
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 AtomicCommerceCategoryFacet extends external_lit_namespaceObject.LitElement {
    initialize() {
        this.validateFacet();
        this.facet && (0, facet_search_aria_live_cjs_namespaceObject.announceFacetSearchResultsWithAriaLive)(this.facet, this.displayName, (msg)=>{
            this.facetSearchAriaMessage.message = msg;
        }, this.bindings.i18n);
    }
    get displayName() {
        return this.facetState?.displayName || 'no-label';
    }
    get focusTargets() {
        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);
        if (!this.activeValueFocus) this.activeValueFocus = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
        return {
            showLessFocus: this.showLessFocus,
            showMoreFocus: this.showMoreFocus,
            headerFocus: this.headerFocus,
            activeValueFocus: this.activeValueFocus
        };
    }
    shouldUpdate(changedProperties) {
        if (changedProperties.has('facetState')) {
            const newState = this.facetState;
            const oldState = changedProperties.get('facetState');
            if (newState && oldState) return (0, facet_search_utils_cjs_namespaceObject.shouldUpdateFacetSearchComponent)(newState.facetSearch, oldState.facetSearch);
        }
        return super.shouldUpdate(changedProperties);
    }
    get hasParents() {
        return !!this.facetState.selectedValueAncestry?.length;
    }
    renderHeader() {
        return (0, facet_header_cjs_namespaceObject.renderFacetHeader)({
            props: {
                i18n: this.bindings.i18n,
                label: this.displayName,
                numberOfActiveValues: this.facetState.hasActiveValues && this.isCollapsed ? 1 : 0,
                isCollapsed: this.isCollapsed,
                headingLevel: 0,
                onToggleCollapse: ()=>{
                    this.isCollapsed = !this.isCollapsed;
                },
                onClearFilters: ()=>{
                    this.focusTargets.headerFocus.focusAfterSearch();
                    this.facet.deselectAll();
                },
                headerRef: (header)=>{
                    this.focusTargets.headerFocus.setTarget(header);
                    if (!this.hasParents) this.focusTargets.activeValueFocus.setTarget(header);
                }
            }
        });
    }
    renderValuesTree(parents, isRoot) {
        if (!this.hasParents) return this.renderChildren();
        if (isRoot) return (0, value_as_tree_container_cjs_namespaceObject.renderCategoryFacetTreeValueContainer)()((0, external_lit_namespaceObject.html)`
        ${(0, all_categories_button_cjs_namespaceObject.renderCategoryFacetAllCategoryButton)({
            props: {
                i18n: this.bindings.i18n,
                onClick: ()=>{
                    this.focusTargets.activeValueFocus.focusAfterSearch();
                    this.facet.deselectAll();
                },
                facetId: this.facetState.facetId,
                field: this.facet.state.field
            }
        })}
        ${(0, parent_as_tree_container_cjs_namespaceObject.renderCategoryFacetParentAsTreeContainer)({
            props: {
                isTopLevel: false
            }
        })((0, external_lit_namespaceObject.html)`${this.renderValuesTree(parents, false)} `)}
      `);
        if (parents.length > 1) {
            const parentValue = parents[0];
            return (0, value_as_tree_container_cjs_namespaceObject.renderCategoryFacetTreeValueContainer)()((0, external_lit_namespaceObject.html)`
        ${(0, parent_button_cjs_namespaceObject.renderCategoryFacetParentButton)({
                props: {
                    i18n: this.bindings.i18n,
                    field: this.facetState?.field,
                    facetValue: parentValue,
                    onClick: ()=>{
                        this.focusTargets.activeValueFocus.focusAfterSearch();
                        this.facet.toggleSelect(parentValue);
                    }
                }
            })}
        ${(0, parent_as_tree_container_cjs_namespaceObject.renderCategoryFacetParentAsTreeContainer)({
                props: {
                    isTopLevel: false
                }
            })(this.renderValuesTree(parents.slice(1), false))}
      `);
        }
        const activeParent = parents[0];
        const activeParentDisplayValue = (0, field_utils_cjs_namespaceObject.getFieldValueCaption)(this.facetState?.field, activeParent.value, this.bindings.i18n);
        return (0, external_lit_namespaceObject.html)`
      ${(0, parent_value_link_cjs_namespaceObject.renderCategoryFacetParentValueLink)({
            props: {
                displayValue: activeParentDisplayValue,
                numberOfResults: activeParent.numberOfResults,
                i18n: this.bindings.i18n,
                isLeafValue: activeParent.isLeafValue,
                onClick: ()=>{
                    this.focusTargets.activeValueFocus.focusAfterSearch();
                    this.facet.deselectAll();
                },
                searchQuery: this.facetState.facetSearch.query,
                setRef: (el)=>{
                    this.focusTargets.activeValueFocus.setTarget(el);
                }
            }
        })((0, external_lit_namespaceObject.html)`
        ${(0, children_as_tree_container_cjs_namespaceObject.renderCategoryFacetChildrenAsTreeContainer)({
            props: {}
        })((0, external_lit_namespaceObject.html)` ${this.renderChildren()}`)}
      `)}
    `;
    }
    renderChild(facetValue, isShowLessFocusTarget, isShowMoreFocusTarget) {
        const displayValue = (0, field_utils_cjs_namespaceObject.getFieldValueCaption)(this.facetState?.field, facetValue.value, this.bindings.i18n);
        const isSelected = 'selected' === facetValue.state;
        return (0, external_lit_namespaceObject.html)`${(0, value_link_cjs_namespaceObject.renderCategoryFacetValueLink)({
            props: {
                displayValue,
                numberOfResults: facetValue.numberOfResults,
                i18n: this.bindings.i18n,
                onClick: ()=>{
                    this.focusTargets.activeValueFocus.focusAfterSearch();
                    this.facet.toggleSelect(facetValue);
                },
                isParent: false,
                isSelected,
                searchQuery: this.facetState.facetSearch.query,
                isLeafValue: facetValue.isLeafValue,
                setRef: (el)=>{
                    isShowLessFocusTarget && this.focusTargets.showLessFocus.setTarget(el);
                    isShowMoreFocusTarget && this.focusTargets.showMoreFocus.setTarget(el);
                }
            }
        })()} `;
    }
    renderChildren() {
        const { values, selectedValueAncestry, hasActiveValues } = this.facetState;
        const nextValues = hasActiveValues ? selectedValueAncestry : values;
        const children = nextValues?.length ? hasActiveValues ? nextValues[nextValues.length - 1].children : nextValues : [];
        return (0, external_lit_namespaceObject.html)`${(0, map_js_namespaceObject.map)(children, (value, i)=>this.renderChild(value, 0 === i, i === this.resultIndexToFocusOnShowMore))}`;
    }
    renderSearchResults() {
        return (0, search_results_container_cjs_namespaceObject.renderCategoryFacetSearchResultsContainer)()((0, external_lit_namespaceObject.html)`${(0, map_js_namespaceObject.map)(this.facetState.facetSearch.values, (value)=>(0, search_value_cjs_namespaceObject.renderCategoryFacetSearchValue)({
                props: {
                    value,
                    field: this.facetState.field,
                    facetId: this.facetState.facetId,
                    searchQuery: this.facetState.facetSearch.query,
                    i18n: this.bindings.i18n,
                    onClick: ()=>{
                        this.focusTargets.activeValueFocus.focusAfterSearch();
                        this.facet.facetSearch.select(value);
                    }
                }
            }))}`);
    }
    renderShowMoreLess() {
        return (0, external_lit_namespaceObject.html)`
      <div class=${this.hasParents ? 'pl-9' : ''}>
        ${(0, facet_show_more_less_cjs_namespaceObject.renderFacetShowMoreLess)({
            props: {
                label: this.displayName,
                i18n: this.bindings.i18n,
                onShowMore: ()=>{
                    this.resultIndexToFocusOnShowMore = this.facetState.values.length;
                    this.focusTargets.showMoreFocus.focusAfterSearch();
                    this.facet.showMoreValues();
                },
                onShowLess: ()=>{
                    this.focusTargets.showLessFocus.focusAfterSearch();
                    this.facet.showLessValues();
                },
                canShowLessValues: this.facetState.canShowLessValues,
                canShowMoreValues: this.facetState.canShowMoreValues
            }
        })}
      </div>
    `;
    }
    render() {
        const { bindings: { i18n }, facetState: { facetSearch, selectedValueAncestry, values } } = this;
        const { hasError, firstRequestExecuted } = this.summaryState;
        if (!firstRequestExecuted || hasError || 0 === values.length) return external_lit_namespaceObject.nothing;
        return (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, facet_search_input_cjs_namespaceObject.renderFacetSearchInput)({
                props: {
                    i18n: this.bindings.i18n,
                    label: this.displayName,
                    query: this.facetState.facetSearch.query,
                    onChange: (value)=>{
                        if ('' === value) return void this.facet.facetSearch.clear();
                        this.facet.facetSearch.updateText(value);
                        this.facet.facetSearch.search();
                    },
                    onClear: ()=>this.facet.facetSearch.clear()
                }
            })}
          ${(0, when_js_namespaceObject.when)((0, facet_search_utils_cjs_namespaceObject.shouldDisplaySearchResults)(facetSearch), ()=>(0, external_lit_namespaceObject.html)`
              ${(0, when_js_namespaceObject.when)(facetSearch.values.length > 0, ()=>(0, facet_values_group_cjs_namespaceObject.renderFacetValuesGroup)({
                        props: {
                            i18n,
                            label: this.displayName,
                            query: facetSearch.query
                        }
                    })(this.renderSearchResults()), ()=>(0, external_lit_namespaceObject.html)`<div class="mt-3"></div>`)}
              ${(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()
                    }
                })}
            `, ()=>(0, external_lit_namespaceObject.html)`
              ${(0, facet_values_group_cjs_namespaceObject.renderFacetValuesGroup)({
                    props: {
                        i18n,
                        label: this.displayName
                    }
                })((0, when_js_namespaceObject.when)(this.hasParents, ()=>(0, external_lit_namespaceObject.html)`
                    ${(0, parent_as_tree_container_cjs_namespaceObject.renderCategoryFacetParentAsTreeContainer)({
                        props: {
                            isTopLevel: true,
                            className: 'mt-3'
                        }
                    })((0, external_lit_namespaceObject.html)`${(0, when_js_namespaceObject.when)(selectedValueAncestry, ()=>selectedValueAncestry && this.renderValuesTree(selectedValueAncestry, true))}`)}
                  `, ()=>(0, external_lit_namespaceObject.html)`
                    ${(0, children_as_tree_container_cjs_namespaceObject.renderCategoryFacetChildrenAsTreeContainer)({
                        props: {
                            className: 'mt-3'
                        }
                    })(this.renderChildren())}
                  `))}
              ${this.renderShowMoreLess()}
            `)}
        `)}
    `);
    }
    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-category-facet>.');
    }
    constructor(...args){
        super(...args), this.isCollapsed = false, this.resultIndexToFocusOnShowMore = 0, this.facetSearchAriaMessage = new accessibility_utils_cjs_namespaceObject.AriaLiveRegionController(this, 'facet-search');
    }
}
AtomicCommerceCategoryFacet.styles = [
    facet_common_tw_css_cjs_default(),
    facet_search_tw_css_cjs_default(),
    (0, external_lit_namespaceObject.css)`
      @reference '../../../utils/tailwind.global.tw.css';
      [part~='active-parent'] {
        @apply pl-9;
      }

      [part~='parents'] [part~='values'] {
        @apply pl-9;
      }

      [part~='all-categories-button'],
      [part~='parent-button'] {
        @apply relative flex w-full items-center py-2.5 pr-2 pl-7 text-left;
      }

      [part~='back-arrow'] {
        @apply absolute left-1 h-5 w-5;
      }
    `
];
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicCommerceCategoryFacet.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof Summary ? Object : Summary)
], AtomicCommerceCategoryFacet.prototype, "summary", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Object
    }),
    _ts_metadata("design:type", "u" < typeof CategoryFacet ? Object : CategoryFacet)
], AtomicCommerceCategoryFacet.prototype, "facet", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true,
        type: Boolean,
        attribute: 'is-collapsed',
        converter: boolean_converter_cjs_namespaceObject.booleanConverter
    })
], AtomicCommerceCategoryFacet.prototype, "isCollapsed", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicCommerceCategoryFacet.prototype, "field", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    (0, bind_state_cjs_namespaceObject.bindStateToController)('facet'),
    _ts_metadata("design:type", "u" < typeof CategoryFacetState ? Object : CategoryFacetState)
], AtomicCommerceCategoryFacet.prototype, "facetState", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('summary'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", Object)
], AtomicCommerceCategoryFacet.prototype, "summaryState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceCategoryFacet.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)
], AtomicCommerceCategoryFacet.prototype, "render", null);
AtomicCommerceCategoryFacet = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-commerce-category-facet'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicCommerceCategoryFacet);
exports.AtomicCommerceCategoryFacet = __webpack_exports__.AtomicCommerceCategoryFacet;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicCommerceCategoryFacet"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
