"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__, {
    AtomicResultMultiValueText: ()=>AtomicResultMultiValueText
});
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 keyed_js_namespaceObject = require("lit/directives/keyed.js");
const when_js_namespaceObject = require("lit/directives/when.js");
const validate_props_controller_cjs_namespaceObject = require("../../common/validate-props-controller/validate-props-controller.cjs");
const result_context_controller_cjs_namespaceObject = require("../result-template-component-utils/context/result-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 AtomicResultMultiValueText extends external_lit_namespaceObject.LitElement {
    get result() {
        return this.resultContext.item;
    }
    initialize() {
        this.breadcrumbManager = (0, headless_namespaceObject.buildBreadcrumbManager)(this.bindings.engine);
    }
    get resultValues() {
        const value = headless_namespaceObject.ResultTemplatesHelpers.getResultProperty(this.result, this.field);
        if (null === value) return null;
        if (Array.isArray(value)) return value.map((v)=>`${v}`.trim());
        if ('string' != typeof value || '' === value.trim()) {
            this.error = new Error(`Could not parse "${value}" from field "${this.field}" as a string array.`);
            return null;
        }
        return this.delimiter ? value.split(this.delimiter).map((value)=>value.trim()) : [
            value
        ];
    }
    get facetSelectedValues() {
        const values = [];
        if (!this.breadcrumbManager) return values;
        this.breadcrumbManager.state.facetBreadcrumbs.filter((facet)=>facet.field === this.field).forEach((facet)=>{
            values.push(...facet.values.map(({ value })=>value.value));
        });
        return values;
    }
    updateSortedValues() {
        const allValues = this.resultValues;
        if (null === allValues) {
            this.sortedValues = null;
            return;
        }
        const allValuesSet = new Set(allValues);
        const firstValues = this.facetSelectedValues.filter((value)=>allValuesSet.has(value));
        this.sortedValues = Array.from(allValues.reduce((set, value)=>set.add(value), new Set(firstValues)));
    }
    getShouldDisplayLabel(values) {
        return this.maxValuesToDisplay > 0 && values.length > this.maxValuesToDisplay;
    }
    getNumberOfValuesToDisplay(values) {
        return Math.min(values.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, keyed_js_namespaceObject.keyed)(value, (0, external_lit_namespaceObject.html)`
        <li part="result-multi-value-text-value">
          <slot name=${`result-multi-value-text-value-${kebabValue}`}>
            ${label}
          </slot>
        </li>
      `);
    }
    renderSeparator() {
        return (0, external_lit_namespaceObject.html)`
      <li
        aria-hidden="true"
        part="result-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="result-multi-value-text-value-more">
        ${this.bindings.i18n.t('n-more', {
            value
        })}
      </li>
    `;
    }
    renderListItems(values) {
        const numberOfValuesToDisplay = this.getNumberOfValuesToDisplay(values);
        const nodes = [];
        for(let i = 0; i < numberOfValuesToDisplay; i++){
            if (i > 0) nodes.push(this.renderSeparator());
            nodes.push(this.renderValue(values[i]));
        }
        if (this.getShouldDisplayLabel(values)) {
            nodes.push(this.renderSeparator());
            nodes.push(this.renderMoreLabel(values.length - numberOfValuesToDisplay));
        }
        return nodes;
    }
    willUpdate() {
        this.updateSortedValues();
    }
    render() {
        return (0, external_lit_namespaceObject.html)`${(0, when_js_namespaceObject.when)(this.sortedValues, ()=>(0, external_lit_namespaceObject.html)`
        <ul part="result-multi-value-text-list" class="m-0 flex list-none p-0">
          ${this.renderListItems(this.sortedValues)}
        </ul>
      `)}`;
    }
    constructor(){
        super(), this.maxValuesToDisplay = 3, this.delimiter = null, this.sortedValues = null, this.resultContext = (0, result_context_controller_cjs_namespaceObject.createResultContextController)(this);
        new validate_props_controller_cjs_namespaceObject.ValidatePropsController(this, ()=>({
                field: this.field,
                maxValuesToDisplay: this.maxValuesToDisplay
            }), AtomicResultMultiValueText.propsSchema);
    }
}
AtomicResultMultiValueText.propsSchema = new bueno_namespaceObject.Schema({
    field: new bueno_namespaceObject.StringValue({
        required: true,
        emptyAllowed: false
    }),
    maxValuesToDisplay: new bueno_namespaceObject.NumberValue({
        min: 0,
        required: false
    })
});
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", String)
], AtomicResultMultiValueText.prototype, "field", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Number,
        reflect: true,
        attribute: 'max-values-to-display'
    })
], AtomicResultMultiValueText.prototype, "maxValuesToDisplay", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: String,
        reflect: true
    }),
    _ts_metadata("design:type", Object)
], AtomicResultMultiValueText.prototype, "delimiter", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultMultiValueText.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultMultiValueText.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", Object)
], AtomicResultMultiValueText.prototype, "sortedValues", 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)
], AtomicResultMultiValueText.prototype, "render", null);
AtomicResultMultiValueText = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-result-multi-value-text'),
    (0, bindings_cjs_namespaceObject.bindings)(),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicResultMultiValueText);
exports.AtomicResultMultiValueText = __webpack_exports__.AtomicResultMultiValueText;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicResultMultiValueText"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
