"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__, {
    AtomicProductMultiValueText: ()=>AtomicProductMultiValueText
});
const commerce_namespaceObject = require("@coveo/headless/commerce");
const external_lit_namespaceObject = require("lit");
const decorators_js_namespaceObject = require("lit/decorators.js");
const product_context_controller_cjs_namespaceObject = require("../product-template-component-utils/context/product-context-controller.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 field_utils_cjs_namespaceObject = require("../../../utils/field-utils.cjs");
const utils_cjs_namespaceObject = require("../../../utils/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 AtomicProductMultiValueText extends external_lit_namespaceObject.LitElement {
    initialize() {
        const searchOrListing = 'product-listing' === this.bindings.interfaceElement.type ? (0, commerce_namespaceObject.buildProductListing)(this.bindings.engine) : (0, commerce_namespaceObject.buildSearch)(this.bindings.engine);
        this.breadcrumbManager = searchOrListing.breadcrumbManager();
        this.values = this.initializeValues();
    }
    initializeValues() {
        const product = this.productController.item;
        if (!product) return [];
        const property = commerce_namespaceObject.ProductTemplatesHelpers.getProductProperty(product, this.field);
        if (null === property) return [];
        if (Array.isArray(property)) return property.map((v)=>`${v}`.trim());
        if ('string' != typeof property || '' === property.trim()) {
            this.error = new Error(`Could not parse "${property}" from field "${this.field}" as a string array.`);
            return [];
        }
        return this.delimiter ? property.split(this.delimiter).map((value)=>value.trim()) : [
            property
        ];
    }
    get facetSelectedValues() {
        return this.breadcrumbManager.state.facetBreadcrumbs.reduce((values, facet)=>{
            if (facet.field !== this.field) return values;
            return values.concat(facet.values.map(({ value })=>value.value));
        }, []);
    }
    get sortedValues() {
        const firstValues = this.facetSelectedValues.filter((value)=>this.values.includes(value));
        return Array.from(new Set([
            ...firstValues,
            ...this.values
        ]));
    }
    get shouldDisplayLabel() {
        return this.maxValuesToDisplay > 0 && this.sortedValues.length > this.maxValuesToDisplay;
    }
    get numberOfValuesToDisplay() {
        return Math.min(this.sortedValues.length, this.maxValuesToDisplay);
    }
    renderValue(value) {
        const label = (0, field_utils_cjs_namespaceObject.getFieldValueCaption)(this.field, value, this.bindings.i18n);
        const kebabValue = (0, utils_cjs_namespaceObject.titleToKebab)(value);
        return (0, external_lit_namespaceObject.html)`
      <li part="product-multi-value-text-value" class="inline-block">
        <slot name=${`product-multi-value-text-value-${kebabValue}`}>
          ${label}
        </slot>
      </li>
    `;
    }
    renderSeparator() {
        return (0, external_lit_namespaceObject.html)`
      <li
        aria-hidden="true"
        part="product-multi-value-text-separator"
        class="${String.raw`inline-block before:inline before:content-[',\00a0']`}"
      ></li>
    `;
    }
    renderMoreLabel(value) {
        return (0, external_lit_namespaceObject.html)`
      <li part="product-multi-value-text-value-more" class="inline-block">
        ${this.bindings.i18n.t('n-more', {
            value
        })}
      </li>
    `;
    }
    renderListItems(values) {
        const templates = [];
        templates.push(this.renderValue(values[0]));
        for(let i = 1; i < this.numberOfValuesToDisplay; i++){
            templates.push(this.renderSeparator());
            templates.push(this.renderValue(values[i]));
        }
        if (this.shouldDisplayLabel) {
            templates.push(this.renderSeparator());
            templates.push(this.renderMoreLabel(values.length - this.numberOfValuesToDisplay));
        }
        return templates;
    }
    render() {
        if (0 === this.sortedValues.length) return external_lit_namespaceObject.nothing;
        return (0, external_lit_namespaceObject.html)`
      <ul
        part="product-multi-value-text-list"
        class="m-0 flex list-none p-0 set-font-size-sm"
      >
        ${this.renderListItems(this.sortedValues)}
      </ul>
    `;
    }
    constructor(...args){
        super(...args), this.productController = (0, product_context_controller_cjs_namespaceObject.createProductContextController)(this), this.maxValuesToDisplay = 3, this.delimiter = null, this.values = [];
    }
}
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof CommerceBindings ? Object : CommerceBindings)
], AtomicProductMultiValueText.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicProductMultiValueText.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicProductMultiValueText.prototype, "field", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true,
        type: Number,
        attribute: 'max-values-to-display'
    })
], AtomicProductMultiValueText.prototype, "maxValuesToDisplay", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true
    }),
    _ts_metadata("design:type", Object)
], AtomicProductMultiValueText.prototype, "delimiter", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", Array)
], AtomicProductMultiValueText.prototype, "values", 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)
], AtomicProductMultiValueText.prototype, "render", null);
AtomicProductMultiValueText = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-product-multi-value-text'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles
], AtomicProductMultiValueText);
exports.AtomicProductMultiValueText = __webpack_exports__.AtomicProductMultiValueText;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicProductMultiValueText"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
