"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__, {
    BaseTemplateController: ()=>BaseTemplateController
});
const item_layout_sections_cjs_namespaceObject = require("../layout/item-layout-sections.cjs");
const external_table_element_utils_cjs_namespaceObject = require("../table-element-utils.cjs");
const utils_cjs_namespaceObject = require("../../../utils/utils.cjs");
class BaseTemplateController {
    hostConnected() {
        this.validateTemplate();
    }
    getLinkTemplateElement(host) {
        return host.querySelector('template[slot="link"]') ?? this.getDefaultLinkTemplateElement();
    }
    getBaseTemplate(conditions) {
        if (this.host.error) return null;
        return {
            conditions: conditions.concat(this.matchConditions),
            content: this.getTemplateElement().content,
            linkContent: this.getLinkTemplateElement(this.host).content,
            priority: 1
        };
    }
    get currentGridCellLinkTarget() {
        return this.gridCellLinkTarget;
    }
    setError(error) {
        this.host.error = error;
    }
    get parentElement() {
        return this.host.parentElement;
    }
    get template() {
        return this.host.querySelector('template');
    }
    parentAttr(attribute) {
        return this.parentElement?.attributes.getNamedItem(attribute)?.value;
    }
    validateTemplate() {
        const hasValidParent = this.validParents.map((p)=>p.toUpperCase()).includes(this.parentElement?.nodeName || '');
        const tagName = this.host.nodeName.toLowerCase();
        if (!hasValidParent) return void this.setError(new Error(BaseTemplateController.ERRORS.invalidParent(tagName, this.validParents)));
        if ('grid' === this.parentAttr('display')) this.gridCellLinkTarget = this.parentAttr('grid-cell-link-target');
        if (!this.template) return void this.setError(new Error(BaseTemplateController.ERRORS.missingTemplate(tagName)));
        if (!this.allowEmpty && !this.template.innerHTML.trim()) return void this.setError(new Error(BaseTemplateController.ERRORS.emptyTemplate(tagName)));
        const warnings = this.getWarnings();
        if (this.template.content.querySelector("script")) console.warn(warnings.scriptTag, this.host);
        const { section, other } = this.groupNodesByType(this.template.content.childNodes);
        if (section?.length && other?.length) console.warn(warnings.sectionMix, this.host, {
            section,
            other
        });
    }
    getWarnings() {
        return {
            scriptTag: 'Any "script" tags defined inside of "template" elements are not supported and will not be executed when the items are rendered.',
            sectionMix: 'Item templates should only contain section elements or non-section elements, not both. Future updates could unpredictably affect this item template.'
        };
    }
    getTemplateElement() {
        return this.host.querySelector('template:not([slot])');
    }
    groupNodesByType(nodes) {
        return (0, utils_cjs_namespaceObject.aggregate)(Array.from(nodes), (node)=>this.getTemplateNodeType(node));
    }
    getTemplateNodeType(node) {
        if ((0, item_layout_sections_cjs_namespaceObject.isResultSectionNode)(node)) return 'section';
        if (!(0, utils_cjs_namespaceObject.isVisualNode)(node)) return 'metadata';
        if ((0, utils_cjs_namespaceObject.isElementNode)(node) && node.tagName.toLowerCase() === external_table_element_utils_cjs_namespaceObject.tableElementTagName) return 'table-column-definition';
        return 'other';
    }
    constructor(host, validParents, allowEmpty = false){
        this.host = host;
        this.validParents = validParents;
        this.allowEmpty = allowEmpty;
        this.matchConditions = [];
        this.host.addController(this);
    }
}
BaseTemplateController.ERRORS = {
    invalidParent: (tagName, validParents)=>`The "${tagName}" component has to be the child of one of the following: ${validParents.map((p)=>`"${p.toLowerCase()}"`).join(', ')}.`,
    missingTemplate: (tagName)=>`The "${tagName}" component must contain a "template" element as a child.`,
    emptyTemplate: (tagName)=>`The "template" tag inside "${tagName}" cannot be empty.`
};
exports.BaseTemplateController = __webpack_exports__.BaseTemplateController;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "BaseTemplateController"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
