import { waitFor } from '@wix/wix-ui-test-utils/unidriver';
import { baseUniDriverFactory, isSled2, isSled3 } from '../../unidriver';
import { EmptyStateUniDriver, FloatingNotificationUniDriver, SelectorListUniDriver, SelectorUniDriver, } from '@wix/design-system/dist/testkit/unidriver';
import { PickerTableListItemUniDriver } from '../PickerTableListItem/PickerTableListItem.uni.driver';
export function PickerUniDriver(base, body) {
    const selectorList = SelectorListUniDriver(base, body);
    const getEmptyState = async (factory) => factory(await base.getNative());
    const getSelectors = () => base
        .$('[data-hook="selector-list-content"]')
        .$$('[data-hook="selector-list-selector"]');
    const selectorDriverAt = (i) => SelectorUniDriver(getSelectors().get(i), body);
    const tableListItemDriverAt = (i) => PickerTableListItemUniDriver(getSelectors().get(i), body);
    const getSearchLoader = () => base.$('[data-hook="search-loader"]');
    const getFloatingNotification = () => FloatingNotificationUniDriver(base.$('[data-hook="floating-notification"]'), body);
    const getPrimaryActionButtonAt = (i) => getSelectors().get(i).$('[data-hook="primary-action-button"]');
    return {
        ...baseUniDriverFactory(base),
        wait: (timeout) => base.wait(timeout),
        waitSearchLoader: (timeout) => getSearchLoader().wait(timeout),
        searchLoaderExists: () => getSearchLoader().exists(),
        waitSearchLoaderRemoved: (timeout) => waitFor(async () => !(await getSearchLoader().exists()), timeout),
        numberOfItemsInList: () => getSelectors().count(),
        primaryButtonExistsAt: (i) => getPrimaryActionButtonAt(i).exists(),
        getPrimaryActionButtonAt,
        scrollDown: async () => {
            const element = await base.getNative();
            // puppeteer
            if (isSled2(element)) {
                const { page } = element;
                await page.evaluate(() => {
                    const e = document.querySelector('[data-hook="selector-list-content"]');
                    e.scrollBy({ behavior: 'smooth', top: e.scrollHeight });
                });
            }
            // playwright — run the same browser-side scroll via the locator's page
            else if (isSled3(element)) {
                const locator = element;
                await locator.page().evaluate(() => {
                    const e = document.querySelector('[data-hook="selector-list-content"]');
                    e.scrollBy({ behavior: 'smooth', top: e.scrollHeight });
                });
            }
            // jsdom
            else {
                await selectorList.scrollDown();
            }
        },
        scrollTo: (i) => getSelectors().get(i).scrollIntoView?.(),
        toggleSelectorAt: selectorList.toggleSelectorAt,
        getTotalSelected: async () => {
            const checked = await Promise.all(await getSelectors().map((selector) => SelectorUniDriver(selector, body).isChecked()));
            return checked.filter(Boolean).length;
        },
        isSelectorCheckedAt: selectorList.isSelectorCheckedAt,
        isListItemDisabledAt: (i) => selectorDriverAt(i).isDisabled(),
        waitListItemAt: async (i, timeout) => waitFor(() => selectorDriverAt(i).exists(), timeout),
        getTableListItemAt: (i) => tableListItemDriverAt(i),
        waitRowEntered: (i, timeout) => base
            .$('[data-hook="selector-list-content"]')
            .$$('[data-hook="picker-list-fade-item"].fade-entered')
            .get(i)
            .wait(timeout),
        hoverItemAt: (i) => getSelectors().get(i).hover(),
        getTitleTextAt: async (i) => selectorDriverAt(i).titleTextDriver().getText(),
        isIndeterminateAt: async (i) => selectorDriverAt(i).isIndeterminate(),
        getTitleTextContentAt: async (i) => getSelectors().get(i).$('[data-hook="selector-title"]').text(),
        getSubtitleTextAt: async (i) => selectorDriverAt(i).subtitleTextDriver().getText(),
        getSubtitleTextContentAt: async (i) => getSelectors().get(i).$('[data-hook="selector-subtitle"]').text(),
        getExtraNodeElementAt: (i) => selectorDriverAt(i).getExtraNode(),
        getImageElementAt: (i) => selectorDriverAt(i).getImage(),
        getToggleTooltipTextAt: (i) => selectorDriverAt(i).getTooltipText(),
        floatingNotificationExists: () => getFloatingNotification().exists(),
        getFloatingNotificationText: () => getFloatingNotification().getText(),
        getEmptyState,
        getEmptyStateByDataHook: (dataHook = 'picker-empty-state') => EmptyStateUniDriver(base.$(`[data-hook="${dataHook}"]`), body),
        getErrorStateByDataHook: (dataHook) => EmptyStateUniDriver(base.$(`[data-hook="${dataHook}"]`), body),
    };
}
//# sourceMappingURL=Picker.uni.driver.js.map