"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__, {
    AtomicCommerceFacets: ()=>AtomicCommerceFacets
});
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 map_js_namespaceObject = require("lit/directives/map.js");
const when_js_namespaceObject = require("lit/directives/when.js");
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");
require("../../common/atomic-facet-placeholder/atomic-facet-placeholder.cjs");
const store_cjs_namespaceObject = require("../../common/interface/store.cjs");
require("../atomic-commerce-category-facet/atomic-commerce-category-facet.cjs");
require("../atomic-commerce-facet/atomic-commerce-facet.cjs");
require("../atomic-commerce-numeric-facet/atomic-commerce-numeric-facet.cjs");
require("../atomic-commerce-timeframe-facet/atomic-commerce-timeframe-facet.cjs");
const keyed_js_namespaceObject = require("lit/directives/keyed.js");
const bind_state_cjs_namespaceObject = require("../../../decorators/bind-state.cjs");
const light_dom_cjs_namespaceObject = require("../../../mixins/light-dom.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 AtomicCommerceFacets extends (0, light_dom_cjs_namespaceObject.LightDomMixin)(external_lit_namespaceObject.LitElement) {
    initialize() {
        this.validateProps();
        const controller = this.controllerBuilder(this.bindings.engine);
        this.facetGenerator = controller.facetGenerator();
        this.summary = controller.summary();
        (0, store_cjs_namespaceObject.createAppLoadedListener)(this.bindings.store, (isAppLoaded)=>{
            this.isAppLoaded = isAppLoaded;
        });
    }
    isProductListing() {
        return 'product-listing' === this.bindings.interfaceElement.type;
    }
    get controllerBuilder() {
        return this.isProductListing() ? commerce_namespaceObject.buildProductListing : commerce_namespaceObject.buildSearch;
    }
    validateProps() {
        new bueno_namespaceObject.Schema({
            collapseFacetsAfter: new bueno_namespaceObject.NumberValue({
                min: -1,
                required: true
            })
        }).validate({
            collapseFacetsAfter: this.collapseFacetsAfter
        });
    }
    shouldCollapseFacet(index, facet) {
        if (-1 === this.collapseFacetsAfter || facet.state.hasActiveValues) return false;
        return this.collapseFacetsAfter ? index + 1 > this.collapseFacetsAfter : true;
    }
    renderFacetPlaceholders() {
        return (0, map_js_namespaceObject.map)(Array.from({
            length: this.collapseFacetsAfter
        }), ()=>(0, external_lit_namespaceObject.html)`<atomic-facet-placeholder
          value-count="8"
        ></atomic-facet-placeholder>`);
    }
    renderFacets() {
        return (0, map_js_namespaceObject.map)(this.facetGenerator.facets, (facet, index)=>{
            if (0 === facet.state.values.length) return external_lit_namespaceObject.nothing;
            const isCollapsed = this.shouldCollapseFacet(index, facet);
            switch(facet.state.type){
                case 'regular':
                    return (0, external_lit_namespaceObject.html)`${(0, keyed_js_namespaceObject.keyed)(facet.state.facetId, (0, external_lit_namespaceObject.html)`
              <atomic-commerce-facet
                .isCollapsed=${isCollapsed}
                .summary=${this.summary}
                .facet=${facet}
                .field=${facet.state.field}
              ></atomic-commerce-facet>
            `)} `;
                case 'numericalRange':
                    return (0, external_lit_namespaceObject.html)`${(0, keyed_js_namespaceObject.keyed)(facet.state.facetId, (0, external_lit_namespaceObject.html)`<atomic-commerce-numeric-facet
              .isCollapsed=${isCollapsed}
              .summary=${this.summary}
              .facet=${facet}
              .field=${facet.state.field}
            ></atomic-commerce-numeric-facet>`)}`;
                case 'dateRange':
                    return (0, external_lit_namespaceObject.html)`${(0, keyed_js_namespaceObject.keyed)(facet.state.facetId, (0, external_lit_namespaceObject.html)`<atomic-commerce-timeframe-facet
              .isCollapsed=${isCollapsed}
              .summary=${this.summary}
              .facet=${facet}
              .field=${facet.state.field}
            ></atomic-commerce-timeframe-facet>`)}`;
                case 'hierarchical':
                    return (0, external_lit_namespaceObject.html)`${(0, keyed_js_namespaceObject.keyed)(facet.state.facetId, (0, external_lit_namespaceObject.html)`<atomic-commerce-category-facet
              .isCollapsed=${isCollapsed}
              .summary=${this.summary}
              .facet=${facet}
              .field=${facet.state.field}
            ></atomic-commerce-category-facet>`)}`;
                default:
                    this.bindings.engine.logger.warn('Unexpected facet type.');
                    return external_lit_namespaceObject.nothing;
            }
        });
    }
    render() {
        return (0, external_lit_namespaceObject.html)`${(0, when_js_namespaceObject.when)(this.isAppLoaded, ()=>this.renderFacets(), ()=>this.renderFacetPlaceholders())}`;
    }
    constructor(...args){
        super(...args), this.collapseFacetsAfter = 4, this.isAppLoaded = false;
    }
}
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicCommerceFacets.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicCommerceFacets.prototype, "error", void 0);
_ts_decorate([
    (0, bind_state_cjs_namespaceObject.bindStateToController)('facetGenerator'),
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", Array)
], AtomicCommerceFacets.prototype, "facetGeneratorState", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true,
        attribute: 'collapse-facets-after',
        type: Number
    })
], AtomicCommerceFacets.prototype, "collapseFacetsAfter", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicCommerceFacets.prototype, "isAppLoaded", 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)
], AtomicCommerceFacets.prototype, "render", null);
AtomicCommerceFacets = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-commerce-facets'),
    (0, bindings_cjs_namespaceObject.bindings)()
], AtomicCommerceFacets);
exports.AtomicCommerceFacets = __webpack_exports__.AtomicCommerceFacets;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicCommerceFacets"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
