local _, core = ...

core.LOCALES = core.LOCALES or {}

local activeLocaleOverride

local function GetSavedLocaleOverride()
    local addon = core.WipedQoL
    if addon and addon.GetDebugLocaleOverride then
        local override = addon:GetDebugLocaleOverride()
        if override == "deDE" or override == "enUS" then
            return override
        end
    end

    local database = _G.WipedQoLDB
    if type(database) ~= "table" or type(database.global) ~= "table" then
        return nil
    end

    local override = database.global.debugLocaleOverride
    if override == "deDE" or override == "enUS" then
        return override
    end

    return nil
end

core.SetActiveLocaleOverride = function(locale)
    if locale == "deDE" or locale == "enUS" then
        activeLocaleOverride = locale
    else
        activeLocaleOverride = nil
    end
end

core.GetActiveLocale = function()
    return activeLocaleOverride or GetSavedLocaleOverride() or GetLocale()
end

core.TRANSLATE = function(msg)
    if not msg then
        return msg
    end

    local activeLocale = core.GetActiveLocale and core.GetActiveLocale() or GetLocale()
    local dictionary = core.LOCALES and core.LOCALES[activeLocale]
    if dictionary and dictionary[msg] then
        return dictionary[msg]
    end

    return msg
end
