"use strict";
/*
 * @poppinss/utils
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.interpolate = void 0;
function uncurryThis(fn) {
    return function (...args) {
        return Function.call.apply(fn, args);
    };
}
const hasOwnProperty = uncurryThis(Object.prototype.hasOwnProperty);
/**
 * Parses prop
 */
function parseProp(data, key) {
    const tokens = key.split('.');
    while (tokens.length) {
        if (data === null || typeof data !== 'object') {
            return;
        }
        const token = tokens.shift();
        data = hasOwnProperty(data, token) ? data[token] : undefined;
    }
    return data;
}
/**
 * A simple function interpolate values inside curly braces.
 *
 * @example
 * `interpolate('hello {{ username }}', { username: 'virk' })`
 */
function interpolate(input, data) {
    return input.replace(/(\\)?{{(.*?)}}/g, (_, escapeChar, key) => {
        if (escapeChar) {
            return `{{${key}}}`;
        }
        return parseProp(data, key.trim());
    });
}
exports.interpolate = interpolate;
