"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.groupArrayByKeyPath = exports.flattenToKeyPathArray = exports.buildKeyPath = void 0;
const constants_1 = require("../constants");
function buildKeyPath(key, prefix) {
    if (typeof prefix === 'string') {
        return `${prefix}.${key}`;
    }
    return key;
}
exports.buildKeyPath = buildKeyPath;
function flattenToKeyPathArray(input, options, prefix) {
    options = options || {};
    const output = [];
    if (options.transformer) {
        const result = options.transformer(input, output, prefix);
        if (typeof result !== 'undefined' && !!result) {
            return output;
        }
    }
    if (Array.isArray(input)) {
        for (let i = 0; i < input.length; i++) {
            if (options.transformer) {
                const result = options.transformer(input[i], output, prefix);
                if (typeof result !== 'undefined' && !!result) {
                    return output;
                }
            }
            if (Array.isArray(input[i])) {
                for (let j = 0; j < input[i].length; j++) {
                    const key = buildKeyPath(input[i][j], prefix);
                    output.push(key);
                }
                continue;
            }
            if (typeof input[i] === 'string') {
                output.push(buildKeyPath(input[i], prefix));
                continue;
            }
            if (typeof input[i] === 'object') {
                const keys = Object.keys(input[i]);
                for (let j = 0; j < keys.length; j++) {
                    const value = buildKeyPath(keys[j], prefix);
                    const data = flattenToKeyPathArray(input[i][keys[j]], options, value);
                    if (data.length === 0) {
                        output.push(value);
                    }
                    else {
                        output.push(...data);
                    }
                }
            }
        }
        return output;
    }
    if (typeof input === 'object' &&
        input !== null) {
        const keys = Object.keys(input);
        for (let i = 0; i < keys.length; i++) {
            const value = buildKeyPath(keys[i], prefix);
            const data = flattenToKeyPathArray(input[keys[i]], options, value);
            if (data.length === 0) {
                output.push(value);
            }
            else {
                output.push(...data);
            }
        }
        return output;
    }
    if (typeof input === 'string') {
        const value = buildKeyPath(input, prefix);
        output.push(value);
        return output;
    }
    return output;
}
exports.flattenToKeyPathArray = flattenToKeyPathArray;
function groupArrayByKeyPath(input) {
    const pathItems = {};
    for (let i = 0; i < input.length; i++) {
        const parts = input[i].split('.');
        let key;
        let name;
        if (parts.length === 1) {
            key = constants_1.DEFAULT_ID;
            name = input[i];
        }
        else {
            name = parts.pop();
            key = parts.join('.');
        }
        if (!Object.prototype.hasOwnProperty.call(pathItems, key)) {
            pathItems[key] = [];
        }
        pathItems[key].push(name);
    }
    return pathItems;
}
exports.groupArrayByKeyPath = groupArrayByKeyPath;
//# sourceMappingURL=array.js.map