local ____lualib = require("lualib_bundle")
local __TS__ParseInt = ____lualib.__TS__ParseInt
local __TS__NumberToString = ____lualib.__TS__NumberToString
local __TS__Iterator = ____lualib.__TS__Iterator
local __TS__ArrayUnshift = ____lualib.__TS__ArrayUnshift
local ____exports = {}
local ____flag = require("functions.flag")
local addFlag = ____flag.addFlag
local ____utils = require("functions.utils")
local assertDefined = ____utils.assertDefined
--- Helper function to convert a set of flags to a single `BitFlags` object.
function ____exports.arrayToBitFlags(self, array)
    local flags = 0
    for ____, flag in ipairs(array) do
        flags = addFlag(nil, flags, flag)
    end
    return flags
end
--- Helper function to convert an array of bits to the resulting decimal number.
function ____exports.convertBinaryToDecimal(self, bits)
    local bitsString = table.concat(bits, "")
    return __TS__ParseInt(bitsString, 2)
end
--- Helper function to convert a number to an array of bits.
-- 
-- @param num The number to convert.
-- @param minLength Optional. Equal to the minimum amount of bits that should be returned. If the
-- converted number of bits is below this number, 0's will be padded to the left
-- side until the minimum length is met. Default is undefined (which will not cause
-- any padding).
function ____exports.convertDecimalToBinary(self, num, minLength)
    local bits = {}
    local bitsString = __TS__NumberToString(num, 2)
    for ____, bitString in __TS__Iterator(bitsString) do
        local bit = tonumber(bitString)
        assertDefined(
            nil,
            bit,
            "Failed to convert the following number to binary: " .. tostring(num)
        )
        bits[#bits + 1] = bit
    end
    if minLength ~= nil then
        while #bits < minLength do
            __TS__ArrayUnshift(bits, 0)
        end
    end
    return bits
end
--- Helper function to count the number of bits that are set to 1 in a binary representation of a
-- number.
function ____exports.countSetBits(self, num)
    local count = 0
    while num > 0 do
        num = num & num - 1
        count = count + 1
    end
    return count
end
--- Helper function to get the value of a specific but in a binary representation of a number.
function ____exports.getKBitOfN(self, k, n)
    return n >> k & 1
end
--- Helper function to get the number of bits in a binary representation of a number.
function ____exports.getNumBitsOfN(self, n)
    local numBits = 0
    while n > 0 do
        numBits = numBits + 1
        n = n >> 1
    end
    return numBits
end
--- Helper function to convert a set of flags to a single `BitFlags` object.
function ____exports.setToBitFlags(self, set)
    local flags = 0
    for ____, flag in __TS__Iterator(set) do
        flags = addFlag(nil, flags, flag)
    end
    return flags
end
return ____exports
