import { getElement, baseUniDriverFactory, } from '../utils/test-utils/unidriver';
import { dataTablePrivateUniDriverFactory } from './DataTable/DataTable.private.uni.driver';
import { checkboxUniDriverFactory } from '../Checkbox/Checkbox.uni.driver';
import { FloatingScrollBarUniDriver } from './FloatingScrollBar/FloatingScrollBar.uni.driver';
export const tableUniDriverFactory = (base, body) => {
    const dataTableDriver = dataTablePrivateUniDriverFactory(base);
    const getRowNumberCell = async (index) => {
        const row = await dataTableDriver.getRow(index);
        const cell = await row.$('[data-hook="bulk-selection-cell"]');
        return cell.$('[data-hook="row-number-cell"]');
    };
    const getRowNumberText = async (index) => {
        const cell = await getRowNumberCell(index);
        return cell.$('[data-hook="row-number-text"]');
    };
    const getRowCheckbox = async (index) => {
        const row = await dataTableDriver.getRow(index);
        const cell = await row.$('[data-hook="bulk-selection-cell"]');
        return cell.$('[data-hook="row-select"]');
    };
    const getRowCheckboxDriver = async (index) => checkboxUniDriverFactory(await getRowCheckbox(index), body);
    const getBulkSelectionCheckboxDriver = async () => {
        const cell = await dataTableDriver.getHeaderCellByDataHook('bulk-selection-cell');
        return checkboxUniDriverFactory(await cell.$('[data-hook="table-select"]'));
    };
    const isBulkSelectionChecked = async () => {
        const checkboxDriver = await getBulkSelectionCheckboxDriver();
        return ((await checkboxDriver.isChecked()) &&
            !(await checkboxDriver.isIndeterminate()));
    };
    const isBulkSelectionIndeterminate = async () => {
        const checkboxDriver = await getBulkSelectionCheckboxDriver();
        return (!(await checkboxDriver.isChecked()) &&
            (await checkboxDriver.isIndeterminate()));
    };
    const isBulkSelectionUnchecked = async () => {
        const checkboxDriver = await getBulkSelectionCheckboxDriver();
        return (!(await checkboxDriver.isChecked()) &&
            !(await checkboxDriver.isIndeterminate()));
    };
    return {
        ...baseUniDriverFactory(base),
        ...dataTableDriver,
        /** Get driver of row selection checkbox by row index */
        getRowCheckboxDriver,
        /** Get driver of row bulk-selection checkbox */
        getBulkSelectionCheckboxDriver,
        /** Whether bulk selection checkbox is disabled */
        isBulkSelectionDisabled: async () => (await getBulkSelectionCheckboxDriver()).isDisabled(),
        /** Whether specific row selection checkbox is disabled */
        isRowSelectionDisabled: async (index) => (await getRowCheckboxDriver(index)).isDisabled(),
        /** Click the row selection checkbox */
        clickRowCheckbox: async (index) => (await getRowCheckbox(index)).click(),
        /** Click the bulk-selection checkbox */
        clickBulkSelectionCheckbox: async () => (await getBulkSelectionCheckboxDriver()).click(),
        /** Is row selected by index */
        isRowSelected: async (index) => (await getRowCheckboxDriver(index)).isChecked(),
        getBulkSelectionState: async () => {
            if (await isBulkSelectionChecked()) {
                return 'ALL';
            }
            if (await isBulkSelectionIndeterminate()) {
                return 'SOME';
            }
            if (await isBulkSelectionUnchecked()) {
                return 'NONE';
            }
        },
        /** Get title-bar (column titles) */
        getTitlebar: () => getElement(base.$('[data-hook="table-title-bar"]')),
        getCellTextValue: async (row = 0, column = 0) => (await dataTableDriver.getCell(row, column))._prop('textContent'),
        isSelectionTooltipEnabled: async (index) => (await getRowCheckboxDriver(index)).isTooltipEnabled(),
        getSelectionTooltipContent: async (index) => (await getRowCheckboxDriver(index)).getTooltipContent(),
        /** Get the row number cell wrapper element by row index */
        getRowNumberCell: async (index) => getRowNumberCell(index),
        /** Get the row number text value by row index */
        getRowNumberText: async (index) => (await getRowNumberText(index))._prop('textContent'),
        /** Whether row number text is visible for a row */
        hasRowNumberText: async (index) => (await getRowNumberText(index)).exists(),
        /** Whether row number cell exists for a row */
        hasRowNumberCell: async (index) => (await getRowNumberCell(index)).exists(),
        getFloatingScrollBarDriver: (viewportElement = body) => FloatingScrollBarUniDriver(viewportElement.$('[data-hook="table-floating-scroll-bar"]'), body),
    };
};
//# sourceMappingURL=Table.uni.driver.js.map