"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sumMap = exports.objectToReadonlyMap = exports.objectToMap = exports.mapSetHash = exports.getReversedMap = exports.defaultMapSetHash = exports.defaultMapGetHash = exports.copyMap = void 0;
const array_1 = require("./array");
/** Helper function to copy a map. (You can also use a Map constructor to accomplish this task.) */
// eslint-disable-next-line isaacscript/no-mutable-return
function copyMap(oldMap) {
    const newMap = new Map();
    for (const [key, value] of oldMap) {
        newMap.set(key, value);
    }
    return newMap;
}
exports.copyMap = copyMap;
/**
 * Helper function to get the value from a `DefaultMap` that corresponds to an entity, assuming that
 * the map uses `PtrHash` as an index.
 */
function defaultMapGetHash(map, entity, ...extraArgs) {
    const ptrHash = GetPtrHash(entity);
    return map.getAndSetDefault(ptrHash, ...extraArgs);
}
exports.defaultMapGetHash = defaultMapGetHash;
/**
 * Helper function to set a value for a `DefaultMap` that corresponds to an entity, assuming that
 * the map uses `PtrHash` as an index.
 *
 * Since `Map` and `DefaultMap` set values in the same way, this function is simply an alias for the
 * `mapSetHash` helper function.
 */
function defaultMapSetHash(
// eslint-disable-next-line isaacscript/prefer-readonly-parameter-types
map, entity, value) {
    mapSetHash(map, entity, value);
}
exports.defaultMapSetHash = defaultMapSetHash;
/**
 * Helper function to get a copy of a map with the keys and the values reversed.
 *
 * For example:
 *
 * ```ts
 * new Map<string, number>([
 *   ["foo", 1],
 *   ["bar", 2],
 * ]);
 * ```
 *
 * Would be reversed to:
 *
 * ```ts
 * new Map<number, string>([
 *   [1, "foo"],
 *   [2, "bar"],
 * ]);
 * ```
 */
function getReversedMap(map) {
    const reverseMap = new Map();
    for (const [key, value] of map) {
        reverseMap.set(value, key);
    }
    return reverseMap;
}
exports.getReversedMap = getReversedMap;
/**
 * Helper function to set a value for a `DefaultMap` that corresponds to an entity, assuming that
 * the map uses `PtrHash` as an index.
 */
function mapSetHash(
// eslint-disable-next-line isaacscript/prefer-readonly-parameter-types
map, entity, value) {
    const hash = GetPtrHash(entity);
    map.set(hash, value);
}
exports.mapSetHash = mapSetHash;
/**
 * Helper function to convert an object to a map.
 *
 * This is useful when you need to construct a type safe object with the `satisfies` operator, but
 * then later on you need to query it in a way where you expect the return value to be T or
 * undefined. In this situation, by converting the object to a map, you can avoid unsafe type
 * assertions.
 *
 * Note that the map values will be inserted in a random order, due to how `pairs` works under the
 * hood.
 *
 * Also see the `objectToReadonlyMap` function.
 */
// eslint-disable-next-line isaacscript/no-mutable-return
function objectToMap(object) {
    const map = new Map();
    for (const [key, value] of Object.entries(object)) {
        map.set(key, value);
    }
    return map;
}
exports.objectToMap = objectToMap;
/**
 * Helper function to convert an object to a read-only map.
 *
 * This is useful when you need to construct a type safe object with the `satisfies` operator, but
 * then later on you need to query it in a way where you expect the return value to be T or
 * undefined. In this situation, by converting the object to a map, you can avoid unsafe type
 * assertions.
 *
 * Note that the map values will be inserted in a random order, due to how `pairs` works under the
 * hood.
 *
 * Also see the `objectToMap` function.
 */
function objectToReadonlyMap(object) {
    return objectToMap(object);
}
exports.objectToReadonlyMap = objectToReadonlyMap;
/** Helper function to sum every value in a map together. */
function sumMap(map) {
    const values = [...map.values()];
    return (0, array_1.sumArray)(values);
}
exports.sumMap = sumMap;
