local addonName, NS = ...

-- ============================================================================
-- VAULTSKINS: LOCALIZATION
-- ============================================================================

-- ============================= DEFAULT STRINGS ==============================
local defaultLocale = {
    ADDON_NAME = "VaultSkins",
    THEMES = "Themes",
    REVEAL = "Reveal",
    GENERAL = "General",
    DIAGNOSTICS = "Diagnostics",
    VAULT_SCALE = "Vault scale",
    AUTO_FIT = "Automatically fit the Vault to the screen",
    OPENING_ANIMATION = "Animate Vault opening",
    SOUND_EFFECTS = "Theme-aware sound effects",
    SOUND_SYNC = "Synchronize sounds with visual phases",
    FRAME_SOUNDS = "Frame sounds",
    REWARD_SOUNDS = "Reward reveal sounds",
    FINAL_SOUNDS = "Final flourish sounds",
    FINAL_FLOURISH = "Final reveal flourish",
    REVEAL_ORDER = "Reveal order",
    REVEAL_SPEED = "Reveal speed",
    PREVIEW = "Preview",
    RESET = "Reset",
    REPAIR = "Repair UI",
    PRINT_STATUS = "Print status",
    COMPATIBILITY_MODE = "UI compatibility",
    COMPAT_AUTO = "Automatic",
    COMPAT_NATIVE = "VaultSkins",
    COMPAT_MINIMAL = "External UI",
    STATUS_READY = "Ready",
    THEME_ENGINE = "Theme Engine",
    OPEN_WITH = "Open anytime with /vskin or /vskins",
    NO_ERRORS = "No VaultSkins errors recorded this session.",
    ERROR_SUMMARY = "%d protected error(s), %d disabled update(s)",
    REPAIR_COMPLETE = "Vault UI repaired and reapplied.",
    SETTINGS_RESET = "Settings reset.",
    SKINS = "SKINS",
    CHOOSE_THEME = "Choose a Great Vault theme.",
    SPEED_FAST = "Fast",
    SPEED_NORMAL = "Normal",
    SPEED_CINEMATIC = "Cinematic",
    MODE_SEQUENTIAL = "One by one",
    MODE_ROWS = "Row by row",
    MODE_REWARDS_LAST = "Rewards last",
    MODE_RANDOM = "Random order",
}

local localeRegistry = {
    enUS = defaultLocale,
}

-- ============================ LOCALE RESOLUTION =============================
local function BuildLocale(locale)
    local result = {}
    for key, value in pairs(defaultLocale) do
        result[key] = value
    end
    local selected = localeRegistry[locale]
    if selected and selected ~= defaultLocale then
        for key, value in pairs(selected) do
            result[key] = value
        end
    end
    return result
end

NS.L = BuildLocale(GetLocale and GetLocale() or "enUS")

-- =========================== EXTERNAL LOCALE API ============================
function NS:RegisterLocale(locale, strings)
    if type(locale) ~= "string" or locale == "" or type(strings) ~= "table" then
        return false
    end
    localeRegistry[locale] = strings
    if GetLocale and GetLocale() == locale then
        local updated = BuildLocale(locale)
        for key in pairs(NS.L) do NS.L[key] = nil end
        for key, value in pairs(updated) do NS.L[key] = value end
    end
    return true
end

_G.VaultSkinsLocaleAPI = {
    apiVersion = 1,
    RegisterLocale = function(locale, strings)
        return NS:RegisterLocale(locale, strings)
    end,
}
