"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
const selectors_1 = require("@cloudscape-design/test-utils-core/selectors");
const collection_preferences_1 = require("../collection-preferences");
const container_1 = require("../container");
const pagination_1 = require("../pagination");
const property_filter_1 = require("../property-filter");
const text_filter_1 = require("../text-filter");
const styles_selectors_js_1 = require("../../../internal/components/expand-toggle-button/styles.selectors.js");
const styles_selectors_js_2 = require("../../../table/body-cell/styles.selectors.js");
const styles_selectors_js_3 = require("../../../table/header-cell/styles.selectors.js");
const styles_selectors_js_4 = require("../../../table/progressive-loading/styles.selectors.js");
const styles_selectors_js_5 = require("../../../table/resizer/styles.selectors.js");
const styles_selectors_js_6 = require("../../../table/selection/styles.selectors.js");
const styles_selectors_js_7 = require("../../../table/styles.selectors.js");
const styles_selectors_js_8 = require("../../../table/test-classes/styles.selectors.js");
class TableWrapper extends selectors_1.ComponentWrapper {
    constructor() {
        super(...arguments);
        this.containerWrapper = new container_1.default(this.getElement());
    }
    findNativeTable() {
        return this.find(`.${styles_selectors_js_7.default.wrapper} > .${styles_selectors_js_7.default.table}`);
    }
    findActiveTHead() {
        return this.findByClassName(styles_selectors_js_7.default['thead-active']);
    }
    findHeaderSlot() {
        return this.findByClassName(styles_selectors_js_7.default['header-controls']);
    }
    /**
     * Alias for findHeaderSlot method for compatibility with previous versions
     * @deprecated
     */
    findHeaderRegion() {
        return this.findHeaderSlot();
    }
    findFooterSlot() {
        return this.containerWrapper.findFooter();
    }
    /**
     * Returns column header cells from the table's header region.
     *
     * By default, returns all leaf-column headers (`scope="col"`).
     * For tables without column grouping this is equivalent to the previous behavior.
     * For tables with column grouping this excludes group header cells.
     *
     * @param option.groupId When provided, returns only leaf columns belonging to this group
     *   (matched via `data-column-group-id` attribute).
     */
    findColumnHeaders(option = {}) {
        const { groupId } = option;
        if (groupId !== undefined) {
            return this.findActiveTHead().findAll(`th[data-column-group-id="${groupId}"]`);
        }
        return this.findActiveTHead().findAll('tr > *');
    }
    /**
     * Returns the element the user clicks when resizing a column.
     *
     * @param columnIndex 1-based index of the column containing the resizer.
     */
    findColumnResizer(columnIndex) {
        return this.findActiveTHead().find(`th[data-column-index="${columnIndex}"] .${styles_selectors_js_5.default.resizer}, tr:not([data-group-level]) > th:nth-child(${columnIndex}) .${styles_selectors_js_5.default.resizer}`);
    }
    /**
     * Returns a table cell based on given row and column indices.
     *
     * @param rowIndex 1-based index of the row of the cell to select.
     * @param columnIndex 1-based index of the column of the cell to select.
     */
    findBodyCell(rowIndex, columnIndex) {
        return this.findNativeTable().find(`tbody tr:nth-child(${rowIndex}) .${styles_selectors_js_2.default['body-cell']}:nth-child(${columnIndex})`);
    }
    /**
     * Returns a table cell counter, if defined, based on given row and column indices.
     *
     * @param rowIndex 1-based index of the row of the cell to select.
     * @param columnIndex 1-based index of the column of the cell to select.
     */
    findBodyCellCounter(rowIndex, columnIndex) {
        var _a, _b;
        return (_b = (_a = this.findBodyCell(rowIndex, columnIndex)) === null || _a === void 0 ? void 0 : _a.find(`.${styles_selectors_js_8.default['body-cell-counter']}`)) !== null && _b !== void 0 ? _b : null;
    }
    findRows() {
        return this.findNativeTable().findAllByClassName(styles_selectors_js_7.default.row);
    }
    findSelectedRows() {
        return this.findAllByClassName(styles_selectors_js_7.default['row-selected']);
    }
    /**
     * Alias for findEmptySlot method for compatibility with previous versions
     * @deprecated
     */
    findEmptyRegion() {
        return this.findEmptySlot();
    }
    findEmptySlot() {
        return this.findByClassName(styles_selectors_js_7.default.empty);
    }
    findLoadingText() {
        return this.findByClassName(styles_selectors_js_7.default.loading);
    }
    /**
     * Returns the clickable sorting area of a column header.
     *
     * @param colIndex 1-based index of the column.
     */
    findColumnSortingArea(colIndex) {
        return this.findActiveTHead().find(`th[data-column-index="${colIndex}"] [role=button], tr:not([data-group-level]) > *:nth-child(${colIndex}) [role=button]`);
    }
    /**
     * Returns the column that is used for ascending sorting.
     */
    findAscSortedColumn() {
        return this.findNativeTable().findByClassName(styles_selectors_js_3.default['header-cell-ascending']);
    }
    /**
     * Returns the column that is used for descending sorting.
     */
    findDescSortedColumn() {
        return this.findNativeTable().findByClassName(styles_selectors_js_3.default['header-cell-descending']);
    }
    /**
     * Returns a row selection area for a given index.
     *
     * @param rowIndex 1-based index of the row selection area to return.
     */
    findRowSelectionArea(rowIndex) {
        return this.findNativeTable().find(`tbody tr:nth-child(${rowIndex}) .${styles_selectors_js_6.default.root}`);
    }
    findSelectAllTrigger() {
        return this.findActiveTHead().find(`.${styles_selectors_js_6.default.root}`);
    }
    findTextFilter() {
        return this.findComponent(`.${styles_selectors_js_7.default['tools-filtering']}`, text_filter_1.default);
    }
    findPropertyFilter() {
        return this.findComponent(`.${styles_selectors_js_7.default['tools-filtering']}`, property_filter_1.default);
    }
    findFilterSlot() {
        return this.findComponent(`.${styles_selectors_js_7.default['tools-filtering']}`, selectors_1.ElementWrapper);
    }
    findCollectionPreferences() {
        return this.findComponent(`.${styles_selectors_js_7.default['tools-preferences']}`, collection_preferences_1.default);
    }
    findPagination() {
        return this.findComponent(`.${styles_selectors_js_7.default['tools-pagination']}`, pagination_1.default);
    }
    /**
     * Returns the button that activates inline editing for a table cell based on given row and column indices.
     *
     * @param rowIndex 1-based index of the row of the cell to select.
     * @param columnIndex 1-based index of the column of the cell to select.
     */
    findEditCellButton(rowIndex, columnIndex) {
        var _a, _b;
        return (_b = (_a = this.findBodyCell(rowIndex, columnIndex)) === null || _a === void 0 ? void 0 : _a.findByClassName(styles_selectors_js_2.default['body-cell-editor'])) !== null && _b !== void 0 ? _b : null;
    }
    findEditingCell() {
        return this.findNativeTable().findByClassName(styles_selectors_js_2.default['body-cell-edit-active']);
    }
    _findEditingCellControls() {
        var _a, _b;
        return (_b = (_a = this.findEditingCell()) === null || _a === void 0 ? void 0 : _a.findByClassName(styles_selectors_js_2.default['body-cell-editor-controls'])) !== null && _b !== void 0 ? _b : null;
    }
    findEditingCellSaveButton() {
        var _a, _b;
        return (_b = (_a = this._findEditingCellControls()) === null || _a === void 0 ? void 0 : _a.find('button[type="submit"]')) !== null && _b !== void 0 ? _b : null;
    }
    findEditingCellCancelButton() {
        var _a, _b;
        return (_b = (_a = this._findEditingCellControls()) === null || _a === void 0 ? void 0 : _a.find('button:first-child')) !== null && _b !== void 0 ? _b : null;
    }
    /**
     * Returns the expandable row toggle button.
     *
     * @param rowIndex 1-based index of the row.
     */
    findExpandToggle(rowIndex) {
        // the file was moved, which changed the hash, so we cannot use old test class anymore.
        const oldSelector = `tbody tr:nth-child(${rowIndex}) .awsui_expand-toggle_1ss49_1w02f_153`;
        const newSelector = `tbody tr:nth-child(${rowIndex}) .${styles_selectors_js_1.default['expand-toggle']}`;
        return this.findNativeTable().findAny(oldSelector, newSelector);
    }
    /**
     * Returns `true` if the row expand toggle is present and expanded. Returns `false` otherwise.
     *
     * @param rowIndex 1-based index of the row.
     */
    /**
     * Returns items loader of the root table level.
     */
    findRootItemsLoader() {
        const selector = `.${styles_selectors_js_4.default['items-loader']}[data-root="true"]`;
        return this.find(selector);
    }
    /**
     * Returns items loader of the specific item (matched by item's track ID).
     *
     * @param itemId the (expandable) item ID provided with `trackBy` property.
     *
     * Note: when used with collection-hooks the `trackBy` is set automatically from `expandableRows.getId`.
     */
    findItemsLoaderByItemId(itemId) {
        const selector = `.${styles_selectors_js_4.default['items-loader']}[data-parentrow="${itemId}"]`;
        return this.find(selector);
    }
}
TableWrapper.rootSelector = styles_selectors_js_7.default.root;
exports.default = TableWrapper;
//# sourceMappingURL=index.js.map