export const postmanVarRegex = /\{\{(.*?)\}\}/gim;
/**
 * Replacer function for regex replace to be used to replace variables
 * notation to ARC's
 *
 * @param match
 * @param value
 * @return Value to be replaced in the string.
 */
export function variablesReplacerFunction(match, value) {
    switch (value) {
        case '$randomInt':
            value = 'random()';
            break;
        case '$guid':
            value = 'uuid()';
            break;
        case '$timestamp':
            value = 'now()';
            break;
        default:
    }
    return `\${${value}}`;
}
/**
 * Parse input string as a payload param key or value.
 *
 * @param input An input to parse.
 * @return Trimmed string
 */
export function paramValue(input) {
    if (!input) {
        return String();
    }
    input = String(input);
    input = input.trim();
    return input;
}
export const dataValue = Symbol('dataValue');
/**
 * Base class for all Postman transformers
 */
export class PostmanTransformer {
    [dataValue];
    logs = [];
    /**
     * @param data Data to be transformed.
     */
    constructor(data) {
        this[dataValue] = data;
    }
    addLog(type, message) {
        this.logs.push({
            type,
            message,
        });
    }
    /**
     * Replaces any occurrence of {{STRING}} with ARC's variables syntax.
     *
     * @param str A string value to check for variables.
     * @return The same string with ARC's variables syntax
     */
    ensureVariablesSyntax(str) {
        if (!str || !str.indexOf) {
            return str;
        }
        // https://jsperf.com/regex-replace-with-test-conditions
        if (str.indexOf('{{') !== -1) {
            str = str.replace(postmanVarRegex, variablesReplacerFunction);
        }
        return str;
    }
    ensureVarsRecursively(obj) {
        if (Array.isArray(obj)) {
            return obj.map((item) => this.ensureVarsRecursively(item));
        }
        if (obj === Object(obj)) {
            Object.keys(obj).forEach((key) => {
                obj[key] = this.ensureVarsRecursively(obj[key]);
            });
            return obj;
        }
        if (typeof obj === 'string') {
            return this.ensureVariablesSyntax(obj);
        }
        return obj;
    }
}
//# sourceMappingURL=PostmanTransformer.js.map