import { Host, h } from "@stencil/core";
/**
* @slot tabs - Content is placed within the `div[role="tablist"]` element as children
* @slot tabpanels - Content is placed directly after the `div[role="tablist"]` element as siblings
* @part tab-list - Exposes the container element that holds all the tab buttons for styling.
*/
export class PdsTabs {
    tabClickHandler(event) {
        if (this.componentId === event.detail[1]) {
            this.activeTabIndex = event.detail[0];
            this.activeTabName = this.tabs[this.activeTabIndex].name;
        }
    }
    handleKeyDown(ev) {
        const keySet = ["ArrowLeft", "ArrowRight", "Home", "End"];
        // Only handle keyboard navigation if the event originated from a tab button
        // that belongs to THIS tabs component
        const target = ev.target;
        const targetTab = target.closest('pds-tab');
        // Check if the tab belongs to this tabs instance (not a nested one)
        const isOwnTab = targetTab && targetTab.closest('pds-tabs') === this.el;
        if (keySet.includes(ev.key) && isOwnTab) {
            ev.preventDefault();
            this.moveActiveTab(ev.key);
        }
    }
    moveActiveTab(key) {
        const firstTabNumber = 0;
        const lastTabNumber = this.tabs.length - 1;
        let moveFocusTo = null;
        switch (key) {
            case 'ArrowLeft':
                moveFocusTo = (this.activeTabIndex === firstTabNumber) ? lastTabNumber : (this.activeTabIndex + (-1));
                break;
            case 'ArrowRight':
                moveFocusTo = (this.activeTabIndex === lastTabNumber) ? firstTabNumber : (this.activeTabIndex + 1);
                break;
            case 'Home':
                moveFocusTo = firstTabNumber;
                break;
            case 'End':
                moveFocusTo = lastTabNumber;
                break;
        }
        // Move focus to the button element within `pds-tab`
        this.tabs[moveFocusTo].children[0].focus();
        this.activeTabName = this.tabs[moveFocusTo].name;
        this.activeTabIndex = moveFocusTo;
    }
    findAllChildren() {
        // Only select direct children tabs/tabpanels, not nested ones
        const allTabs = Array.from(this.el.querySelectorAll('pds-tab'));
        const allTabPanels = Array.from(this.el.querySelectorAll('pds-tabpanel'));
        // Filter to only include tabs that belong to this tabs component (not nested)
        this.tabs = allTabs.filter(tab => tab.closest('pds-tabs') === this.el);
        this.tabPanels = allTabPanels.filter(panel => panel.closest('pds-tabs') === this.el);
    }
    propGeneration(child, index = 0) {
        child.parentComponentId = this.componentId.toString();
        child.variant = this.variant.toString();
        child.selected = (this.activeTabName === child.name) ? true : false;
        child['index'] = index;
    }
    passPropsToChildren() {
        this.tabs.forEach((child, index) => {
            if (this.activeTabName === child.name)
                this.activeTabIndex = index;
            this.propGeneration(child, index);
        });
        this.tabPanels.forEach((child) => {
            this.propGeneration(child);
        });
    }
    classNames() {
        let className = `pds-tabs`;
        if (this.variant && this.variant != 'primary') {
            const variantClassName = `pds-tabs--${this.variant}`;
            className += ' ' + variantClassName;
        }
        return className;
    }
    ;
    componentWillLoad() {
        this.findAllChildren();
    }
    componentWillRender() {
        this.passPropsToChildren();
    }
    render() {
        return (h(Host, { key: '213dfbe8f2828b430ab81dce23f43c2137206861', "active-tab-name": this.activeTabName, class: this.classNames(), id: this.componentId }, h("div", { key: '68989e852e2eeed180f91e5ac901c4318d6c0d70', class: "pds-tabs__tablist", role: "tablist", "aria-label": this.tablistLabel, part: "tab-list" }, h("slot", { key: '4a06996745c049fdaf830460e8d57caefd50900b', name: "tabs" })), h("slot", { key: 'e0558392bff0955defb133b36dd4e7360f33acce', name: "tabpanels" })));
    }
    static get is() { return "pds-tabs"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["pds-tabs.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["pds-tabs.css"]
        };
    }
    static get properties() {
        return {
            "tablistLabel": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Sets the aria-label attached to the tablist element"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "tablist-label"
            },
            "componentId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "A unique identifier used for the underlying component `id` attribute."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "component-id"
            },
            "variant": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'primary' | 'availability' | 'filter' | 'pill'",
                    "resolved": "\"availability\" | \"filter\" | \"pill\" | \"primary\"",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Sets tabs variant styles as outlined in Figma documentation"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "variant"
            },
            "activeTabName": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Sets the starting active tab name and maintains the name as the component re-renders"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "active-tab-name"
            },
            "activeTabIndex": {
                "type": "number",
                "mutable": true,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "internal",
                            "text": undefined
                        }],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "active-tab-index"
            }
        };
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "pdsTabClick",
                "method": "tabClickHandler",
                "target": "body",
                "capture": false,
                "passive": false
            }, {
                "name": "keydown",
                "method": "handleKeyDown",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
//# sourceMappingURL=pds-tabs.js.map
