"use strict";
/*
 * Copyright (c) 2022.
 * Author Peter Placzek (tada5hi)
 * For the full copyright and license information,
 * view the LICENSE file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyMapping = void 0;
const object_1 = require("./object");
function applyMapping(name, map, onlyKey) {
    if (typeof map === 'undefined') {
        return name;
    }
    let parts = name.split('.');
    const output = [];
    let run = true;
    while (run) {
        const value = parts.shift();
        if (typeof value === 'undefined') {
            run = false;
            break;
        }
        if ((0, object_1.hasOwnProperty)(map, value)) {
            output.push(map[value]);
        }
        else {
            let found = false;
            const rest = [];
            const copy = [...parts];
            while (copy.length > 0) {
                const key = [value, ...copy].join('.');
                if ((0, object_1.hasOwnProperty)(map, key)) {
                    output.push(map[key]);
                    found = true;
                    break;
                }
                else {
                    const last = copy.pop();
                    if (last) {
                        rest.unshift(last);
                    }
                }
            }
            if (found) {
                parts = rest;
            }
            else {
                output.push(value);
            }
        }
    }
    if (onlyKey) {
        return output.pop();
    }
    return output.join('.');
}
exports.applyMapping = applyMapping;
//# sourceMappingURL=mapping.js.map