"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasKey = exports.revertObj = exports.fromEntries = exports.values = exports.entries = exports.makeTagObj = exports.decKeyFrom = exports.incKeyFrom = exports.deepDecFrom = exports.deepIncFrom = exports.decKey = exports.incKey = exports.unshiftKey = exports.pushKey = exports.addKey = exports.omit = exports.inplaceOmit = exports.ensure = exports.ensureObj = exports.deepGetOrDefault = exports.deepDeepMerge = exports.deepDestroy = exports.deepDelete = exports.deepSet = exports.get = exports.set = exports._keyCount = exports.clearObj = exports.invertObj = exports.clone = exports.getObjSingleKey = exports.extractFromObj = exports.isNotEmptyObject = exports.isEmptyObject = exports.filterObj = exports.findKeyObjIndexed = exports.reduceObj = exports.findObj = exports.someObj = exports.mapObjKeyKV = exports.mapObjKeyVK = exports.mapObjKey = exports.mapObjKV = exports.mapObjVK = exports.forEachObjKV = exports.forEachObjVK = void 0;
const f_1 = require("../system/f/object/DeepMerge/f");
const f_2 = require("../system/f/object/Keys/f");
function forEachObjVK(obj, callback) {
    for (const key in obj) {
        callback(obj[key], key);
    }
}
exports.forEachObjVK = forEachObjVK;
function forEachObjKV(obj, callback) {
    for (const key in obj) {
        callback(key, obj[key]);
    }
}
exports.forEachObjKV = forEachObjKV;
function mapObjVK(obj, callback) {
    const result = {};
    for (const key in obj) {
        result[key] = callback(obj[key], key);
    }
    return result;
}
exports.mapObjVK = mapObjVK;
function mapObjKV(obj, callback) {
    const result = {};
    for (const key in obj) {
        result[key] = callback(key, obj[key]);
    }
    return result;
}
exports.mapObjKV = mapObjKV;
function mapObjKey(obj, callback) {
    return mapObjKeyVK(obj, callback);
}
exports.mapObjKey = mapObjKey;
function mapObjKeyVK(obj, callback) {
    const entries = [];
    for (const key in obj) {
        const value = obj[key];
        const newKey = callback(value, key);
        entries.push([newKey, value]);
    }
    const result = fromEntries(entries);
    return result;
}
exports.mapObjKeyVK = mapObjKeyVK;
function mapObjKeyKV(obj, callback) {
    const entries = [];
    for (const key in obj) {
        const value = obj[key];
        const newKey = callback(key, value);
        entries.push([newKey, value]);
    }
    const result = fromEntries(entries);
    return result;
}
exports.mapObjKeyKV = mapObjKeyKV;
function someObj(obj, callback) {
    for (const key in obj) {
        if (callback(obj[key], key)) {
            return true;
        }
    }
    return false;
}
exports.someObj = someObj;
function findObj(obj, callback) {
    for (const key in obj) {
        if (callback(obj[key], key)) {
            return obj[key];
        }
    }
    return undefined;
}
exports.findObj = findObj;
function reduceObj(obj, callback, acc) {
    let _acc = acc;
    for (const key in obj) {
        _acc = callback(_acc, obj[key], key);
    }
    return _acc;
}
exports.reduceObj = reduceObj;
function findKeyObjIndexed(obj, callback) {
    for (const key in obj) {
        if (callback(obj[key], key)) {
            return key;
        }
    }
}
exports.findKeyObjIndexed = findKeyObjIndexed;
function filterObj(obj, callback) {
    const result = {};
    for (const key in obj) {
        if (callback(obj[key], key)) {
            result[key] = obj[key];
        }
    }
    return result;
}
exports.filterObj = filterObj;
function isEmptyObject(obj) {
    return (0, f_2.keys)(obj).length === 0;
}
exports.isEmptyObject = isEmptyObject;
function isNotEmptyObject(obj) {
    return !isEmptyObject(obj);
}
exports.isNotEmptyObject = isNotEmptyObject;
function extractFromObj(obj, keys) {
    const result = {};
    for (const key of keys) {
        if (obj[key]) {
            result[key] = obj[key];
        }
    }
    return result;
}
exports.extractFromObj = extractFromObj;
const getObjSingleKey = (obj) => {
    return (0, f_2.keys)(obj)[0];
};
exports.getObjSingleKey = getObjSingleKey;
function clone(a) {
    if (typeof a !== 'object' || a === null) {
        return a;
    }
    if (a instanceof Set) {
        return new Set(clone([...a]));
    }
    const _a = (Array.isArray(a) ? [] : {});
    for (const k in a) {
        _a[k] = clone(a[k]);
    }
    return _a;
}
exports.clone = clone;
function invertObj(obj) {
    const _obj = {};
    for (const key in obj) {
        const value = obj[key];
        _obj[value] = key;
    }
    return _obj;
}
exports.invertObj = invertObj;
function clearObj(obj) {
    for (const key in obj) {
        delete obj[key];
    }
}
exports.clearObj = clearObj;
function _keyCount(obj) {
    return (0, f_2.keys)(obj).length;
}
exports._keyCount = _keyCount;
function set(obj, k, v) {
    obj[k] = v;
}
exports.set = set;
function get(obj, k) {
    return obj[k];
}
exports.get = get;
function deepSet(obj, path, value) {
    let o = obj;
    const last_index = path.length - 1;
    if (last_index >= 0) {
        const last_p = path[last_index];
        for (let i = 0; i < last_index; i++) {
            const p = path[i];
            if (o[p] === undefined) {
                o[p] = {};
            }
            o = o[p];
        }
        o[last_p] = value;
    }
}
exports.deepSet = deepSet;
function deepDelete(obj, path) {
    let o = obj;
    const last_index = path.length - 1;
    if (last_index >= 0) {
        const last_p = path[last_index];
        for (let i = 0; i < last_index; i++) {
            const p = path[i];
            if (o[p] === undefined) {
                return;
            }
            o = o[p];
        }
        delete o[last_p];
    }
}
exports.deepDelete = deepDelete;
function deepDestroy(obj, path) {
    if (path.length === 1) {
        delete obj[path[0]];
        return;
    }
    const [p, ...rest] = path;
    const o = obj[p];
    deepDestroy(o, rest);
    if (isEmptyObject(o)) {
        delete obj[p];
    }
}
exports.deepDestroy = deepDestroy;
function deepDeepMerge(obj, path, value) {
    deepSet(obj, path, (0, f_1.default)(deepGetOrDefault(obj, path, {}), value));
}
exports.deepDeepMerge = deepDeepMerge;
function deepGetOrDefault(obj, path, d) {
    let o = obj;
    for (let i = 0; i < path.length; i++) {
        const p = path[i];
        if (o[p] === undefined) {
            return d;
        }
        o = o[p];
    }
    return o;
}
exports.deepGetOrDefault = deepGetOrDefault;
function ensureObj(obj, key) {
    return ensure(obj, key, () => ({}));
}
exports.ensureObj = ensureObj;
function ensure(obj, key, generate) {
    let value = obj[key];
    if (value === undefined) {
        value = generate();
        obj[key] = value;
    }
    return value;
}
exports.ensure = ensure;
function inplaceOmit(obj, key) {
    let value = obj[key];
    delete obj[key];
    return value;
}
exports.inplaceOmit = inplaceOmit;
function omit(obj, key) {
    const _b = obj, _c = key, value = _b[_c], rest = __rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
    return rest;
}
exports.omit = omit;
function addKey(obj, key, value) {
    if (obj[key] === undefined) {
        obj[key] = new Set();
    }
    obj[key].add(value);
}
exports.addKey = addKey;
function pushKey(obj, key, value) {
    if (obj[key] === undefined) {
        obj[key] = [];
    }
    obj[key].push(value);
}
exports.pushKey = pushKey;
function unshiftKey(obj, key, value) {
    if (obj[key] === undefined) {
        obj[key] = [];
    }
    obj[key].unshift(value);
}
exports.unshiftKey = unshiftKey;
function incKey(obj, key) {
    return incKeyFrom(obj, key, 0);
}
exports.incKey = incKey;
function decKey(obj, key) {
    return decKeyFrom(obj, key, 0);
}
exports.decKey = decKey;
function deepIncFrom(obj, path, from) {
    let value = deepGetOrDefault(obj, path, undefined);
    if (value === undefined) {
        value = from;
    }
    deepSet(obj, path, value + 1);
}
exports.deepIncFrom = deepIncFrom;
function deepDecFrom(obj, path, from) {
    let value = deepGetOrDefault(obj, path, undefined);
    if (value === undefined) {
        value = from;
    }
    deepSet(obj, path, value - 1);
}
exports.deepDecFrom = deepDecFrom;
function incKeyFrom(obj, key, from) {
    if (obj[key] === undefined) {
        obj[key] = from;
    }
    obj[key]++;
}
exports.incKeyFrom = incKeyFrom;
function decKeyFrom(obj, key, from) {
    if (obj[key] === undefined) {
        obj[key] = from;
    }
    obj[key]--;
}
exports.decKeyFrom = decKeyFrom;
function makeTagObj(keys, value) {
    const obj = {};
    for (const key of keys) {
        obj[key] = value;
    }
    return obj;
}
exports.makeTagObj = makeTagObj;
function entries(obj) {
    return Object.entries(obj);
}
exports.entries = entries;
function values(obj) {
    return Object.values(obj);
}
exports.values = values;
function fromEntries(entries) {
    return Object.fromEntries(entries);
}
exports.fromEntries = fromEntries;
function revertObj(obj) {
    return fromEntries(entries(obj).map(([key, value]) => {
        return [value, key];
    }));
}
exports.revertObj = revertObj;
function hasKey(obj, key) {
    return obj[key] !== undefined;
}
exports.hasKey = hasKey;
//# sourceMappingURL=object.js.map