"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KEY_WITH_DOUBLE_CURLY_BRACES = void 0;
exports.formatByParams = formatByParams;
exports.formatByParamsQuotedEnv = formatByParamsQuotedEnv;
exports.escapeChars = escapeChars;
function defaultKeyToRegex(key) {
    return new RegExp(`{${key}}`, 'g');
}
exports.KEY_WITH_DOUBLE_CURLY_BRACES = {
    keyToRegex(key) {
        return new RegExp(`{{${key}}}`, 'g');
    },
};
/**
 * Replaces occurrences of strings like '{key1}', '{key2}'
 * by corresponding values from params, i.e. params['key1'], params['key2']
 * @param template
 * @param params
 * @example formatByParamas('hello {user}', {user: 'world'}); // returns 'hello world'
 */
function formatByParams(template, params, { keyToRegex = defaultKeyToRegex } = {}) {
    return Object.keys(params).reduce((acc, key) => {
        return acc.replace(keyToRegex(key), String(params[key]));
    }, template);
}
/**
 * Replaces occurrences of strings like `"$key1"`, `"$key2"`
 * by corresponding values from params, i.e. `"params['key1']"`, `"params['key2']"`
 * @param template
 * @param params
 * @example formatByParamas('hello {user}', {user: 'world'}); // returns 'hello world'
 */
function formatByParamsQuotedEnv(template, params, { sanitizeParams = (v) => v } = {}) {
    return Object.keys(params).reduce((acc, key) => {
        const replacement = sanitizeParams(String(params[key]));
        const res = acc.replace(new RegExp(`"\\$${key}"`, 'g'), `"${replacement}"`);
        return res;
    }, template);
}
const DEFAULT_CHARS = {
    '\\': '\\\\',
    '"': '\\"',
};
function escapeChars(v, map = DEFAULT_CHARS) {
    let res = '';
    for (let i = 0; i < v.length; ++i) {
        res += map[v[i]] ?? v[i];
    }
    return res;
}
