"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__, {
    AtomicBreadbox: ()=>AtomicBreadbox
});
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 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 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 field_utils_cjs_namespaceObject = require("../../../utils/field-utils.cjs");
const hidden_state_controller_cjs_namespaceObject = require("../../../utils/hidden-state-controller.cjs");
function _check_private_redeclaration(obj, privateCollection) {
    if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object");
}
function _class_apply_descriptor_get(receiver, descriptor) {
    if (descriptor.get) return descriptor.get.call(receiver);
    return descriptor.value;
}
function _class_apply_descriptor_set(receiver, descriptor, value) {
    if (descriptor.set) descriptor.set.call(receiver, value);
    else {
        if (!descriptor.writable) throw new TypeError("attempted to set read only private field");
        descriptor.value = value;
    }
}
function _class_extract_field_descriptor(receiver, privateMap, action) {
    if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance");
    return privateMap.get(receiver);
}
function _class_private_field_get(receiver, privateMap) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
    return _class_apply_descriptor_get(receiver, descriptor);
}
function _class_private_field_init(obj, privateMap, value) {
    _check_private_redeclaration(obj, privateMap);
    privateMap.set(obj, value);
}
function _class_private_field_set(receiver, privateMap, value) {
    var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
    _class_apply_descriptor_set(receiver, descriptor, value);
    return value;
}
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);
}
var _hiddenState = /*#__PURE__*/ new WeakMap();
class AtomicBreadbox extends external_lit_namespaceObject.LitElement {
    initialize() {
        this.breadcrumbManager = (0, headless_namespaceObject.buildBreadcrumbManager)(this.bindings.engine);
        this.facetManager = (0, headless_namespaceObject.buildFacetManager)(this.bindings.engine);
        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();
    }
    render() {
        const breadcrumbs = this.allBreadcrumbs;
        _class_private_field_get(this, _hiddenState).isEmpty = !breadcrumbs.length;
        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
            }
        })} `)}`;
    }
    disconnectedCallback() {
        super.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)}`;
    }
    get facetBreadcrumbs() {
        return this.breadcrumbManagerState.facetBreadcrumbs.flatMap(({ facetId, field, values })=>values.map((value)=>({
                    value,
                    facetId,
                    field
                }))).filter(({ facetId })=>this.bindings.store.state.facets[facetId]).map(({ value, facetId, field })=>({
                facetId,
                label: this.bindings.store.state.facets[facetId]?.label(),
                state: value.value.state,
                deselect: value.deselect,
                formattedValue: [
                    (0, field_utils_cjs_namespaceObject.getFieldValueCaption)(field, value.value.value, this.bindings.i18n)
                ]
            }));
    }
    get categoryFacetBreadcrumbs() {
        return this.breadcrumbManagerState.categoryFacetBreadcrumbs.map(({ facetId, field, path, deselect })=>({
                facetId,
                label: this.bindings.store.state.categoryFacets[facetId].label(),
                deselect: deselect,
                formattedValue: path.map((pathValue)=>(0, field_utils_cjs_namespaceObject.getFieldValueCaption)(field, pathValue.value, this.bindings.i18n))
            }));
    }
    get numericFacetBreadcrumbs() {
        return this.breadcrumbManagerState.numericFacetBreadcrumbs.flatMap(({ facetId, field, values })=>values.map((value)=>({
                    value,
                    facetId,
                    field
                }))).map(({ value, facetId })=>({
                facetId,
                label: this.bindings.store.state.numericFacets[facetId].label(),
                state: value.value.state,
                deselect: value.deselect,
                formattedValue: [
                    this.bindings.store.state.numericFacets[facetId].format(value.value)
                ],
                content: this.bindings.store.state.numericFacets[facetId].content?.(value.value)
            }));
    }
    get dateFacetBreadcrumbs() {
        return this.breadcrumbManagerState.dateFacetBreadcrumbs.flatMap(({ facetId, field, values })=>values.map((value)=>({
                    value,
                    facetId,
                    field
                }))).map(({ value, facetId })=>({
                facetId,
                label: this.bindings.store.state.dateFacets[facetId].label(),
                state: value.value.state,
                deselect: value.deselect,
                formattedValue: [
                    this.bindings.store.state.dateFacets[facetId].format(value.value)
                ]
            }));
    }
    get automaticFacetBreadcrumbs() {
        return this.breadcrumbManagerState.automaticFacetBreadcrumbs.flatMap(({ facetId, field, label, values })=>values.map((value)=>({
                    value,
                    facetId,
                    field,
                    label
                }))).map(({ value, facetId, field, label })=>({
                facetId,
                state: value.value.state,
                label: label ? label : field,
                deselect: value.deselect,
                formattedValue: [
                    (0, field_utils_cjs_namespaceObject.getFieldValueCaption)(field, value.value.value, this.bindings.i18n)
                ]
            }));
    }
    get allBreadcrumbs() {
        return [
            ...this.facetBreadcrumbs,
            ...this.categoryFacetBreadcrumbs,
            ...this.numericFacetBreadcrumbs,
            ...this.dateFacetBreadcrumbs,
            ...this.automaticFacetBreadcrumbs
        ];
    }
    renderBreadcrumbs(breadcrumbs) {
        const sortedBreadcrumbs = breadcrumbs.sort((a, b)=>{
            const indexA = this.facetManagerState.facetIds.indexOf(a.facetId);
            const indexB = this.facetManagerState.facetIds.indexOf(b.facetId);
            return indexA - indexB;
        });
        this.numberOfBreadcrumbs = sortedBreadcrumbs.length;
        return sortedBreadcrumbs.map((breadcrumb, index)=>{
            const isLastBreadcrumb = 1 === this.allBreadcrumbs.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
                }
            })}`)}`;
        });
    }
    constructor(){
        super(), _class_private_field_init(this, _hiddenState, {
            writable: true,
            value: void 0
        }), this.lastRemovedBreadcrumbIndex = 0, this.numberOfBreadcrumbs = 0, this.numberOfCollapsedBreadcrumbs = 0, _class_private_field_set(this, _hiddenState, new hidden_state_controller_cjs_namespaceObject.HiddenStateController(this)), this.isCollapsed = true, this.showMoreText = '', this.disableCollapse = false, this.breadboxAriaMessage = new accessibility_utils_cjs_namespaceObject.AriaLiveRegionController(this, 'breadbox', true), this.pathLimit = 3;
        new validate_props_controller_cjs_namespaceObject.ValidatePropsController(this, ()=>({
                pathLimit: this.pathLimit
            }), AtomicBreadbox.propsSchema);
    }
}
AtomicBreadbox.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;
      }
    `
];
AtomicBreadbox.propsSchema = new bueno_namespaceObject.Schema({
    pathLimit: new bueno_namespaceObject.NumberValue({
        default: 3,
        min: 1,
        required: false
    })
});
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('breadcrumbManager'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof BreadcrumbManagerState ? Object : BreadcrumbManagerState)
], AtomicBreadbox.prototype, "breadcrumbManagerState", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('facetManager'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof FacetManagerState ? Object : FacetManagerState)
], AtomicBreadbox.prototype, "facetManagerState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicBreadbox.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicBreadbox.prototype, "isCollapsed", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicBreadbox.prototype, "showMoreText", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        attribute: 'disable-collapse'
    })
], AtomicBreadbox.prototype, "disableCollapse", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        attribute: 'path-limit'
    })
], AtomicBreadbox.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)
], AtomicBreadbox.prototype, "render", null);
AtomicBreadbox = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-breadbox'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicBreadbox);
exports.AtomicBreadbox = __webpack_exports__.AtomicBreadbox;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicBreadbox"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
