/*!
 * KoliBri - The accessible HTML-Standard
 */
import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
import { c as KolLinkWcTag, t as KolTreeTag } from './component-names.js';
import { I as IconFC } from './component3.js';
import './common.js';
import { b as watchBoolean } from './prop.validators.js';
import { v as validateHref } from './href.js';
import { v as validateLabel } from './label.js';
import { v as validateOpen } from './open.js';
import { c as clsx } from './clsx.js';
import { c as createUniqueId } from './dev.utils.js';

const validateActive = (component, value, options) => {
    watchBoolean(component, '_active', value, options);
};

const KolTreeItemWc$1 = proxyCustomElement(class KolTreeItemWc extends HTMLElement {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.groupId = createUniqueId('tree-group');
        this.state = {
            _active: false,
            _hasChildren: false,
            _href: '',
            _label: '',
            _open: false,
        };
    }
    render() {
        const { _href, _active, _hasChildren, _open, _label } = this.state;
        return (h(Host, { key: '5dafc8516e71c4908d468b8844b0e765d1cba71a', onSlotchange: this.handleSlotchange.bind(this) }, h("li", { key: 'b050361b08edf953c60cda62bce3c716528d19ca', class: "kol-tree-item", style: {
                '--level': `${this.level}`,
            } }, h(KolLinkWcTag, { key: '5575f3158d9adbb566f75ec7687d83f1b84a8935', class: clsx('kol-tree-item__link', {
                'kol-tree-item__link--first-level': this.level === 0,
                'kol-tree-item__link--active': _active,
            }), _href: _href, _label: "", _role: "treeitem", _tabIndex: _active ? 0 : -1, _ariaExpanded: _hasChildren ? _open : undefined, _ariaOwns: _hasChildren ? this.groupId : undefined, ref: (element) => (this.linkElement = element) }, h("span", { key: 'f64170d2888e773e1649252efac509108c92ebe6', class: "kol-tree-item__link-inner", slot: "expert" }, _hasChildren ? (h("span", { class: "kol-tree-item__toggle-button", onClick: (event) => (_open ? void this.handleCollapseClick(event) : void this.handleExpandClick(event)) }, h(IconFC, { class: "kol-tree-item__toggle-button-icon", icons: `kolicon kolicon-${_open ? 'chevron-down' : 'chevron-right'}`, label: '' }))) : (h("span", { class: "kol-tree-item__toggle-button-placeholder" })), h("span", { key: '02eeae533f9c27fb1d34856ef83dc54b0472abb4', class: "kol-tree-item__text" }, _label))), h("ul", { key: 'fdfad19b37792ed8efed76b6e9b7bf977ee7d6cb', class: "kol-tree-item__children", hidden: !_hasChildren || !_open, role: "group", id: this.groupId }, h("slot", { key: 'eba7b5f9a6ee173a12d67238195d5c5fd34f38b4' })))));
    }
    validateActive(value) {
        validateActive(this, value || false);
    }
    validateLabel(value) {
        validateLabel(this, value);
    }
    validateOpen(value) {
        validateOpen(this, value);
    }
    validateHref(value) {
        validateHref(this, value);
    }
    componentWillLoad() {
        this.validateActive(this._active);
        this.validateLabel(this._label);
        this.validateOpen(this._open);
        this.validateHref(this._href);
        this.checkForChildren();
        this.determineTreeItemDepth();
    }
    determineTreeItemDepth() {
        var _a, _b;
        let level = 0;
        let traverseItem = (_b = (_a = this.host) === null || _a === void 0 ? void 0 : _a.parentNode) === null || _b === void 0 ? void 0 : _b.host.parentNode;
        while (traverseItem !== null && traverseItem.tagName.toLowerCase() !== KolTreeTag && traverseItem !== document.body) {
            traverseItem = traverseItem.parentElement;
            level += 1;
        }
        this.level = level;
    }
    handleSlotchange() {
        this.checkForChildren();
    }
    checkForChildren() {
        var _a, _b, _c;
        this.state = Object.assign(Object.assign({}, this.state), { _hasChildren: Boolean((_c = (_b = (_a = this.host) === null || _a === void 0 ? void 0 : _a.querySelector('slot')) === null || _b === void 0 ? void 0 : _b.assignedElements) === null || _c === void 0 ? void 0 : _c.call(_b).length) });
    }
    getTreeParent() {
        var _a, _b;
        let element = this.host;
        while (element) {
            const parent = element.closest(KolTreeTag);
            if (parent) {
                const treeWc = (_a = parent.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector('kol-tree-wc');
                if (treeWc) {
                    return treeWc;
                }
                return undefined;
            }
            const shadowHost = (_b = element.getRootNode()) === null || _b === void 0 ? void 0 : _b.host;
            if (!shadowHost || shadowHost === document.body) {
                break;
            }
            element = shadowHost.parentElement;
        }
        return undefined;
    }
    async focus(options) {
        if (this.host && this.linkElement) {
            return this.linkElement.focus(options);
        }
    }
    async handleExpandClick(event) {
        event.preventDefault();
        if (this.host && this.linkElement) {
            await this.linkElement.focus();
        }
        await this.expand();
    }
    async expand() {
        var _a, _b;
        if (this.state._hasChildren) {
            this.state = Object.assign(Object.assign({}, this.state), { _open: true });
            void ((_b = (_a = this.getTreeParent()) === null || _a === void 0 ? void 0 : _a.invalidateOpenItemsCache) === null || _b === void 0 ? void 0 : _b.call(_a));
        }
    }
    async handleCollapseClick(event) {
        event.preventDefault();
        if (this.host && this.linkElement) {
            await this.linkElement.focus();
        }
        await this.collapse();
    }
    async collapse() {
        var _a, _b;
        if (this.state._hasChildren) {
            this.state = Object.assign(Object.assign({}, this.state), { _open: false });
            void ((_b = (_a = this.getTreeParent()) === null || _a === void 0 ? void 0 : _a.invalidateOpenItemsCache) === null || _b === void 0 ? void 0 : _b.call(_a));
        }
    }
    async isOpen() {
        var _a;
        return (_a = this.state._open) !== null && _a !== void 0 ? _a : false;
    }
    get host() { return this; }
    static get watchers() { return {
        "_active": ["validateActive"],
        "_label": ["validateLabel"],
        "_open": ["validateOpen"],
        "_href": ["validateHref"]
    }; }
}, [260, "kol-tree-item-wc", {
        "_active": [4],
        "_label": [1],
        "_open": [4],
        "_href": [1],
        "level": [32],
        "state": [32],
        "focus": [64],
        "expand": [64],
        "collapse": [64],
        "isOpen": [64]
    }, undefined, {
        "_active": ["validateActive"],
        "_label": ["validateLabel"],
        "_open": ["validateOpen"],
        "_href": ["validateHref"]
    }]);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["kol-tree-item-wc"];
    components.forEach(tagName => { switch (tagName) {
        case "kol-tree-item-wc":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, KolTreeItemWc$1);
            }
            break;
    } });
}

const KolTreeItemWc = KolTreeItemWc$1;
const defineCustomElement = defineCustomElement$1;

export { KolTreeItemWc, defineCustomElement };
//# sourceMappingURL=kol-tree-item-wc.js.map

//# sourceMappingURL=kol-tree-item-wc.js.map