"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cucumber_1 = require("@cucumber/cucumber");
const locator_1 = require("../runtime/locator");
const response_compare_1 = require("../runtime/response-compare");
(0, cucumber_1.When)('I submit the form and capture response matching {string}', async function (match) {
    this.capturedResponse = await (0, response_compare_1.captureJsonResponse)(requirePage(this), match, async () => {
        await findSubmitLocator(this).click();
    });
});
(0, cucumber_1.When)('I click {string} and capture response matching {string}', async function (target, match) {
    this.capturedResponse = await (0, response_compare_1.captureJsonResponse)(requirePage(this), match, async () => {
        await (0, locator_1.locatorByAny)(requirePage(this), target, this.config.selectors).first().click();
    });
});
(0, cucumber_1.When)('I click {string} at index {int} and capture response matching {string}', async function (target, index, match) {
    this.capturedResponse = await (0, response_compare_1.captureJsonResponse)(requirePage(this), match, async () => {
        await (0, locator_1.atIndex)((0, locator_1.locatorByAny)(requirePage(this), target, this.config.selectors), index).click();
    });
});
(0, cucumber_1.Then)('I compare response with expected result {string}', async function (expectedResult) {
    await (0, response_compare_1.compareCapturedResponse)(baseCompareOptions(this, expectedResult));
});
(0, cucumber_1.Then)('I compare response with expected result {string} with Parent Object {string}', async function (expectedResult, parentObject) {
    await (0, response_compare_1.compareCapturedResponse)({
        ...baseCompareOptions(this, expectedResult),
        parentObject
    });
});
(0, cucumber_1.Then)('I compare response with expected result {string} with Primary Key {string}', async function (expectedResult, primaryKey) {
    await (0, response_compare_1.compareCapturedResponse)({
        ...baseCompareOptions(this, expectedResult),
        primaryKey
    });
});
(0, cucumber_1.Then)('I compare response with expected result {string} and ignore {string}', async function (expectedResult, ignore) {
    await (0, response_compare_1.compareCapturedResponse)({
        ...baseCompareOptions(this, expectedResult),
        ignore
    });
});
(0, cucumber_1.Then)('I compare response with expected result {string} for specific fields {string}', async function (expectedResult, fields) {
    await (0, response_compare_1.compareCapturedResponse)({
        ...baseCompareOptions(this, expectedResult),
        fields
    });
});
(0, cucumber_1.Then)('I compare response with expected result {string} and fail on extra {string}', async function (expectedResult, failOnExtra) {
    await (0, response_compare_1.compareCapturedResponse)({
        ...baseCompareOptions(this, expectedResult),
        failOnExtra
    });
});
function baseCompareOptions(world, expectedResult) {
    return {
        config: world.config,
        featureName: world.featureName,
        expectedResult,
        capturedResponse: world.capturedResponse
    };
}
function requirePage(world) {
    if (!world.page) {
        throw new Error('Cannot capture a response before the Playwright page has been created.');
    }
    return world.page;
}
function findSubmitLocator(world) {
    const page = requirePage(world);
    const submitSelector = world.config.auth?.submitSelector;
    const submitButtonName = world.config.auth?.submitButtonName ?? 'Login';
    if (submitSelector) {
        return page.locator(submitSelector);
    }
    return page
        .getByRole('button', { name: submitButtonName, exact: true })
        .or(page.getByRole('button', { name: /submit/i }))
        .or(page.locator('button[type="submit"]'))
        .first();
}
