local ____lualib = require("lualib_bundle")
local Map = ____lualib.Map
local __TS__New = ____lualib.__TS__New
local __TS__Iterator = ____lualib.__TS__Iterator
local __TS__ObjectEntries = ____lualib.__TS__ObjectEntries
local __TS__Spread = ____lualib.__TS__Spread
local ____exports = {}
local ____array = require("functions.array")
local sumArray = ____array.sumArray
--- Helper function to set a value for a `DefaultMap` that corresponds to an entity, assuming that
-- the map uses `PtrHash` as an index.
function ____exports.mapSetHash(self, map, entity, value)
    local hash = GetPtrHash(entity)
    map:set(hash, value)
end
--- Helper function to copy a map. (You can also use a Map constructor to accomplish this task.)
function ____exports.copyMap(self, oldMap)
    local newMap = __TS__New(Map)
    for ____, ____value in __TS__Iterator(oldMap) do
        local key = ____value[1]
        local value = ____value[2]
        newMap:set(key, value)
    end
    return newMap
end
--- Helper function to get the value from a `DefaultMap` that corresponds to an entity, assuming that
-- the map uses `PtrHash` as an index.
function ____exports.defaultMapGetHash(self, map, entity, ...)
    local ptrHash = GetPtrHash(entity)
    return map:getAndSetDefault(ptrHash, ...)
end
--- 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 ____exports.defaultMapSetHash(self, map, entity, value)
    ____exports.mapSetHash(nil, map, entity, value)
end
--- 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 ____exports.getReversedMap(self, map)
    local reverseMap = __TS__New(Map)
    for ____, ____value in __TS__Iterator(map) do
        local key = ____value[1]
        local value = ____value[2]
        reverseMap:set(value, key)
    end
    return reverseMap
end
--- 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.
function ____exports.objectToMap(self, object)
    local map = __TS__New(Map)
    for ____, ____value in ipairs(__TS__ObjectEntries(object)) do
        local key = ____value[1]
        local value = ____value[2]
        map:set(key, value)
    end
    return map
end
--- 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 ____exports.objectToReadonlyMap(self, object)
    return ____exports.objectToMap(nil, object)
end
--- Helper function to sum every value in a map together.
function ____exports.sumMap(self, map)
    local values = {__TS__Spread(map:values())}
    return sumArray(nil, values)
end
return ____exports
