"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VariableResolver = void 0;
exports.resolveVariableDefaults = resolveVariableDefaults;
/**
 * Utility class for resolving variable placeholders in strings
 */
class VariableResolver {
    /**
     * Resolve ${variable} placeholders in a string using context variables and currentData
     * Supports dot notation for nested object access (e.g., ${user.email})
     */
    static resolve(text, context) {
        if (typeof text !== 'string')
            return text;
        return text.replace(/\$\{([\w.]+)\}/g, (match, path) => {
            const parts = path.split('.');
            const varName = parts[0];
            // Check currentData first (for data-driven tests)
            if (context.currentData?.values[varName] !== undefined) {
                let value = context.currentData.values[varName];
                // Handle dot notation for nested access
                if (parts.length > 1 && value !== null && typeof value === 'object') {
                    for (let i = 1; i < parts.length; i++) {
                        if (value === undefined || value === null)
                            break;
                        value = value[parts[i]];
                    }
                }
                if (value !== undefined && value !== null) {
                    return typeof value === 'object' ? JSON.stringify(value) : String(value);
                }
            }
            // Then check context variables
            let value = context.variables.get(varName);
            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;
            return typeof value === 'object' ? JSON.stringify(value) : String(value);
        });
    }
    /**
     * Resolve variables in an object recursively
     */
    static resolveObject(obj, context) {
        if (typeof obj === 'string') {
            return this.resolve(obj, context);
        }
        if (Array.isArray(obj)) {
            return obj.map(item => this.resolveObject(item, context));
        }
        if (obj && typeof obj === 'object') {
            const result = {};
            for (const [key, value] of Object.entries(obj)) {
                result[key] = this.resolveObject(value, context);
            }
            return result;
        }
        return obj;
    }
    /**
     * Check if a string contains variable placeholders
     */
    static hasVariables(text) {
        return typeof text === 'string' && /\$\{[\w.]+\}/.test(text);
    }
}
exports.VariableResolver = VariableResolver;
/**
 * Resolve variable defaults from globals format
 * Handles objects with { type, default, description } structure
 */
function resolveVariableDefaults(vars) {
    if (!vars)
        return {};
    const resolved = {};
    for (const [key, value] of Object.entries(vars)) {
        if (value && typeof value === 'object' && 'default' in value) {
            resolved[key] = value.default;
        }
        else {
            resolved[key] = value;
        }
    }
    return resolved;
}
