"use strict";
/*
 * Copyright (c) 2021-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.mergeQueryFields = exports.buildQueryFields = void 0;
const smob_1 = require("smob");
const utils_1 = require("../../utils");
function buildQueryFields(input) {
    if (typeof input === 'undefined') {
        return [];
    }
    const data = (0, utils_1.groupArrayByKeyPath)((0, utils_1.flattenToKeyPathArray)(input));
    const keys = Object.keys(data);
    if (keys.length === 1) {
        return data[keys[0]];
    }
    return data;
}
exports.buildQueryFields = buildQueryFields;
function mergeQueryFields(target, source) {
    if (Array.isArray(target)) {
        target = (0, utils_1.groupArrayByKeyPath)(target);
    }
    if (Array.isArray(source)) {
        source = (0, utils_1.groupArrayByKeyPath)(source);
    }
    const merge = (0, smob_1.createMerger)({
        array: true,
        arrayDistinct: true,
    });
    const data = merge({}, target, source);
    const keys = Object.keys(data);
    if (keys.length === 1) {
        return data[keys[0]];
    }
    return data;
}
exports.mergeQueryFields = mergeQueryFields;
//# sourceMappingURL=build.js.map