local ____lualib = require("lualib_bundle")
local __TS__ArraySlice = ____lualib.__TS__ArraySlice
local ____exports = {}
local HORSE_PILL_COLOR_ADJUSTMENT
local ____isaac_2Dtypescript_2Ddefinitions = require("isaac-typescript-definitions")
local PillColor = ____isaac_2Dtypescript_2Ddefinitions.PillColor
local ____cachedEnumValues = require("cachedEnumValues")
local PILL_COLOR_VALUES = ____cachedEnumValues.PILL_COLOR_VALUES
local ____cachedClasses = require("core.cachedClasses")
local game = ____cachedClasses.game
local itemConfig = ____cachedClasses.itemConfig
local ____constantsFirstLast = require("core.constantsFirstLast")
local FIRST_HORSE_PILL_COLOR = ____constantsFirstLast.FIRST_HORSE_PILL_COLOR
local FIRST_PILL_COLOR = ____constantsFirstLast.FIRST_PILL_COLOR
local LAST_HORSE_PILL_COLOR = ____constantsFirstLast.LAST_HORSE_PILL_COLOR
local LAST_NORMAL_PILL_COLOR = ____constantsFirstLast.LAST_NORMAL_PILL_COLOR
local LAST_VANILLA_PILL_EFFECT = ____constantsFirstLast.LAST_VANILLA_PILL_EFFECT
local ____PHDPillConversionsMap = require("maps.PHDPillConversionsMap")
local PHD_PILL_CONVERSIONS_MAP = ____PHDPillConversionsMap.PHD_PILL_CONVERSIONS_MAP
local ____falsePHDPillConversionsMap = require("maps.falsePHDPillConversionsMap")
local FALSE_PHD_PILL_CONVERSIONS_MAP = ____falsePHDPillConversionsMap.FALSE_PHD_PILL_CONVERSIONS_MAP
local ____pillEffectClasses = require("objects.pillEffectClasses")
local DEFAULT_PILL_EFFECT_CLASS = ____pillEffectClasses.DEFAULT_PILL_EFFECT_CLASS
local PILL_EFFECT_CLASSES = ____pillEffectClasses.PILL_EFFECT_CLASSES
local ____pillEffectNames = require("objects.pillEffectNames")
local DEFAULT_PILL_EFFECT_NAME = ____pillEffectNames.DEFAULT_PILL_EFFECT_NAME
local PILL_EFFECT_NAMES = ____pillEffectNames.PILL_EFFECT_NAMES
local ____pillEffectTypeToPillEffects = require("objects.pillEffectTypeToPillEffects")
local PILL_EFFECT_TYPE_TO_PILL_EFFECTS = ____pillEffectTypeToPillEffects.PILL_EFFECT_TYPE_TO_PILL_EFFECTS
local ____pillEffectTypes = require("objects.pillEffectTypes")
local DEFAULT_PILL_EFFECT_TYPE = ____pillEffectTypes.DEFAULT_PILL_EFFECT_TYPE
local PILL_EFFECT_TYPES = ____pillEffectTypes.PILL_EFFECT_TYPES
local ____types = require("functions.types")
local asNumber = ____types.asNumber
local asPillColor = ____types.asPillColor
local asPillEffect = ____types.asPillEffect
local ____utils = require("functions.utils")
local iRange = ____utils.iRange
--- Helper function to see if the given pill color is a horse pill.
-- 
-- Under the hood, this checks for `pillColor > 2048`.
function ____exports.isHorsePill(self, pillColor)
    return asNumber(nil, pillColor) > HORSE_PILL_COLOR_ADJUSTMENT
end
function ____exports.isVanillaPillEffect(self, pillEffect)
    return pillEffect <= LAST_VANILLA_PILL_EFFECT
end
HORSE_PILL_COLOR_ADJUSTMENT = 2048
--- Helper function to get an array with every non-null pill color. This includes all gold colors and
-- all horse colors.
function ____exports.getAllPillColors(self)
    return __TS__ArraySlice(PILL_COLOR_VALUES, 1)
end
--- Helper function to get the associated pill effect after False PHD is acquired. If a pill effect
-- is not altered by False PHD, then the same pill effect will be returned.
function ____exports.getFalsePHDPillEffect(self, pillEffect)
    local convertedPillEffect = FALSE_PHD_PILL_CONVERSIONS_MAP:get(pillEffect)
    return convertedPillEffect or pillEffect
end
--- Helper function to get the corresponding horse pill color from a normal pill color.
-- 
-- For example, passing `PillColor.BLUE_BLUE` would result in 2049, which is the value that
-- corresponds to the horse pill color for blue/blue.
-- 
-- If passed a horse pill color, this function will return the unmodified pill color.
function ____exports.getHorsePillColor(self, pillColor)
    return ____exports.isHorsePill(nil, pillColor) and pillColor or pillColor + HORSE_PILL_COLOR_ADJUSTMENT
end
--- Helper function to get an array with every non-gold horse pill color.
function ____exports.getHorsePillColors(self)
    return iRange(nil, FIRST_HORSE_PILL_COLOR, LAST_HORSE_PILL_COLOR)
end
--- Helper function to get the corresponding normal pill color from a horse pill color.
-- 
-- For example, passing 2049 would result in `PillColor.BLUE_BLUE`.
-- 
-- If called with a non-horse pill color, this function will return back the same color.
function ____exports.getNormalPillColorFromHorse(self, pillColor)
    return ____exports.isHorsePill(nil, pillColor) and asPillColor(nil, pillColor - HORSE_PILL_COLOR_ADJUSTMENT) or pillColor
end
--- Helper function to get an array with every non-gold and non-horse pill color.
function ____exports.getNormalPillColors(self)
    return iRange(nil, FIRST_PILL_COLOR, LAST_NORMAL_PILL_COLOR)
end
--- Helper function to get the associated pill effect after PHD is acquired. If a pill effect is not
-- altered by PHD, then the same pill effect will be returned.
function ____exports.getPHDPillEffect(self, pillEffect)
    local convertedPillEffect = PHD_PILL_CONVERSIONS_MAP:get(pillEffect)
    return convertedPillEffect or pillEffect
end
--- Helper function to get the corresponding pill color from an effect by repeatedly using the
-- `ItemPool.GetPillEffect` method.
-- 
-- Note that this will return the corresponding effect even if the passed pill color is not yet
-- identified by the player.
-- 
-- Returns `PillColor.NULL` if there is the corresponding pill color cannot be found.
-- 
-- This function is especially useful in the `POST_USE_PILL` callback, since at that point, the used
-- pill is already consumed, and the callback only passes the effect. In this specific circumstance,
-- consider using the `POST_USE_PILL_FILTER` callback instead of the `POST_USE_PILL` callback, since
-- it correctly passes the color and handles the case of horse pills.
function ____exports.getPillColorFromEffect(self, pillEffect)
    local itemPool = game:GetItemPool()
    local normalPillColors = ____exports.getNormalPillColors(nil)
    for ____, normalPillColor in ipairs(normalPillColors) do
        local normalPillEffect = itemPool:GetPillEffect(normalPillColor)
        if normalPillEffect == pillEffect then
            return normalPillColor
        end
    end
    return PillColor.NULL
end
--- Helper function to get a pill effect class from a PillEffect enum value. In this context, the
-- class is equal to the numerical prefix in the "class" tag in the "pocketitems.xml" file. Use the
-- `getPillEffectType` helper function to determine whether the pill effect is positive, negative,
-- or neutral.
-- 
-- Due to limitations in the API, this function will not work properly for modded pill effects, and
-- will always return `DEFAULT_PILL_EFFECT_CLASS` in those cases.
function ____exports.getPillEffectClass(self, pillEffect)
    local pillEffectClass = PILL_EFFECT_CLASSES[pillEffect]
    return pillEffectClass or DEFAULT_PILL_EFFECT_CLASS
end
--- Helper function to get a pill effect name from a `PillEffect`. Returns "Unknown" if the provided
-- pill effect is not valid.
-- 
-- This function works for both vanilla and modded pill effects.
-- 
-- For example, `getPillEffectName(PillEffect.BAD_GAS)` would return "Bad Gas".
function ____exports.getPillEffectName(self, pillEffect)
    local pillEffectName = PILL_EFFECT_NAMES[pillEffect]
    if pillEffectName ~= nil then
        return pillEffectName
    end
    local itemConfigPillEffect = itemConfig:GetPillEffect(pillEffect)
    if itemConfigPillEffect ~= nil then
        return itemConfigPillEffect.Name
    end
    return DEFAULT_PILL_EFFECT_NAME
end
--- Helper function to get a pill effect type from a `PillEffect` enum value. In this context, the
-- type is equal to positive, negative, or neutral. This is derived from the suffix of the "class"
-- tag in the "pocketitems.xml" file. Use the `getPillEffectClass` helper function to determine the
-- "power" of the pill.
-- 
-- Due to limitations in the API, this function will not work properly for modded pill effects, and
-- will always return `DEFAULT_PILL_EFFECT_TYPE` in those cases.
function ____exports.getPillEffectType(self, pillEffect)
    local pillEffectType = PILL_EFFECT_TYPES[pillEffect]
    return pillEffectType or DEFAULT_PILL_EFFECT_TYPE
end
function ____exports.getVanillaPillEffectsOfType(self, pillEffectType)
    return PILL_EFFECT_TYPE_TO_PILL_EFFECTS[pillEffectType]
end
--- Helper function to see if the given pill color is either a gold pill or a horse gold pill.
function ____exports.isGoldPill(self, pillColor)
    return pillColor == PillColor.GOLD or pillColor == PillColor.HORSE_GOLD
end
function ____exports.isModdedPillEffect(self, pillEffect)
    return not ____exports.isVanillaPillEffect(nil, pillEffect)
end
--- Helper function to see if the given pill color is not a gold pill and not a horse pill and not
-- the null value.
-- 
-- Under the hood, this checks using the `FIRST_PILL_COLOR` and `LAST_NORMAL_PILL_COLOR` constants.
function ____exports.isNormalPillColor(self, pillColor)
    return pillColor >= FIRST_PILL_COLOR and pillColor <= LAST_NORMAL_PILL_COLOR
end
function ____exports.isValidPillEffect(self, pillEffect)
    local potentialPillEffect = asPillEffect(nil, pillEffect)
    local itemConfigPillEffect = itemConfig:GetPillEffect(potentialPillEffect)
    return itemConfigPillEffect ~= nil
end
return ____exports
