"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__, {
    AtomicResultChildren: ()=>AtomicResultChildren
});
const headless_namespaceObject = require("@coveo/headless");
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 child_templates_context_controller_cjs_namespaceObject = require("../../common/item-list/context/child-templates-context-controller.cjs");
const folded_item_list_context_controller_cjs_namespaceObject = require("../../common/item-list/context/folded-item-list-context-controller.cjs");
const item_display_config_context_controller_cjs_namespaceObject = require("../../common/item-list/context/item-display-config-context-controller.cjs");
const result_template_provider_cjs_namespaceObject = require("../../common/item-list/result-template-provider.cjs");
const unfolded_item_cjs_namespaceObject = require("../../common/item-list/unfolded-item.cjs");
const children_wrapper_cjs_namespaceObject = require("../../common/result-children/children-wrapper.cjs");
const collection_guard_cjs_namespaceObject = require("../../common/result-children/collection-guard.cjs");
const guard_cjs_namespaceObject = require("../../common/result-children/guard.cjs");
const show_hide_button_cjs_namespaceObject = require("../../common/result-children/show-hide-button.cjs");
const result_context_controller_cjs_namespaceObject = require("../result-template-component-utils/context/result-context-controller.cjs");
const bindings_cjs_namespaceObject = require("../../../decorators/bindings.cjs");
const with_tailwind_styles_cjs_namespaceObject = require("../../../decorators/with-tailwind-styles.cjs");
const event_utils_cjs_namespaceObject = require("../../../utils/event-utils.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
require("../atomic-result/atomic-result.cjs");
const binding_guard_cjs_namespaceObject = require("../../../decorators/binding-guard.cjs");
const error_guard_cjs_namespaceObject = require("../../../decorators/error-guard.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);
}
const childTemplateComponent = 'atomic-result-children-template';
const componentTag = 'atomic-result-children';
class AtomicResultChildren extends external_lit_namespaceObject.LitElement {
    connectedCallback() {
        super.connectedCallback();
        this.addEventListener('atomic/resolveChildTemplates', this.resolveChildTemplates);
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.removeEventListener('atomic/resolveChildTemplates', this.resolveChildTemplates);
        this.foldedResultListUnsubscriber?.();
    }
    initialize() {
        if (this.inheritTemplates) return;
        const childrenTemplates = Array.from(this.querySelectorAll(childTemplateComponent)).filter((template)=>!(0, utils_cjs_namespaceObject.elementHasAncestorTag)(template, childTemplateComponent));
        if (!childrenTemplates.length) {
            this.error = new Error(`The "${componentTag}" component requires at least one "${childTemplateComponent}" component.`);
            return;
        }
        this.itemTemplateProvider = new result_template_provider_cjs_namespaceObject.ResultTemplateProvider({
            includeDefaultTemplate: false,
            templateElements: childrenTemplates,
            getResultTemplateRegistered: ()=>this.resultTemplateRegistered,
            getTemplateHasError: ()=>this.templateHasError,
            setResultTemplateRegistered: (value)=>{
                this.resultTemplateRegistered = value;
            },
            setTemplateHasError: (value)=>{
                this.templateHasError = value;
            },
            bindings: this.bindings
        });
    }
    get foldedResultList() {
        return this.foldedItemListContextController.foldedItemList;
    }
    get result() {
        return this.resultContextController.item;
    }
    get displayConfig() {
        return this.displayConfigContextController.config;
    }
    get effectiveItemTemplateProvider() {
        return this.itemTemplateProvider || this.childTemplatesContextController.itemTemplateProvider;
    }
    get collection() {
        if (!this.foldedResultListState || !this.result) return;
        return this.foldedResultListState.results.find((r)=>r.result.uniqueId === this.result?.result.uniqueId);
    }
    willUpdate() {
        if (this.foldedResultList && !this.foldedResultListUnsubscriber) {
            this.foldedResultListUnsubscriber = this.foldedResultList.subscribe(()=>{
                this.foldedResultListState = this.foldedResultList.state;
                if (!this.initialChildren && this.collection) this.initialChildren = this.collection.children;
            });
            this.foldedResultListState = this.foldedResultList.state;
        }
        if (!this.initialChildren && this.collection) this.initialChildren = this.collection.children;
    }
    loadFullCollection() {
        this.loadedFullCollection = true;
        this.dispatchEvent((0, event_utils_cjs_namespaceObject.buildCustomEvent)('atomic/loadCollection', this.collection));
    }
    renderChild(child, isLast) {
        const content = this.effectiveItemTemplateProvider?.getTemplateContent(child.result);
        if (!content) return external_lit_namespaceObject.nothing;
        const key = child.result.uniqueId + child.children.map((c)=>c.result.uniqueId).join(',');
        return (0, external_lit_namespaceObject.html)`<atomic-result
      .key=${key}
      .content=${content}
      .result=${child}
      .interactiveResult=${(0, headless_namespaceObject.buildInteractiveResult)(this.bindings.engine, {
            options: {
                result: (0, unfolded_item_cjs_namespaceObject.extractUnfoldedItem)(child)
            }
        })}
      .store=${this.bindings.store}
      density=${this.displayConfig?.density ?? 'normal'}
      image-size=${this.imageSize || this.displayConfig?.imageSize || 'icon'}
      classes=${`child-result ${isLast ? 'last-child' : ''}`.trim()}
    ></atomic-result>`;
    }
    renderChildren(children) {
        if (0 === children.length) return external_lit_namespaceObject.nothing;
        return (0, children_wrapper_cjs_namespaceObject.renderChildrenWrapper)()((0, external_lit_namespaceObject.html)`${(0, map_js_namespaceObject.map)(children, (child, i)=>this.renderChild(child, i === children.length - 1))}`);
    }
    renderCollection() {
        const collection = this.collection;
        const children = this.showInitialChildren ? this.initialChildren : collection.children;
        const showShouldButtons = this.loadedFullCollection || collection.moreResultsAvailable;
        const showHideButton = showShouldButtons ? (0, show_hide_button_cjs_namespaceObject.renderShowHideButton)({
            props: {
                moreResultsAvailable: collection.moreResultsAvailable,
                loadFullCollection: ()=>this.loadFullCollection(),
                showInitialChildren: this.showInitialChildren,
                toggleShowInitialChildren: this.toggleShowInitialChildren,
                loadAllResults: this.bindings.i18n.t('load-all-results'),
                collapseResults: this.bindings.i18n.t('collapse-results')
            }
        }) : external_lit_namespaceObject.nothing;
        const guard = (0, collection_guard_cjs_namespaceObject.renderCollectionGuard)({
            props: {
                isLoadingMoreResults: collection.isLoadingMoreResults,
                moreResultsAvailable: collection.moreResultsAvailable,
                numberOfChildren: collection.children.length,
                density: this.displayConfig?.density ?? 'normal',
                imageSize: this.imageSize || this.displayConfig?.imageSize || 'icon',
                noResultText: this.bindings.i18n.t(this.noResultText)
            }
        });
        return (0, external_lit_namespaceObject.html)`${showHideButton}${guard(this.renderChildren(children))}`;
    }
    renderFoldedResult() {
        if (!this.result || 0 === this.result.children.length) return external_lit_namespaceObject.nothing;
        return this.renderChildren(this.result.children);
    }
    render() {
        return (0, external_lit_namespaceObject.html)`${(0, when_js_namespaceObject.when)(this.bindings && (this.displayConfig || this.inheritTemplates), ()=>(0, guard_cjs_namespaceObject.renderResultChildrenGuard)({
                props: {
                    inheritTemplates: this.inheritTemplates,
                    resultTemplateRegistered: this.resultTemplateRegistered,
                    templateHasError: this.templateHasError
                }
            })(this.collection ? this.renderCollection() : this.renderFoldedResult()))}`;
    }
    constructor(){
        super(), this.inheritTemplates = false, this.noResultText = 'no-documents-related', this.resultTemplateRegistered = false, this.templateHasError = false, this.showInitialChildren = true, this.loadedFullCollection = false, this.resolveChildTemplates = (event)=>{
            event.preventDefault();
            const provider = this.itemTemplateProvider || this.childTemplatesContextController.itemTemplateProvider;
            event.detail(provider ?? void 0);
        }, this.toggleShowInitialChildren = ()=>{
            if (!this.foldedResultList || !this.result) return;
            if (this.showInitialChildren) this.foldedResultList.logShowMoreFoldedResults(this.result.result);
            else this.foldedResultList.logShowLessFoldedResults();
            this.showInitialChildren = !this.showInitialChildren;
        };
        this.foldedItemListContextController = new folded_item_list_context_controller_cjs_namespaceObject.FoldedItemListContextController(this);
        this.childTemplatesContextController = new child_templates_context_controller_cjs_namespaceObject.ChildTemplatesContextController(this);
        this.displayConfigContextController = new item_display_config_context_controller_cjs_namespaceObject.ItemDisplayConfigContextController(this);
        this.resultContextController = (0, result_context_controller_cjs_namespaceObject.createResultContextController)(this, {
            folded: true
        });
    }
}
AtomicResultChildren.styles = (0, external_lit_namespaceObject.css)`
    @reference '../../../utils/tailwind.global.tw.css';
    @reference '../../../utils/tailwind-utilities/set-font-size.css';

    .show-hide-button {
      @apply set-font-size-sm;
    }

    .no-result-root {
      @apply text-neutral-dark;
    }
  `;
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        type: Boolean,
        attribute: 'inherit-templates'
    })
], AtomicResultChildren.prototype, "inheritTemplates", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        reflect: true,
        attribute: 'image-size'
    }),
    _ts_metadata("design:type", "u" < typeof ItemDisplayImageSize ? Object : ItemDisplayImageSize)
], AtomicResultChildren.prototype, "imageSize", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.property)({
        attribute: 'no-result-text'
    })
], AtomicResultChildren.prototype, "noResultText", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Bindings ? Object : Bindings)
], AtomicResultChildren.prototype, "bindings", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicResultChildren.prototype, "error", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicResultChildren.prototype, "resultTemplateRegistered", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicResultChildren.prototype, "templateHasError", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicResultChildren.prototype, "showInitialChildren", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)()
], AtomicResultChildren.prototype, "loadedFullCollection", void 0);
_ts_decorate([
    (0, decorators_js_namespaceObject.state)(),
    _ts_metadata("design:type", "u" < typeof FoldedResultListState ? Object : FoldedResultListState)
], AtomicResultChildren.prototype, "foldedResultListState", void 0);
_ts_decorate([
    (0, error_guard_cjs_namespaceObject.errorGuard)(),
    (0, binding_guard_cjs_namespaceObject.bindingGuard)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicResultChildren.prototype, "render", null);
AtomicResultChildren = _ts_decorate([
    (0, decorators_js_namespaceObject.customElement)('atomic-result-children'),
    with_tailwind_styles_cjs_namespaceObject.withTailwindStyles,
    (0, bindings_cjs_namespaceObject.bindings)(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicResultChildren);
exports.AtomicResultChildren = __webpack_exports__.AtomicResultChildren;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "AtomicResultChildren"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
