import { waitForUpdatesAsync } from '../../../testing/async-helpers';
import { waitTimeout } from '../../../utilities/testing/component';
/**
 * Page object for the `nimble-tabs` and `nimble-anchor-tabs` components to provide
 * consistent ways of querying and interacting with the component during tests.
 */
export class TabsBasePageObject {
    constructor(tabsElement, tabElementName, tabPanelElementName) {
        this.tabsElement = tabsElement;
        this.tabElementName = tabElementName;
        this.tabPanelElementName = tabPanelElementName;
    }
    async clickTab(index) {
        if (index >= this.tabsElement.tabs.length) {
            throw new Error(`Tab with index ${index} not found`);
        }
        this.tabsElement.tabs[index].click();
        await waitForUpdatesAsync();
    }
    async pressKeyOnTab(index, key) {
        if (index >= this.tabsElement.tabs.length) {
            throw new Error(`Tab with index ${index} not found`);
        }
        const tab = this.tabsElement.tabs[index];
        tab.dispatchEvent(new KeyboardEvent('keydown', { key }));
        await waitForUpdatesAsync();
    }
    async setTabsWidth(width) {
        this.tabsElement.style.width = `${width}px`;
        await waitForUpdatesAsync();
        await waitForUpdatesAsync(); // wait for the resize observer to fire
    }
    async clickScrollLeftButton() {
        const leftButton = this.tabsElement.shadowRoot.querySelector('.scroll-button.left');
        if (!leftButton) {
            throw new Error('Scroll left button not found');
        }
        leftButton.click();
        await waitForUpdatesAsync();
        await waitTimeout(50); // let animation run
    }
    async clickScrollRightButton() {
        const rightButton = this.tabsElement.shadowRoot.querySelector('.scroll-button.right');
        if (!rightButton) {
            throw new Error('Scroll right button not found');
        }
        rightButton.click();
        await waitForUpdatesAsync();
        await waitTimeout(50); // let animation run
    }
    areScrollButtonsVisible() {
        return (this.tabsElement.shadowRoot.querySelectorAll('.scroll-button')
            .length > 0);
    }
    async addTab(label) {
        const tab = document.createElement(this.tabElementName);
        tab.textContent = label;
        const lastTab = this.tabsElement.querySelector(`${this.tabElementName}:last-of-type`);
        lastTab.insertAdjacentElement('afterend', tab);
        if (this.tabPanelElementName) {
            const tabPanel = document.createElement(this.tabPanelElementName);
            const lastTabPanel = this.tabsElement.querySelector(`${this.tabPanelElementName}:last-of-type`);
            lastTabPanel.insertAdjacentElement('afterend', tabPanel);
        }
        await waitForUpdatesAsync();
    }
    async removeTab(index) {
        if (index >= this.tabsElement.tabs.length) {
            throw new Error(`Tab with index ${index} not found`);
        }
        const tabToRemove = this.tabsElement.tabs[index];
        tabToRemove?.remove();
        if (this.tabPanelElementName) {
            const tabPanelToRemove = this.tabsElement.querySelectorAll(this.tabPanelElementName)[index];
            tabPanelToRemove?.remove();
        }
        await waitForUpdatesAsync();
    }
    async updateTabLabel(index, label) {
        if (index >= this.tabsElement.tabs.length) {
            throw new Error(`Tab with index ${index} not found`);
        }
        const tab = this.tabsElement.tabs[index];
        tab.textContent = label;
        await waitForUpdatesAsync();
    }
    getTabsViewScrollOffset() {
        return this.tabsElement.shadowRoot.querySelector('.tablist')
            .scrollLeft;
    }
}
//# sourceMappingURL=tabs-base.pageobject.js.map