-- =============================================================
-- Locales/Locale.lua
-- Nem: Healer Alerts — localization framework.
-- Hand-rolled (no AceLocale). English source strings are the keys.
-- =============================================================

local _, ns = ...

local L = {}
ns.L = L

local active, defaults, registry = {}, {}, {}
local soundRegistry = {}
local currentLocale = "enUS"

local displayNames = {
    enUS = "English",
    deDE = "Deutsch",
    esES = "Español",
    esMX = "Español (México)",
    frFR = "Français",
    itIT = "Italiano",
    koKR = "한국어",
    ptBR = "Português",
    ruRU = "Русский",
    zhCN = "简体中文",
    zhTW = "繁體中文",
}

function L:SetDefaultStrings(t)
    for k, v in pairs(t) do defaults[k] = v end
end

function L:RegisterLocale(key, t)
    registry[key] = t
end

function L:ApplyLocale(key)
    wipe(active)
    currentLocale = key
    local r = registry[key]
    if r then
        for k, v in pairs(r) do active[k] = v end
    end
end

function L:RegisterSounds(key, t)
    soundRegistry[key] = t
end

function L:GetSoundOverride(name)
    local r = soundRegistry[currentLocale]
    return r and r[name] or nil
end

function L:GetLocale()
    return currentLocale
end

function L:GetDisplayName(key)
    return displayNames[key] or key
end

function L:GetAvailableLocales()
    local out = {}
    for key in pairs(registry) do
        out[#out + 1] = { Key = key, Name = displayNames[key] or key }
    end
    table.sort(out, function(a, b) return a.Name < b.Name end)
    return out
end

setmetatable(L, {
    __index = function(_, key)
        if type(key) == "string" then
            return active[key] or defaults[key] or key
        end
        return nil
    end,
})
