"use strict";
// © 2022 SolarWinds Worldwide, LLC. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to
//  deal in the Software without restriction, including without limitation the
//  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
//  sell copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
Object.defineProperty(exports, "__esModule", { value: true });
exports.TableAtom = void 0;
const tslib_1 = require("tslib");
const isNil_1 = tslib_1.__importDefault(require("lodash/isNil"));
const protractor_1 = require("protractor");
const atom_1 = require("../../atom");
const checkbox_atom_1 = require("../checkbox/checkbox.atom");
const icon_atom_1 = require("../icon/icon.atom");
const selector_atom_1 = require("../selector/selector.atom");
class TableAtom extends atom_1.Atom {
    constructor() {
        super(...arguments);
        this.getRow = (rowIndex) => this.getElement().all(protractor_1.by.tagName("tr")).get(rowIndex);
        /**
         * Check whether a row is selected
         */
        this.isRowSelected = (rowIndex) => tslib_1.__awaiter(this, void 0, void 0, function* () { return atom_1.Atom.hasClass(this.getRow(rowIndex), "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 = (rowIndex) => tslib_1.__awaiter(this, void 0, void 0, function* () { return atom_1.Atom.hasClass(this.getRow(rowIndex), "nui-table__table-row--clickable"); });
        /**
         *
         * @param {number} rowIndex = 0 stands for header row
         * @param {number} cellIndex
         * @returns {Promise<string>}
         */
        this.getCellText = (rowIndex, cellIndex) => tslib_1.__awaiter(this, void 0, void 0, function* () { return this.getCell(rowIndex, cellIndex).getText(); });
        /**
         * Gets all the resizers of table
         * @returns {ElementArrayFinder}
         */
        this.getResizers = () => this.getElement().all(protractor_1.by.css(".nui-table__resizer"));
        /**
         * Gets all header cells of the table
         * @returns {ElementArrayFinder}
         */
        this.getHeaderCells = () => this.getRow(0).all(protractor_1.by.tagName("th"));
        this.getSortingIcon = (headerCell) => atom_1.Atom.findIn(icon_atom_1.IconAtom, headerCell);
        this.getHeaderCellsWithIcon = () => this.getRow(0).all(protractor_1.by.css(".nui-table__icon-cell"));
        this.getCheckbox = (element) => atom_1.Atom.findIn(checkbox_atom_1.CheckboxAtom, element);
        this.getSelector = (element) => atom_1.Atom.findIn(selector_atom_1.SelectorAtom, element);
        this.isSortingIconDisplayed = (headerCell) => tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield atom_1.Atom.findCount(icon_atom_1.IconAtom, headerCell)) > 0; });
    }
    getColumn(name) {
        return this.getElement().element(protractor_1.by.cssContainingText("th", name));
    }
    /**
     *
     * @param {number} rowIndex = 0 stands for header row
     * @param {number} cellIndex
     * @returns {ElementFinder}
     */
    getCell(rowIndex, cellIndex) {
        const tableRow = this.getRow(rowIndex);
        if (rowIndex === 0) {
            return tableRow.all(protractor_1.by.tagName("th")).get(cellIndex);
        }
        return tableRow.all(protractor_1.by.tagName("td")).get(cellIndex);
    }
    getRowContent(rowIndex) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const tableRow = this.getRow(rowIndex);
            const tableCellTag = rowIndex === 0 ? "th" : "td";
            return tableRow
                .all(protractor_1.by.tagName(tableCellTag))
                .map((elem) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                if (!elem) {
                    throw new Error("elem is not defined");
                }
                return elem.getText();
            }));
        });
    }
    getRowsCount() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this.getElement()
                .all(protractor_1.by.tagName("tr"))
                .count()
                .then((value) => value - 1); // -1 because we don't need to count header row
        });
    }
    checkSortingIcons(expectedCellWithIconIndex) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const missedExpectations = [];
            const tableHeaderRow = this.getRow(0);
            yield tableHeaderRow
                .all(protractor_1.by.tagName("th"))
                .each((headerCell, headerCellIndex) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                if (!headerCell) {
                    throw new Error("headerCell is not defined");
                }
                const isIconDisplayed = yield this.isSortingIconDisplayed(headerCell);
                if (headerCellIndex === expectedCellWithIconIndex &&
                    !isIconDisplayed) {
                    missedExpectations.push(`Expected cell with index = ${headerCellIndex} to contain sorting icon`);
                    fail(missedExpectations.join("\n"));
                }
                else if (headerCellIndex !== expectedCellWithIconIndex &&
                    isIconDisplayed) {
                    missedExpectations.push(`Expected cell with index = ${headerCellIndex} to not contain sorting icon`);
                    fail(missedExpectations.join("\n"));
                }
            }));
        });
    }
    /**
     * 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
     */
    checkSelectability(enabled) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let rowsWithCheckboxes = 0;
            const rowCount = yield this.getElement().all(protractor_1.by.tagName("tr")).count();
            yield this.getElement()
                .all(protractor_1.by.tagName("tr"))
                .each((row, rowIndex) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                if (!row || isNil_1.default(rowIndex)) {
                    throw new Error("row is not defined");
                }
                const checkBoxPresent = yield atom_1.Atom.findIn(checkbox_atom_1.CheckboxAtom, this.getCell(rowIndex, 0)).isPresent();
                if (checkBoxPresent) {
                    rowsWithCheckboxes++;
                }
            }));
            return rowsWithCheckboxes === (enabled ? rowCount : 0);
        });
    }
    /**
     * 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 enabled Pass 'true' if you want to check whether clicking to select is enabled for all body rows.
     * Pass 'false' to check whether clicking to select is disabled for all body rows.
     *
     * @returns The aggregate clickability status for all body rows
     */
    checkRowClickability(enabled) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let clickableRows = 0;
            const rowCount = yield this.getRowsCount();
            yield this.getElement()
                .all(protractor_1.by.tagName("tr"))
                .each((row, index) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                if (!row || isNil_1.default(index)) {
                    throw new Error("row is not defined");
                }
                // index >= 1 to skip header row
                if (index >= 1) {
                    const clickable = yield this.isRowClickable(index);
                    if (clickable) {
                        clickableRows++;
                    }
                }
            }));
            return clickableRows === (enabled ? rowCount : 0);
        });
    }
    /**
     * Checks if all checkboxes in all rows selected
     */
    isAllRowsSelected() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let failedTestsCount = 0;
            yield this.getElement()
                .all(protractor_1.by.tagName("tr"))
                .each((row, index) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                if (!row || isNil_1.default(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));
                    const isChecked = yield checkBox.isChecked();
                    if (isChecked !== true) {
                        failedTestsCount++;
                    }
                }
            }));
            return failedTestsCount === 0;
        });
    }
}
exports.TableAtom = TableAtom;
TableAtom.CSS_CLASS = "nui-table__table";
//# sourceMappingURL=table.atom.js.map