const TOKEN_RE = /\$\{([a-zA-Z_][a-zA-Z0-9_]*)\}/g;
export class InterpolationError extends Error {
    key;
    source;
    constructor(key, source) {
        super(`Unknown variable "${key}" referenced in: ${source}`);
        this.key = key;
        this.source = source;
        this.name = "InterpolationError";
    }
}
export function interpolate(source, vars, opts = {}) {
    return source.replace(TOKEN_RE, (match, key) => {
        if (Object.prototype.hasOwnProperty.call(vars, key)) {
            const value = vars[key];
            return typeof value === "string" ? value : String(value);
        }
        if (opts.strict) {
            throw new InterpolationError(key, source);
        }
        return match;
    });
}
/** Quick predicate: does this string contain any interpolation tokens? */
export function hasInterpolation(source) {
    TOKEN_RE.lastIndex = 0;
    return TOKEN_RE.test(source);
}
//# sourceMappingURL=interpolate.js.map