"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getObjSingleKey = void 0;
exports.forEachObjVK = forEachObjVK;
exports.forEachObjKV = forEachObjKV;
exports.mapObjVK = mapObjVK;
exports.mapObjKV = mapObjKV;
exports.mapObjKey = mapObjKey;
exports.mapObjKeyVK = mapObjKeyVK;
exports.mapObjKeyKV = mapObjKeyKV;
exports.someObj = someObj;
exports.findObj = findObj;
exports.reduceObj = reduceObj;
exports.findKeyObjIndexed = findKeyObjIndexed;
exports.filterObj = filterObj;
exports.isEmptyObject = isEmptyObject;
exports.isNotEmptyObject = isNotEmptyObject;
exports.extractFromObj = extractFromObj;
exports.invertObj = invertObj;
exports.clearObj = clearObj;
exports._keyCount = _keyCount;
exports.set = set;
exports.get = get;
exports.deepGet = deepGet;
exports.deepSet = deepSet;
exports.deepDelete = deepDelete;
exports.deepDestroy = deepDestroy;
exports.deepDeepMerge = deepDeepMerge;
exports.deepGetOrDefault = deepGetOrDefault;
exports.ensureObj = ensureObj;
exports.ensure = ensure;
exports.inplaceOmit = inplaceOmit;
exports.omit = omit;
exports.addKey = addKey;
exports.pushKey = pushKey;
exports.unshiftKey = unshiftKey;
exports.incKey = incKey;
exports.decKey = decKey;
exports.deepIncFrom = deepIncFrom;
exports.deepInc = deepInc;
exports.deepPush = deepPush;
exports.deepDecFrom = deepDecFrom;
exports.deepDec = deepDec;
exports.incKeyFrom = incKeyFrom;
exports.decKeyFrom = decKeyFrom;
exports.makeTagObj = makeTagObj;
exports.entries = entries;
exports.values = values;
exports.fromEntries = fromEntries;
exports.revertObj = revertObj;
exports.hasKey = hasKey;
exports.keys = keys;
const deepDefault_1 = require("../deepDefault");
const f_1 = __importDefault(require("../system/f/object/DeepMerge/f"));
function forEachObjVK(obj, callback) {
    for (const key in obj) {
        callback(obj[key], key);
    }
}
function forEachObjKV(obj, callback) {
    for (const key in obj) {
        callback(key, obj[key]);
    }
}
function mapObjVK(obj, callback) {
    const result = {};
    for (const key in obj) {
        result[key] = callback(obj[key], key);
    }
    return result;
}
function mapObjKV(obj, callback) {
    const result = {};
    for (const key in obj) {
        result[key] = callback(key, obj[key]);
    }
    return result;
}
function mapObjKey(obj, callback) {
    return mapObjKeyVK(obj, callback);
}
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;
}
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;
}
function someObj(obj, callback) {
    for (const key in obj) {
        if (callback(obj[key], key)) {
            return true;
        }
    }
    return false;
}
function findObj(obj, callback) {
    for (const key in obj) {
        if (callback(obj[key], key)) {
            return obj[key];
        }
    }
    return undefined;
}
function reduceObj(obj, callback, acc) {
    let _acc = acc;
    for (const key in obj) {
        _acc = callback(_acc, obj[key], key);
    }
    return _acc;
}
function findKeyObjIndexed(obj, callback) {
    for (const key in obj) {
        if (callback(obj[key], key)) {
            return key;
        }
    }
}
function filterObj(obj, callback) {
    const result = {};
    for (const key in obj) {
        if (callback(obj[key], key)) {
            result[key] = obj[key];
        }
    }
    return result;
}
function isEmptyObject(obj) {
    return keys(obj).length === 0;
}
function isNotEmptyObject(obj) {
    return !isEmptyObject(obj);
}
function extractFromObj(obj, keys) {
    const result = {};
    for (const key of keys) {
        if (obj[key]) {
            result[key] = obj[key];
        }
    }
    return result;
}
const getObjSingleKey = (obj) => {
    return keys(obj)[0];
};
exports.getObjSingleKey = getObjSingleKey;
function invertObj(obj) {
    const _obj = {};
    for (const key in obj) {
        const value = obj[key];
        _obj[value] = key;
    }
    return _obj;
}
function clearObj(obj) {
    for (const key in obj) {
        delete obj[key];
    }
}
function _keyCount(obj) {
    return keys(obj).length;
}
function set(obj, k, v) {
    obj[k] = v;
}
function get(obj, k) {
    return obj[k];
}
function deepGet(obj, path) {
    let v = obj;
    for (const p of path) {
        v = obj[p];
        obj = v;
    }
    return v;
}
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;
    }
}
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];
    }
}
function deepDestroy(obj, path) {
    if (path.length === 1) {
        delete obj[path[0]];
        return;
    }
    const [p, ...rest] = path;
    const o = obj[p];
    if (o) {
        deepDestroy(o, rest);
        if (isEmptyObject(o)) {
            delete obj[p];
        }
    }
}
function deepDeepMerge(obj, path, value) {
    deepSet(obj, path, (0, f_1.default)(deepGetOrDefault(obj, path, {}), value));
}
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;
}
function ensureObj(obj, key) {
    return ensure(obj, key, () => ({}));
}
function ensure(obj, key, generate) {
    let value = obj[key];
    if (value === undefined) {
        value = generate();
        obj[key] = value;
    }
    return value;
}
function inplaceOmit(obj, key) {
    let value = obj[key];
    delete obj[key];
    return value;
}
function omit(obj, key) {
    const { [key]: value, ...rest } = obj;
    return rest;
}
function addKey(obj, key, value) {
    if (obj[key] === undefined) {
        obj[key] = new Set();
    }
    obj[key].add(value);
}
function pushKey(obj, key, value) {
    if (obj[key] === undefined) {
        obj[key] = [];
    }
    obj[key].push(value);
}
function unshiftKey(obj, key, value) {
    if (obj[key] === undefined) {
        obj[key] = [];
    }
    obj[key].unshift(value);
}
function incKey(obj, key) {
    return incKeyFrom(obj, key, 0);
}
function decKey(obj, key) {
    return decKeyFrom(obj, key, 0);
}
function deepIncFrom(obj, path, from) {
    let value = deepGetOrDefault(obj, path, undefined);
    if (value === undefined) {
        value = from;
    }
    deepSet(obj, path, value + 1);
}
function deepInc(obj, path) {
    return deepIncFrom(obj, path, 0);
}
function deepPush(obj, path, value) {
    (0, deepDefault_1.deepDefault_)(obj, path, []);
    const array = deepGetOrDefault(obj, path, undefined);
    array.push(value);
}
function deepDecFrom(obj, path, from) {
    let value = deepGetOrDefault(obj, path, undefined);
    if (value === undefined) {
        value = from;
    }
    deepSet(obj, path, value - 1);
}
function deepDec(obj, path) {
    return deepDecFrom(obj, path, 0);
}
function incKeyFrom(obj, key, from) {
    if (obj[key] === undefined) {
        obj[key] = from;
    }
    obj[key]++;
}
function decKeyFrom(obj, key, from) {
    if (obj[key] === undefined) {
        obj[key] = from;
    }
    obj[key]--;
}
function makeTagObj(keys, value) {
    const obj = {};
    for (const key of keys) {
        obj[key] = value;
    }
    return obj;
}
function entries(obj) {
    return Object.entries(obj);
}
function values(obj) {
    return Object.values(obj);
}
function fromEntries(entries) {
    return Object.fromEntries(entries);
}
function revertObj(obj) {
    return fromEntries(entries(obj).map(([key, value]) => {
        return [value, key];
    }));
}
function hasKey(obj, key) {
    return obj[key] !== undefined;
}
function keys(obj) {
    return Object.keys(obj);
}
//# sourceMappingURL=object.js.map