export class Tabs {
    componentDidLoad() {
        this.createGroup();
        const [group] = this.tabGroup;
        this.selectGroup(group);
        if (this.selectedIndex)
            this.activeIndex = Number(this.selectedIndex) - 1;
    }
    componentDidUpdate() {
        if (this.selectedIndex)
            this.activeIndex = Number(this.selectedIndex) - 1;
    }
    onSelectedTab(event) {
        const group = this.tabGroup.find(group => group.header.id === event.detail.id);
        this.selectGroup(group);
    }
    createGroup() {
        const tabsHeaderEl = Array.from(this.host.querySelectorAll('ema-tab-header'));
        this.tabsHeader = tabsHeaderEl.map(el => el.getChild());
        const tabsContentEl = Array.from(this.host.querySelectorAll('ema-tab-content'));
        this.tabsContent = tabsContentEl.map(el => el.getChild());
        this.tabGroup = this.tabsHeader.map(header => {
            const content = this.tabsContent.find(content => content.name === header.name);
            return {
                header: header,
                content: content
            };
        });
    }
    selectGroup(group) {
        this.tabGroup.forEach(_ => {
            _.header.unselect();
            _.content.unselect();
        });
        group.header.select();
        group.content.select();
    }
    set activeIndex(index) {
        let i = 0;
        this.tabGroup.forEach(_ => {
            if (i == index) {
                _.header.select();
                _.content.select();
            }
            else {
                _.header.unselect();
                _.content.unselect();
            }
            i++;
        });
    }
    render() {
        return [
            h("div", { class: "ema-tabs-header" },
                h("slot", { name: "header" })),
            h("div", { class: "ema-tabs-content" },
                h("slot", { name: "content" }))
        ];
    }
    static get is() { return "ema-tabs"; }
    static get properties() { return {
        "host": {
            "elementRef": true
        },
        "selectedIndex": {
            "type": Number,
            "attr": "selected-index"
        }
    }; }
    static get listeners() { return [{
            "name": "onSelect",
            "method": "onSelectedTab"
        }]; }
    static get style() { return "/**style-placeholder:ema-tabs:**/"; }
}
