"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const promises_1 = __importDefault(require("node:fs/promises"));
const node_path_1 = __importDefault(require("node:path"));
const cucumber_1 = require("@cucumber/cucumber");
const ag_grid_1 = require("../runtime/ag-grid");
(0, cucumber_1.Then)('the grid should be visible', async function () {
    await (0, ag_grid_1.assertGridVisible)(requirePage(this));
});
(0, cucumber_1.Then)('the grid {int} should be visible', async function (gridIndex) {
    await (0, ag_grid_1.assertGridVisible)(requirePage(this), gridIndex);
});
(0, cucumber_1.Then)('the {string} grid should be visible', async function (gridName) {
    await (0, ag_grid_1.assertGridVisible)(requirePage(this), resolveGridName(this, gridName));
});
(0, cucumber_1.Then)('the grid should have {int} rows', async function (expectedCount) {
    await (0, ag_grid_1.assertGridRowCount)(requirePage(this), expectedCount);
});
(0, cucumber_1.Then)('the {string} grid should have {int} rows', async function (gridName, expectedCount) {
    await (0, ag_grid_1.assertGridRowCount)(requirePage(this), expectedCount, resolveGridName(this, gridName));
});
(0, cucumber_1.Then)('the grid should have at least {int} row', async function (minimumCount) {
    await (0, ag_grid_1.assertGridMinimumRowCount)(requirePage(this), minimumCount);
});
(0, cucumber_1.Then)('the grid should have at least {int} rows', async function (minimumCount) {
    await (0, ag_grid_1.assertGridMinimumRowCount)(requirePage(this), minimumCount);
});
(0, cucumber_1.Then)('the {string} grid should have at least {int} row', async function (gridName, minimumCount) {
    await (0, ag_grid_1.assertGridMinimumRowCount)(requirePage(this), minimumCount, resolveGridName(this, gridName));
});
(0, cucumber_1.Then)('the {string} grid should have at least {int} rows', async function (gridName, minimumCount) {
    await (0, ag_grid_1.assertGridMinimumRowCount)(requirePage(this), minimumCount, resolveGridName(this, gridName));
});
(0, cucumber_1.Then)('the grid column {string} should be visible', async function (columnName) {
    await (0, ag_grid_1.assertGridColumnVisible)(requirePage(this), columnName);
});
(0, cucumber_1.Then)('the {string} grid column {string} should be visible', async function (gridName, columnName) {
    await (0, ag_grid_1.assertGridColumnVisible)(requirePage(this), columnName, resolveGridName(this, gridName));
});
(0, cucumber_1.Then)('the grid cell in row {int} and column {string} should contain {string}', async function (rowIndex, columnName, expectedText) {
    await assertIndexedCellText(this, rowIndex, columnName, expectedText, 'contain');
});
(0, cucumber_1.Then)('the {string} grid cell in row {int} and column {string} should contain {string}', async function (gridName, rowIndex, columnName, expectedText) {
    await assertIndexedCellText(this, rowIndex, columnName, expectedText, 'contain', resolveGridName(this, gridName));
});
(0, cucumber_1.Then)('the grid cell in row {int} and column {string} should equal {string}', async function (rowIndex, columnName, expectedText) {
    await assertIndexedCellText(this, rowIndex, columnName, expectedText, 'equal');
});
(0, cucumber_1.Then)('the {string} grid cell in row {int} and column {string} should equal {string}', async function (gridName, rowIndex, columnName, expectedText) {
    await assertIndexedCellText(this, rowIndex, columnName, expectedText, 'equal', resolveGridName(this, gridName));
});
(0, cucumber_1.Then)('the grid row where column {string} is {string} should have column {string} containing {string}', async function (lookupColumn, lookupValue, targetColumn, expectedText) {
    await assertMatchedCellText(this, lookupColumn, lookupValue, targetColumn, expectedText, 'contain');
});
(0, cucumber_1.Then)('the {string} grid row where column {string} is {string} should have column {string} containing {string}', async function (gridName, lookupColumn, lookupValue, targetColumn, expectedText) {
    await assertMatchedCellText(this, lookupColumn, lookupValue, targetColumn, expectedText, 'contain', resolveGridName(this, gridName));
});
(0, cucumber_1.Then)('the grid row where column {string} is {string} should have column {string} equal to {string}', async function (lookupColumn, lookupValue, targetColumn, expectedText) {
    await assertMatchedCellText(this, lookupColumn, lookupValue, targetColumn, expectedText, 'equal');
});
(0, cucumber_1.Then)('the {string} grid row where column {string} is {string} should have column {string} equal to {string}', async function (gridName, lookupColumn, lookupValue, targetColumn, expectedText) {
    await assertMatchedCellText(this, lookupColumn, lookupValue, targetColumn, expectedText, 'equal', resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I click on the grid cell in row {int} and column {string}', async function (rowIndex, columnName) {
    await interactWithIndexedCell(this, rowIndex, columnName, 'click');
});
(0, cucumber_1.When)('I click on the {string} grid cell in row {int} and column {string}', async function (gridName, rowIndex, columnName) {
    await interactWithIndexedCell(this, rowIndex, columnName, 'click', resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I double click on the grid cell in row {int} and column {string}', async function (rowIndex, columnName) {
    await interactWithIndexedCell(this, rowIndex, columnName, 'doubleClick');
});
(0, cucumber_1.When)('I double click on the {string} grid cell in row {int} and column {string}', async function (gridName, rowIndex, columnName) {
    await interactWithIndexedCell(this, rowIndex, columnName, 'doubleClick', resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I right click on the grid cell in row {int} and column {string}', async function (rowIndex, columnName) {
    await interactWithIndexedCell(this, rowIndex, columnName, 'rightClick');
});
(0, cucumber_1.When)('I right click on the {string} grid cell in row {int} and column {string}', async function (gridName, rowIndex, columnName) {
    await interactWithIndexedCell(this, rowIndex, columnName, 'rightClick', resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I click on the grid cell where column {string} is {string} in column {string}', async function (lookupColumn, lookupValue, targetColumn) {
    await interactWithMatchedCell(this, lookupColumn, lookupValue, targetColumn, 'click');
});
(0, cucumber_1.When)('I click on the {string} grid cell where column {string} is {string} in column {string}', async function (gridName, lookupColumn, lookupValue, targetColumn) {
    await interactWithMatchedCell(this, lookupColumn, lookupValue, targetColumn, 'click', resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I double click on the grid cell where column {string} is {string} in column {string}', async function (lookupColumn, lookupValue, targetColumn) {
    await interactWithMatchedCell(this, lookupColumn, lookupValue, targetColumn, 'doubleClick');
});
(0, cucumber_1.When)('I double click on the {string} grid cell where column {string} is {string} in column {string}', async function (gridName, lookupColumn, lookupValue, targetColumn) {
    await interactWithMatchedCell(this, lookupColumn, lookupValue, targetColumn, 'doubleClick', resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I right click on the grid cell where column {string} is {string} in column {string}', async function (lookupColumn, lookupValue, targetColumn) {
    await interactWithMatchedCell(this, lookupColumn, lookupValue, targetColumn, 'rightClick');
});
(0, cucumber_1.When)('I right click on the {string} grid cell where column {string} is {string} in column {string}', async function (gridName, lookupColumn, lookupValue, targetColumn) {
    await interactWithMatchedCell(this, lookupColumn, lookupValue, targetColumn, 'rightClick', resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I edit the grid cell in row {int} and column {string} with {string}', async function (rowIndex, columnName, value) {
    const cell = await (0, ag_grid_1.getGridCellByRowIndex)(requirePage(this), rowIndex, columnName);
    await (0, ag_grid_1.editGridCell)(cell, value);
});
(0, cucumber_1.When)('I edit the {string} grid cell in row {int} and column {string} with {string}', async function (gridName, rowIndex, columnName, value) {
    const cell = await (0, ag_grid_1.getGridCellByRowIndex)(requirePage(this), rowIndex, columnName, resolveGridName(this, gridName));
    await (0, ag_grid_1.editGridCell)(cell, value);
});
(0, cucumber_1.When)('I sort the grid column {string} ascending', async function (columnName) {
    await (0, ag_grid_1.sortGridColumn)(requirePage(this), columnName, 'ascending');
});
(0, cucumber_1.When)('I sort the {string} grid column {string} ascending', async function (gridName, columnName) {
    await (0, ag_grid_1.sortGridColumn)(requirePage(this), columnName, 'ascending', resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I sort the grid column {string} descending', async function (columnName) {
    await (0, ag_grid_1.sortGridColumn)(requirePage(this), columnName, 'descending');
});
(0, cucumber_1.When)('I sort the {string} grid column {string} descending', async function (gridName, columnName) {
    await (0, ag_grid_1.sortGridColumn)(requirePage(this), columnName, 'descending', resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I filter the grid column {string} by {string}', async function (columnName, value) {
    await (0, ag_grid_1.filterGridColumn)(requirePage(this), columnName, value);
});
(0, cucumber_1.When)('I filter the {string} grid column {string} by {string}', async function (gridName, columnName, value) {
    await (0, ag_grid_1.filterGridColumn)(requirePage(this), columnName, value, resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I clear the grid column {string} filter', async function (columnName) {
    await (0, ag_grid_1.clearGridColumnFilter)(requirePage(this), columnName);
});
(0, cucumber_1.When)('I clear the {string} grid column {string} filter', async function (gridName, columnName) {
    await (0, ag_grid_1.clearGridColumnFilter)(requirePage(this), columnName, resolveGridName(this, gridName));
});
(0, cucumber_1.When)('I extract the grid as JSON', async function () {
    const gridJson = (0, ag_grid_1.stringifyGridJson)(await (0, ag_grid_1.extractGridAsJson)(requirePage(this)));
    await this.attach(gridJson, 'application/json');
});
(0, cucumber_1.When)('I extract the {string} grid as JSON', async function (gridName) {
    const gridJson = (0, ag_grid_1.stringifyGridJson)(await (0, ag_grid_1.extractGridAsJson)(requirePage(this), resolveGridName(this, gridName)));
    await this.attach(gridJson, 'application/json');
});
(0, cucumber_1.When)('I extract the grid as JSON to {string}', async function (filePath) {
    const targetPath = node_path_1.default.resolve(process.cwd(), filePath);
    await promises_1.default.mkdir(node_path_1.default.dirname(targetPath), { recursive: true });
    await promises_1.default.writeFile(targetPath, (0, ag_grid_1.stringifyGridJson)(await (0, ag_grid_1.extractGridAsJson)(requirePage(this))), 'utf8');
});
(0, cucumber_1.When)('I extract the {string} grid as JSON to {string}', async function (gridName, filePath) {
    const targetPath = node_path_1.default.resolve(process.cwd(), filePath);
    await promises_1.default.mkdir(node_path_1.default.dirname(targetPath), { recursive: true });
    await promises_1.default.writeFile(targetPath, (0, ag_grid_1.stringifyGridJson)(await (0, ag_grid_1.extractGridAsJson)(requirePage(this), resolveGridName(this, gridName))), 'utf8');
});
async function assertIndexedCellText(world, rowIndex, columnName, expectedText, match, gridTarget = 0) {
    const cell = await (0, ag_grid_1.getGridCellByRowIndex)(requirePage(world), rowIndex, columnName, gridTarget);
    await (0, ag_grid_1.assertGridCellText)(cell, expectedText, match);
}
async function assertMatchedCellText(world, lookupColumn, lookupValue, targetColumn, expectedText, match, gridTarget = 0) {
    const cell = await (0, ag_grid_1.getGridCellByRowValue)(requirePage(world), { lookupColumn, lookupValue, targetColumn }, gridTarget);
    await (0, ag_grid_1.assertGridCellText)(cell, expectedText, match);
}
async function interactWithIndexedCell(world, rowIndex, columnName, clickType, gridTarget = 0) {
    const cell = await (0, ag_grid_1.getGridCellByRowIndex)(requirePage(world), rowIndex, columnName, gridTarget);
    await (0, ag_grid_1.interactWithGridCell)(cell, clickType);
}
async function interactWithMatchedCell(world, lookupColumn, lookupValue, targetColumn, clickType, gridTarget = 0) {
    const cell = await (0, ag_grid_1.getGridCellByRowValue)(requirePage(world), { lookupColumn, lookupValue, targetColumn }, gridTarget);
    await (0, ag_grid_1.interactWithGridCell)(cell, clickType);
}
function resolveGridName(world, gridName) {
    const selector = world.config.grids?.[gridName];
    if (!selector) {
        throw new Error(`Grid "${gridName}" was not found in bdd-ui.config.json grids.`);
    }
    return selector;
}
function requirePage(world) {
    if (!world.page) {
        throw new Error('Cannot interact with the grid before the Playwright page has been created.');
    }
    return world.page;
}
