local addonName, ns = ...

ns.Locales = ns.Locales or {}

local function ApplyBindingGlobals(L)
    for k, v in pairs(L) do
        if k:find("^BINDING_") then
            _G[k] = v
        end
    end
end

function ns.ApplyLanguage()
    local OneWoW_GUI = LibStub("OneWoW_GUI-1.0", true)
    local selectedLang
    if OneWoW_GUI and OneWoW_GUI.GetSetting then
        selectedLang = OneWoW_GUI:GetSetting("language")
    end
    selectedLang = selectedLang or GetLocale()
    if selectedLang == "esMX" then selectedLang = "esES" end
    local localeData = ns.Locales[selectedLang] or ns.Locales["enUS"]
    local fallback = ns.Locales["enUS"]
    for k, v in pairs(fallback) do
        ns.L[k] = localeData[k] or v
    end
    ApplyBindingGlobals(ns.L)
end
