import { h } from "@stencil/core";
import { Host } from "@stencil/core/internal";
import { cls } from "../../utils/helpers";
export class XTabs {
    constructor() {
        this.block = false;
        this.position = 'left';
        this.tabs = [];
    }
    /**
     * Element event on slot make changes
     *
     * @param evt
     */
    handleSlotChange() {
        this.computeChildTabs();
    }
    /**
     * On component did loaded hook
     */
    componentDidLoad() {
        this.computeChildTabs();
        if (!this.tabs.length) {
            return;
        }
        setTimeout(() => {
            if (!this.el.querySelectorAll('nav .tab-active').length) {
                this.showTab(0, this.el.querySelectorAll('.tab-content x-tab').item(0));
            }
        }, 10);
    }
    /**
     * Update the child tabs and render teh result
     */
    computeChildTabs() {
        this.tabs = Array.from(this.el.querySelector('.tab-content').children)
            .filter(tab => tab.nodeName.toLowerCase() === 'x-tab');
    }
    /**
     * Show the content of selected tab by index and set active
     *
     * @param index
     * @param tab
     */
    showTab(index, tab) {
        this.el.querySelectorAll('x-tab.tab-active, nav li.tab-active').forEach(tab => {
            tab.setAttribute('active', 'false');
            tab.classList.remove('tab-active');
        });
        setTimeout(() => {
            var _a;
            (_a = this.el.querySelector(`.tab-index-${index}`)) === null || _a === void 0 ? void 0 : _a.classList.add('tab-active');
            tab.setAttribute('active', 'true');
        }, 10);
    }
    /**
     * Render element
     */
    render() {
        return h(Host, { key: '63094062152c8178a5f6daf6a642297b31af5db6' }, h("nav", { key: '3cf279acdd2fa05d57d9e7196de288815bbdf61c', class: cls(`tabs-${this.position}`, { 'tabs-block': this.block }) }, h("ul", { key: '05e62c14175b51cef36b0daf7c1f8d036bd0217b' }, this.tabs.map((tab, i) => (h("li", { key: i, class: cls(`tab-index-${i}`, { 'tab-disabled': tab.classList.contains('tab-disabled'), 'tab-active': tab.classList.contains('tab-active') }) }, h("a", { href: "javascript:;", tabIndex: i, onClick: () => this.showTab(i, tab) }, tab.getAttribute('icon') && h("x-icon", { name: tab.getAttribute('icon') }), h("span", null, tab.getAttribute('label')), tab.getAttribute('badge') && h("x-badge", { color: "primary" }, tab.getAttribute('badge')))))))), h("div", { key: 'dd928ac5fa5d3bffa0947b4537b61c70a2bb2d1d', class: "tab-content" }, h("slot", { key: '176ee5e3e0ad043409202ff41dda9ab7b2a26d47' })));
    }
    static get is() { return "x-tabs"; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "block": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element full width block style"
                },
                "attribute": "block",
                "reflect": false,
                "defaultValue": "false"
            },
            "position": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "TabsPositionType",
                    "resolved": "\"centered\" | \"left\" | \"right\"",
                    "references": {
                        "TabsPositionType": {
                            "location": "local",
                            "path": "/opt/atlassian/pipelines/agent/build/src/components/x-tabs/component.tsx",
                            "id": "src/components/x-tabs/component.tsx::TabsPositionType"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element align position tabs"
                },
                "attribute": "position",
                "reflect": false,
                "defaultValue": "'left'"
            }
        };
    }
    static get states() {
        return {
            "tabs": {}
        };
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "slotchange",
                "method": "handleSlotChange",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
