// eslint-disable-next-line no-restricted-imports
import { isNil } from "lodash";
import { Atom } from "../../atom";
import { expect } from "../../setup";
import { CheckboxAtom } from "../checkbox/checkbox.atom";
import { IconAtom } from "../icon/icon.atom";
import { SelectorAtom } from "../selector/selector.atom";
export class TableAtom extends Atom {
    constructor() {
        super(...arguments);
        this.getRow = (rowIndex) => this.getLocator().locator("tr").nth(rowIndex);
        /**
         * Check whether a row is selected
         */
        this.isRowSelected = async (rowIndex) => {
            await expect(this.getRow(rowIndex)).toContainClass("nui-table__table-row--selected");
        };
        /**
         * Check whether any part of a row can be clicked to make a selection (not just the checkbox)
         */
        this.isRowClickable = async (rowIndex) => {
            await expect(this.getRow(rowIndex)).toContainClass("nui-table__table-row--clickable");
        };
        /**
         * Check whether any part of a row can be clicked to make a selection (not just the checkbox)
         */
        this.isNotRowClickable = async (rowIndex) => {
            await expect(this.getRow(rowIndex)).not.toContainClass("nui-table__table-row--clickable");
        };
        /**
         *
         * @deprecated
         * @param {number} rowIndex = 0 stands for header row
         * @param {number} cellIndex
         * @returns {Promise<string>}
         */
        this.getCellText = async (rowIndex, cellIndex) => this.getCell(rowIndex, cellIndex).textContent();
        /**
         * Gets all the resizers of table
         * @returns {Locator}
         */
        this.getResizers = () => this.getLocator().locator(".nui-table__resizer");
        /**
         * Gets all header cells of the table
         * @returns {Locator}
         */
        this.getHeaderCells = () => this.getRow(0).locator("th");
        this.getHeaderCellsWithIcon = () => this.getRow(0).locator(".nui-table__icon-cell");
        this.getCheckbox = (element) => Atom.findIn(CheckboxAtom, element);
        this.getSelector = (element) => Atom.findIn(SelectorAtom, element);
        this.isSortingIconDisplayed = async (headerCell, msg) => {
            expect(await headerCell.count(), msg).toBeGreaterThan(0);
        };
        this.isNotSortingIconDisplayed = async (headerCell, msg) => {
            expect(await headerCell.count(), msg).not.toBeGreaterThan(0);
        };
    }
    static { this.CSS_CLASS = "nui-table__table"; }
    getColumn(name) {
        return this.getLocator().locator("th").filter({ hasText: name });
    }
    /**
     *
     * @param {number} rowIndex = 0 stands for header row
     * @param {number} cellIndex
     * @returns {Locator}
     */
    getCell(rowIndex, cellIndex) {
        const tableRow = this.getRow(rowIndex);
        if (rowIndex === 0) {
            return tableRow.locator("th").nth(cellIndex);
        }
        return tableRow.locator("td").nth(cellIndex);
    }
    async getRowContent(rowIndex) {
        const tableRow = this.getRow(rowIndex);
        const tableCellTag = rowIndex === 0 ? "th" : "td";
        return tableRow.locator(tableCellTag).allTextContents();
    }
    /*
     * @deprecated use haveCount
     */
    async getRowsCount() {
        return this.getLocator()
            .locator("tr")
            .count()
            .then((value) => value - 1); // -1 because we don't need to count header row
    }
    getSortingIcon(headerCell) {
        return Atom.findIn(IconAtom, headerCell);
    }
    async checkSortingIcons(expectedCellWithIconIndex) {
        const tableHeaderRow = this.getRow(0);
        const cell = tableHeaderRow
            .locator("th")
            .nth(expectedCellWithIconIndex);
        await this.isSortingIconDisplayed(cell, `Expected cell with index = ${expectedCellWithIconIndex} to contain sorting icon`);
    }
    /**
     * Use this method to check whether selection is enabled or disabled for all rows including the header
     *
     * @param enabled Pass 'true' if you want to check whether selection is enabled for all rows including the header.
     * Pass 'false' to check whether selection is disabled for all rows including the header.
     *
     * @returns The aggregate selectability status for all rows
     */
    async checkSelectability(enabled) {
        let selectionValidationPassed = 0;
        const rowCount = await this.getLocator().locator("tr").count();
        const rows = await this.getLocator().locator("tr").all();
        for (const row of rows) {
            const rowIndex = rows.indexOf(row);
            if (!row || isNil(rowIndex)) {
                throw new Error("row is not defined");
            }
            const cell = this.getCell(rowIndex, 0);
            if (enabled) {
                await Atom.findIn(CheckboxAtom, cell).toBeVisible();
            }
            else {
                await Atom.findIn(CheckboxAtom, cell).toBeHidden();
            }
            selectionValidationPassed++;
        }
        return selectionValidationPassed === rowCount;
    }
    /**
     * Use this method to check whether selection by clicking on a row is enabled or disabled for all body rows.
     * (When row clicking is enabled, the user doesn't have to specifically click the checkbox in order to select the row.)
     * @param checker
     * the function to check if the row has some expectation
     * Pass 'false' to check whether clicking to select is disabled for all body rows.
     *
     * @returns The aggregate clickability status for all body rows
     */
    async checkRow(checker = this.isRowClickable) {
        const rows = await this.getLocator().locator("tr").all();
        for (const row of rows) {
            const index = rows.indexOf(row);
            if (!row || isNil(index)) {
                throw new Error("row is not defined");
            }
            // index >= 1 to skip header row
            if (index >= 1) {
                await checker(index);
            }
        }
    }
    async checkRowClickability(all) {
        await this.checkRow(all ? this.isRowClickable : this.isNotRowClickable);
    }
    /**
     * Checks if all checkboxes in all rows selected
     */
    async isAllRowsSelected() {
        const rows = await this.getLocator().locator("tr").all();
        for (const row of rows) {
            const index = rows.indexOf(row);
            if (!row || isNil(index)) {
                throw new Error("row is not defined");
            }
            // index >= 1 to skip header row
            if (index >= 1) {
                const checkBox = this.getCheckbox(this.getCell(index, 0));
                await checkBox.toBeChecked();
            }
        }
        return true;
    }
    /**
     * Checks if all checkboxes in all rows selected
     */
    async isAllRowsNotSelected() {
        const rows = await this.getLocator().locator("tr").all();
        for (const row of rows) {
            const index = rows.indexOf(row);
            if (!row || isNil(index)) {
                throw new Error("row is not defined");
            }
            // index >= 1 to skip header row
            if (index >= 1) {
                const checkBox = this.getCheckbox(this.getCell(index, 0));
                await checkBox.toNotBeChecked();
            }
        }
        return true;
    }
}
//# sourceMappingURL=table.atom.js.map