"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assign = assign;
exports.deleteFromJson = deleteFromJson;
exports.groupObjectCountsByThreeProps = groupObjectCountsByThreeProps;
exports.groupObjectCountsByTwoProps = groupObjectCountsByTwoProps;
exports.groupObjectCountsByProp = groupObjectCountsByProp;
exports.filterFalsyKeys = filterFalsyKeys;
exports.count2DDictionaryValues = count2DDictionaryValues;
exports.count3DDictionaryValues = count3DDictionaryValues;
function assign(obj, keyPath, value) {
    const lastKeyIndex = keyPath.length - 1;
    for (let i = 0; i < lastKeyIndex; ++i) {
        const key = keyPath[i];
        if (!(key in obj)) {
            obj[key] = {};
        }
        obj = obj[key];
    }
    if (!obj[keyPath[lastKeyIndex]] || typeof obj[keyPath[lastKeyIndex]] == "string") {
        obj[keyPath[lastKeyIndex]] = value;
    }
    else if (Array.isArray(value)) {
        obj[keyPath[lastKeyIndex]] = [...obj[keyPath[lastKeyIndex]], ...value];
    }
    else if (value === false) {
        obj[keyPath[lastKeyIndex]] = false;
    }
    else {
        obj[keyPath[lastKeyIndex]] = { ...obj[keyPath[lastKeyIndex]], ...value };
    }
}
function deleteFromJson(obj, keyPath) {
    const lastKeyIndex = keyPath.length - 1;
    let _obj = obj;
    for (let i = 0; i < lastKeyIndex; ++i) {
        const key = keyPath[i];
        _obj = obj[key];
    }
    delete _obj[keyPath[lastKeyIndex]];
    if (lastKeyIndex !== 0 && Object.values(_obj).length === 0) {
        deleteFromJson(obj, keyPath.slice(0, lastKeyIndex));
    }
}
function groupObjectCountsByThreeProps(objects, primaryProp, secondaryProp, tertiaryProp) {
    return objects.reduce((result, obj) => {
        result[obj[primaryProp]] = result[obj[primaryProp]] ?? {};
        result[obj[primaryProp]][obj[secondaryProp]] = result[obj[primaryProp]][obj[secondaryProp]] ?? {};
        const existingCount = result[obj[primaryProp]][obj[secondaryProp]][obj[tertiaryProp]];
        result[obj[primaryProp]][obj[secondaryProp]][obj[tertiaryProp]] =
            existingCount === undefined ? 1 : existingCount + 1;
        return result;
    }, {});
}
function groupObjectCountsByTwoProps(objects, primaryProp, getSecondaryProp) {
    return objects.reduce((result, obj) => {
        result[obj[primaryProp]] = result[obj[primaryProp]] ?? {};
        const existingCount = result[obj[primaryProp]][getSecondaryProp(obj)];
        result[obj[primaryProp]][getSecondaryProp(obj)] = existingCount === undefined ? 1 : existingCount + 1;
        return result;
    }, {});
}
function groupObjectCountsByProp(objects, getProp) {
    return objects.reduce((result, obj) => {
        const existingCount = result[getProp(obj)];
        result[getProp(obj)] = existingCount === undefined ? 1 : existingCount + 1;
        return result;
    }, {});
}
function filterFalsyKeys(obj) {
    return Object.fromEntries(Object.entries(obj).filter(([, v]) => v));
}
function count2DDictionaryValues(dictionary) {
    return Object.entries(dictionary).reduce((output, [key, innerDict]) => {
        const innerDictOutput = Object.entries(innerDict).reduce((innerOutput, [key2, vals]) => {
            return { ...innerOutput, [key2]: vals.length };
        }, {});
        return { ...output, [key]: innerDictOutput };
    }, {});
}
function count3DDictionaryValues(dictionary, innerPropName) {
    return Object.entries(dictionary).reduce((output, [key, innerDict]) => {
        const innerDictOutput = Object.entries(innerDict).reduce((innerOutput, [key2, vals]) => {
            return { ...innerOutput, [key2]: vals[innerPropName].length };
        }, {});
        return { ...output, [key]: innerDictOutput };
    }, {});
}
//# sourceMappingURL=ObjectUtils.js.map