"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExpect = getExpect;
exports.executeWebAssertion = executeWebAssertion;
exports.resolveVariables = resolveVariables;
exports.resolveSelector = resolveSelector;
exports.getDisplaySelector = getDisplaySelector;
// Import Playwright's expect for assertions with auto-waiting
// We use dynamic import with string concatenation to prevent Vite from
// trying to bundle the playwright package (which is Node.js-only)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let playwrightExpect = null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function getExpect() {
    if (!playwrightExpect) {
        // Use string concatenation to hide import from Vite's static analysis
        const moduleName = '@playwright' + '/test';
        const { expect } = await Promise.resolve(`${moduleName}`).then(s => __importStar(require(s)));
        playwrightExpect = expect;
    }
    return playwrightExpect;
}
/**
 * Execute a web assertion with soft assertion support
 * Wraps Playwright's expect() to handle soft assertion mode
 */
async function executeWebAssertion(context, assertionFn, errorMessage, details) {
    try {
        await assertionFn();
    }
    catch (err) {
        if (context.softAssertions) {
            // Soft assertion mode - collect error
            const softError = {
                message: err.message || errorMessage,
                stepType: details?.stepType,
                expected: details?.expected,
                actual: details?.actual,
                timestamp: new Date().toISOString(),
            };
            if (!context.softAssertionErrors) {
                context.softAssertionErrors = [];
            }
            context.softAssertionErrors.push(softError);
            context.logger.warn(`Soft assertion failed: ${softError.message}`);
        }
        else {
            // Hard assertion mode - re-throw
            throw err;
        }
    }
}
/**
 * Resolve ${variable} placeholders in text
 * Supports dot notation for object properties (e.g., ${user.email})
 */
function resolveVariables(text, context) {
    return text.replace(/\$\{([\w.]+)\}/g, (match, path) => {
        const parts = path.split('.');
        const varName = parts[0];
        let value = context.variables.get(varName);
        // Handle dot notation for nested object access
        if (parts.length > 1 && value !== undefined && value !== null) {
            for (let i = 1; i < parts.length; i++) {
                if (value === undefined || value === null)
                    break;
                value = value[parts[i]];
            }
        }
        if (value === undefined || value === null)
            return match;
        if (typeof value === 'object')
            return JSON.stringify(value);
        return String(value);
    });
}
/**
 * Resolve a selector based on its type
 * - For 'testid:value' format: constructs [testIdAttribute="value"] using the global testIdAttribute
 * - For other formats: returns the selector as-is
 */
function resolveSelector(params, context) {
    const rawSelector = resolveVariables(params.SELECTOR, context);
    // Check for testid: prefix (e.g., "testid:nav-sign-in")
    if (rawSelector.startsWith('testid:')) {
        const testIdValue = rawSelector.substring(7); // Remove 'testid:' prefix
        const testIdAttr = context.testIdAttribute || 'data-testid';
        return `[${testIdAttr}="${testIdValue}"]`;
    }
    return rawSelector;
}
/**
 * Get a display-friendly version of the selector (strips testid: prefix)
 */
function getDisplaySelector(params, context) {
    const rawSelector = params.SELECTOR;
    // For testid: prefix, show the actual CSS selector that will be used
    if (rawSelector.startsWith('testid:')) {
        const testIdValue = rawSelector.substring(7);
        const testIdAttr = context.testIdAttribute || 'data-testid';
        return `[${testIdAttr}="${testIdValue}"]`;
    }
    return rawSelector;
}
