"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.count3DDictionaryValues = exports.count2DDictionaryValues = exports.filterFalsyKeys = exports.groupObjectCountsByProp = exports.groupObjectCountsByTwoProps = exports.groupObjectCountsByThreeProps = exports.deleteFromJson = exports.assign = void 0;
var tslib_1 = require("tslib");
function assign(obj, keyPath, value) {
    var lastKeyIndex = keyPath.length - 1;
    for (var i = 0; i < lastKeyIndex; ++i) {
        var 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]] = tslib_1.__spreadArray(tslib_1.__spreadArray([], obj[keyPath[lastKeyIndex]], true), value, true);
    }
    else if (value === false) {
        obj[keyPath[lastKeyIndex]] = false;
    }
    else {
        obj[keyPath[lastKeyIndex]] = tslib_1.__assign(tslib_1.__assign({}, obj[keyPath[lastKeyIndex]]), value);
    }
}
exports.assign = assign;
function deleteFromJson(obj, keyPath) {
    var lastKeyIndex = keyPath.length - 1;
    var _obj = obj;
    for (var i = 0; i < lastKeyIndex; ++i) {
        var key = keyPath[i];
        _obj = obj[key];
    }
    delete _obj[keyPath[lastKeyIndex]];
    if (lastKeyIndex !== 0 && Object.values(_obj).length === 0) {
        deleteFromJson(obj, keyPath.slice(0, lastKeyIndex));
    }
}
exports.deleteFromJson = deleteFromJson;
function groupObjectCountsByThreeProps(objects, primaryProp, secondaryProp, tertiaryProp) {
    return objects.reduce(function (result, obj) {
        var _a, _b;
        result[obj[primaryProp]] = (_a = result[obj[primaryProp]]) !== null && _a !== void 0 ? _a : {};
        result[obj[primaryProp]][obj[secondaryProp]] = (_b = result[obj[primaryProp]][obj[secondaryProp]]) !== null && _b !== void 0 ? _b : {};
        var existingCount = result[obj[primaryProp]][obj[secondaryProp]][obj[tertiaryProp]];
        result[obj[primaryProp]][obj[secondaryProp]][obj[tertiaryProp]] =
            existingCount === undefined ? 1 : existingCount + 1;
        return result;
    }, {});
}
exports.groupObjectCountsByThreeProps = groupObjectCountsByThreeProps;
function groupObjectCountsByTwoProps(objects, primaryProp, getSecondaryProp) {
    return objects.reduce(function (result, obj) {
        var _a;
        result[obj[primaryProp]] = (_a = result[obj[primaryProp]]) !== null && _a !== void 0 ? _a : {};
        var existingCount = result[obj[primaryProp]][getSecondaryProp(obj)];
        result[obj[primaryProp]][getSecondaryProp(obj)] = existingCount === undefined ? 1 : existingCount + 1;
        return result;
    }, {});
}
exports.groupObjectCountsByTwoProps = groupObjectCountsByTwoProps;
function groupObjectCountsByProp(objects, getProp) {
    return objects.reduce(function (result, obj) {
        var existingCount = result[getProp(obj)];
        result[getProp(obj)] = existingCount === undefined ? 1 : existingCount + 1;
        return result;
    }, {});
}
exports.groupObjectCountsByProp = groupObjectCountsByProp;
function filterFalsyKeys(obj) {
    return Object.fromEntries(Object.entries(obj).filter(function (_a) {
        var v = _a[1];
        return v;
    }));
}
exports.filterFalsyKeys = filterFalsyKeys;
function count2DDictionaryValues(dictionary) {
    return Object.entries(dictionary).reduce(function (output, _a) {
        var _b;
        var key = _a[0], innerDict = _a[1];
        var innerDictOutput = Object.entries(innerDict).reduce(function (innerOutput, _a) {
            var _b;
            var key2 = _a[0], vals = _a[1];
            return tslib_1.__assign(tslib_1.__assign({}, innerOutput), (_b = {}, _b[key2] = vals.length, _b));
        }, {});
        return tslib_1.__assign(tslib_1.__assign({}, output), (_b = {}, _b[key] = innerDictOutput, _b));
    }, {});
}
exports.count2DDictionaryValues = count2DDictionaryValues;
function count3DDictionaryValues(dictionary, innerPropName) {
    return Object.entries(dictionary).reduce(function (output, _a) {
        var _b;
        var key = _a[0], innerDict = _a[1];
        var innerDictOutput = Object.entries(innerDict).reduce(function (innerOutput, _a) {
            var _b;
            var key2 = _a[0], vals = _a[1];
            return tslib_1.__assign(tslib_1.__assign({}, innerOutput), (_b = {}, _b[key2] = vals[innerPropName].length, _b));
        }, {});
        return tslib_1.__assign(tslib_1.__assign({}, output), (_b = {}, _b[key] = innerDictOutput, _b));
    }, {});
}
exports.count3DDictionaryValues = count3DDictionaryValues;
//# sourceMappingURL=ObjectUtils.js.map