"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__, {
    AtomicCommerceBreadbox: ()=>AtomicCommerceBreadbox
});
const bueno_namespaceObject = require("@coveo/bueno");
const commerce_namespaceObject = require("@coveo/headless/commerce");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const breadcrumb_button_cjs_namespaceObject = require("../../common/breadbox/breadcrumb-button.cjs");
const breadcrumb_clear_all_cjs_namespaceObject = require("../../common/breadbox/breadcrumb-clear-all.cjs");
const breadcrumb_container_cjs_namespaceObject = require("../../common/breadbox/breadcrumb-container.cjs");
const breadcrumb_content_cjs_namespaceObject = require("../../common/breadbox/breadcrumb-content.cjs");
const breadcrumb_show_less_cjs_namespaceObject = require("../../common/breadbox/breadcrumb-show-less.cjs");
const breadcrumb_show_more_cjs_namespaceObject = require("../../common/breadbox/breadcrumb-show-more.cjs");
const formatter_cjs_namespaceObject = require("../../common/facets/numeric-facet/formatter.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 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 date_utils_cjs_namespaceObject = require("../../../utils/date-utils.cjs");
const field_utils_cjs_namespaceObject = require("../../../utils/field-utils.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 AtomicCommerceBreadbox extends external_lit_namespaceObject.LitElement {
    initialize() {
        if ('product-listing' === this.bindings.interfaceElement.type) this.searchOrListing = (0, commerce_namespaceObject.buildProductListing)(this.bindings.engine);
        else this.searchOrListing = (0, commerce_namespaceObject.buildSearch)(this.bindings.engine);
        this.context = (0, commerce_namespaceObject.buildContext)(this.bindings.engine);
        this.breadcrumbManager = this.searchOrListing.breadcrumbManager();
        if (this.disableCollapse) this.isCollapsed = false;
        else if (window.ResizeObserver && this.parentElement) {
            this.resizeObserver = new ResizeObserver(()=>this.adaptBreadcrumbs());
            this.resizeObserver.observe(this.parentElement);
        }
        this.breadcrumbRemovedFocus = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
        this.breadcrumbShowMoreFocus = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
        this.breadcrumbShowLessFocus = new accessibility_utils_cjs_namespaceObject.FocusTargetController(this, this.bindings);
    }
    updated() {
        this.adaptBreadcrumbs();
    }
    disconnectedCallback() {
        this.resizeObserver?.disconnect();
    }
    get breadcrumbs() {
        return Array.from(this.shadowRoot.querySelectorAll('li.breadcrumb'));
    }
    get showMoreButton() {
        return this.shadowRoot.querySelector('button[part="show-more"]');
    }
    get showLessButton() {
        return this.shadowRoot.querySelector('button[part="show-less"]');
    }
    hide(element) {
        element.style.display = 'none';
    }
    show(element) {
        element.style.display = '';
    }
    showAllBreadcrumbs() {
        this.breadcrumbs.forEach((breadcrumb)=>this.show(breadcrumb));
    }
    adaptBreadcrumbs() {
        if (this.disableCollapse || !this.breadcrumbs.length) return;
        this.showAllBreadcrumbs();
        if (!this.isCollapsed) return void this.updateShowLessDisplay();
        this.hideOverflowingBreadcrumbs();
    }
    updateShowLessDisplay() {
        this.show(this.showLessButton);
        if (0 === this.showLessButton.offsetTop) this.hide(this.showLessButton);
    }
    hideOverflowingBreadcrumbs() {
        let hiddenBreadcrumbs = 0;
        for(let i = this.breadcrumbs.length - 1; this.isOverflowing && i >= 0; i--){
            this.hide(this.breadcrumbs[i]);
            hiddenBreadcrumbs++;
        }
        this.updateShowMoreValue(hiddenBreadcrumbs);
    }
    get isOverflowing() {
        const listElement = this.shadowRoot.querySelector('ul');
        if (!listElement) return false;
        return listElement.scrollWidth > listElement.clientWidth;
    }
    updateShowMoreValue(value) {
        this.numberOfCollapsedBreadcrumbs = value;
        if (0 === value) return void this.hide(this.showMoreButton);
        this.show(this.showMoreButton);
        this.showMoreText = `+ ${value.toLocaleString(this.bindings.i18n.language)}`;
    }
    getNumberFormatter(field) {
        if ('ec_price' === field || 'ec_promo_price' === field) return (0, format_common_cjs_namespaceObject.defaultCurrencyFormatter)(this.contextState.currency);
        return format_common_cjs_namespaceObject.defaultNumberFormatter;
    }
    buildBreadcrumb(breadcrumb) {
        return breadcrumb.values.map((value)=>({
                facetId: breadcrumb.facetId,
                label: breadcrumb.facetDisplayName ?? this.bindings.i18n.t('no-label'),
                deselect: value.deselect,
                formattedValue: this.valueForFacetType(breadcrumb.type, breadcrumb.facetId, value)
            }));
    }
    renderBreadcrumbs(breadcrumbs) {
        this.numberOfBreadcrumbs = breadcrumbs.length;
        return breadcrumbs.map((breadcrumb, index)=>{
            const isLastBreadcrumb = 1 === breadcrumbs.length;
            return (0, external_lit_namespaceObject.html)`${(0, breadcrumb_button_cjs_namespaceObject.renderBreadcrumbButton)({
                props: {
                    pathLimit: this.pathLimit,
                    breadcrumb,
                    i18n: this.bindings.i18n,
                    onSelectBreadcrumb: ()=>{
                        if (isLastBreadcrumb) this.bindings.store.state.resultList?.focusOnFirstResultAfterNextSearch();
                        else if (this.numberOfBreadcrumbs > 1) this.breadcrumbRemovedFocus.focusAfterSearch();
                        this.lastRemovedBreadcrumbIndex = index;
                        breadcrumb.deselect();
                    },
                    refCallback: async (ref)=>{
                        if (this.lastRemovedBreadcrumbIndex === index) await this.breadcrumbRemovedFocus.setTarget(ref);
                        if (this.firstExpandedBreadcrumbIndex === index) await this.breadcrumbShowMoreFocus.setTarget(ref);
                    },
                    ariaController: this.breadboxAriaMessage
                }
            })((0, external_lit_namespaceObject.html)`${(0, breadcrumb_content_cjs_namespaceObject.renderBreadcrumbContent)({
                props: {
                    pathLimit: this.pathLimit,
                    isCollapsed: this.isCollapsed,
                    i18n: this.bindings.i18n,
                    breadcrumb
                }
            })}`)}`;
        });
    }
    render() {
        const breadcrumbs = this.breadcrumbManagerState.facetBreadcrumbs.flatMap((breadcrumb)=>this.buildBreadcrumb(breadcrumb));
        if (!breadcrumbs.length) return external_lit_namespaceObject.nothing;
        return (0, external_lit_namespaceObject.html)`${(0, breadcrumb_container_cjs_namespaceObject.renderBreadcrumbContainer)({
            props: {
                isCollapsed: this.isCollapsed,
                i18n: this.bindings.i18n
            }
        })((0, external_lit_namespaceObject.html)`${this.renderBreadcrumbs(breadcrumbs)}
      ${this.disableCollapse ? external_lit_namespaceObject.nothing : (0, external_lit_namespaceObject.html)`${(0, breadcrumb_show_more_cjs_namespaceObject.renderBreadcrumbShowMore)({
            props: {
                refCallback: async (el)=>{
                    await this.breadcrumbShowLessFocus.setTarget(el);
                },
                onShowMore: ()=>{
                    this.firstExpandedBreadcrumbIndex = this.numberOfBreadcrumbs - this.numberOfCollapsedBreadcrumbs;
                    this.breadcrumbShowMoreFocus.focusOnNextTarget();
                    this.isCollapsed = false;
                },
                isCollapsed: this.isCollapsed,
                i18n: this.bindings.i18n,
                numberOfCollapsedBreadcrumbs: this.numberOfCollapsedBreadcrumbs,
                value: this.showMoreText,
                ariaLabel: this.bindings.i18n.t('show-n-more-filters', {
                    value: this.numberOfCollapsedBreadcrumbs
                })
            }
        })}
          ${(0, breadcrumb_show_less_cjs_namespaceObject.renderBreadcrumbShowLess)({
            props: {
                onShowLess: ()=>{
                    this.breadcrumbShowLessFocus.focusOnNextTarget();
                    this.isCollapsed = true;
                },
                isCollapsed: this.isCollapsed,
                i18n: this.bindings.i18n
            }
        })}`}
      ${(0, breadcrumb_clear_all_cjs_namespaceObject.renderBreadcrumbClearAll)({
            props: {
                refCallback: async (ref)=>{
                    const isFocusTarget = this.lastRemovedBreadcrumbIndex === this.numberOfBreadcrumbs;
                    if (isFocusTarget) await this.breadcrumbRemovedFocus.setTarget(ref);
                },
                onClick: ()=>{
                    this.breadcrumbManager.deselectAll();
                    this.bindings.store.state.resultList?.focusOnFirstResultAfterNextSearch();
                },
                isCollapsed: this.isCollapsed,
                i18n: this.bindings.i18n
            }
        })} `)}`;
    }
    constructor(){
        super(), this.lastRemovedBreadcrumbIndex = 0, this.numberOfBreadcrumbs = 0, this.numberOfCollapsedBreadcrumbs = 0, this.isCollapsed = true, this.showMoreText = '', this.disableCollapse = false, this.breadboxAriaMessage = new accessibility_utils_cjs_namespaceObject.AriaLiveRegionController(this, 'breadbox', true), this.pathLimit = 3, this.valueForFacetType = (type, field, value)=>{
            switch(type){
                case 'numericalRange':
                    return [
                        (0, formatter_cjs_namespaceObject.formatHumanReadable)({
                            facetValue: value.value,
                            logger: this.bindings.engine.logger,
                            i18n: this.bindings.i18n,
                            field: field,
                            manualRanges: [],
                            formatter: this.getNumberFormatter(field)
                        })
                    ];
                case 'dateRange':
                    return [
                        this.bindings.i18n.t('to', {
                            start: (0, date_utils_cjs_namespaceObject.parseDate)(value.value.start).format('YYYY-MM-DD'),
                            end: (0, date_utils_cjs_namespaceObject.parseDate)(value.value.end).format('YYYY-MM-DD')
                        })
                    ];
                case 'hierarchical':
                    return value.value.path.map((pathValue)=>(0, field_utils_cjs_namespaceObject.getFieldValueCaption)(field, pathValue, this.bindings.i18n));
                case 'regular':
                    return [
                        (0, field_utils_cjs_namespaceObject.getFieldValueCaption)(field, value.value.value, this.bindings.i18n)
                    ];
                default:
                    this.bindings.engine.logger.warn('Unexpected breadcrumb type.');
                    return [];
            }
        };
        new validate_props_controller_cjs_namespaceObject.ValidatePropsController(this, ()=>({
                pathLimit: this.pathLimit
            }), AtomicCommerceBreadbox.propsSchema);
    }
}
AtomicCommerceBreadbox.styles = [
    (0, external_lit_namespaceObject.css)`
      @reference '../../../utils/tailwind.global.tw.css';

      [part='breadcrumb-label'].excluded,
      [part='breadcrumb-value'].excluded {
        text-decoration: line-through;
        @apply text-error;
      }

      /* Prepend non-breaking space to all breadcrumb values for consistent spacing */
      [part='breadcrumb-value']::before {
        content: '\00a0';
      }
    `
];
AtomicCommerceBreadbox.propsSchema = new bueno_namespaceObject.Schema({
    pathLimit: new bueno_namespaceObject.NumberValue({
        default: 3,
        min: 1,
        required: false
    })
});
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('context'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof ContextState ? Object : ContextState)
], AtomicCommerceBreadbox.prototype, "contextState", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('breadcrumbManager'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof BreadcrumbManagerState ? Object : BreadcrumbManagerState)
], AtomicCommerceBreadbox.prototype, "breadcrumbManagerState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceBreadbox.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicCommerceBreadbox.prototype, "isCollapsed", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicCommerceBreadbox.prototype, "showMoreText", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        attribute: 'disable-collapse'
    })
], AtomicCommerceBreadbox.prototype, "disableCollapse", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        attribute: 'path-limit'
    })
], AtomicCommerceBreadbox.prototype, "pathLimit", 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)
], AtomicCommerceBreadbox.prototype, "render", null);
AtomicCommerceBreadbox = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-commerce-breadbox'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicCommerceBreadbox);
exports.AtomicCommerceBreadbox = __webpack_exports__.AtomicCommerceBreadbox;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicCommerceBreadbox"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
