"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__, {
    ItemLayoutController: ()=>ItemLayoutController
});
const external_item_layout_utils_cjs_namespaceObject = require("./item-layout-utils.cjs");
class ItemLayoutController {
    hostConnected() {
        this.createLayout();
    }
    hostUpdated() {
        if (!this.options.classesOnly) this.applyLayoutClasses();
    }
    getLayout() {
        return this.layout;
    }
    getCombinedClasses(additionalContent) {
        const config = this.getLayout();
        const layoutClasses = config ? (0, external_item_layout_utils_cjs_namespaceObject.getItemLayoutClasses)(config, additionalContent) : [];
        const itemClasses = this.options.itemClasses().split(/\s+/).filter((c)=>c);
        return [
            ...layoutClasses,
            ...itemClasses
        ];
    }
    applyLayoutClassesToElement(element, additionalContent) {
        const config = this.getLayout();
        if (!config) return;
        const classes = this.getCombinedClasses(additionalContent);
        if (classes.length > 0) element.classList.add(...classes);
    }
    applyLayoutClasses() {
        const config = this.getLayout();
        if (!config) return;
        const classes = this.getCombinedClasses();
        const root = this.host.shadowRoot?.querySelector('.result-root');
        if (!root || 0 === classes.length) return;
        if (this.hasCustomRenderFunction()) this.observeAndApplyClasses(root, classes);
        else this.addClassesToElements(root, classes);
    }
    createLayout() {
        const content = this.options.content();
        if (!content) {
            console.warn(`${this.options.elementPrefix}: content property is undefined. Cannot create layout.`, this.host);
            this.layout = null;
            return;
        }
        const config = this.options.layoutConfig();
        this.layout = {
            children: content.children,
            display: config.display,
            density: config.density,
            imageSize: config.imageSize
        };
    }
    hasCustomRenderFunction() {
        return void 0 !== this.options.renderingFunction();
    }
    addClassesToElements(root, classes) {
        if (0 === classes.length) return;
        const elements = root.querySelectorAll('*');
        elements.forEach((element)=>{
            const tagName = element.tagName.toLowerCase();
            if (tagName.startsWith(`${this.options.elementPrefix}-`)) element.classList.add(...classes);
        });
    }
    observeAndApplyClasses(root, classes) {
        const observer = new MutationObserver((mutations)=>{
            const hasNewElements = mutations.some((mutation)=>'childList' === mutation.type && mutation.addedNodes.length > 0);
            if (hasNewElements) {
                this.addClassesToElements(root, classes);
                observer.disconnect();
            }
        });
        observer.observe(root, {
            childList: true,
            subtree: true
        });
    }
    constructor(host, options){
        this.layout = null;
        this.host = host;
        this.options = {
            classesOnly: false,
            ...options
        };
        host.addController(this);
    }
}
exports.ItemLayoutController = __webpack_exports__.ItemLayoutController;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "ItemLayoutController"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
