local addonName, addonTable = ...
addonTable.locales = addonTable.locales or {}

local function CopyTable(src)
    local out = {}
    if not src then return out end
    for k, v in pairs(src) do out[k] = v end
    return out
end

local function ResolveSavedLanguage()
    local saved = _G.PremadeCDsDB
    if type(saved) ~= "table" then return nil end
    local playerName = UnitName("player")
    if not playerName then return nil end
    local charToProfile = saved.charToProfile or {}
    local profileName = charToProfile[playerName] or "Default"
    local profiles = saved.profiles or {}
    local profile = profiles[profileName] or profiles.Default
    return profile and profile.language or nil
end

local SUPPORTED = { enUS = true, esES = true, frFR = true, deDE = true, koKR = true, zhCN = true, zhTW = true, jaJP = true }

local function NormalizeLocale(locale)
    if locale == "Auto" or locale == nil or locale == "" then
        locale = ResolveSavedLanguage() or GetLocale()
    end
    if locale == "enGB" then locale = "enUS" end
    if locale == "esMX" then locale = "esES" end
    if locale == "zhSG" then locale = "zhCN" end
    if not SUPPORTED[locale] then
        locale = "enUS"
    end
    return locale
end

addonTable.NormalizeLocale = addonTable.NormalizeLocale or NormalizeLocale

addonTable.GetDict = addonTable.GetDict or function(locale)
    local normalized = NormalizeLocale(locale)
    local base = addonTable.locales.enUS or {}
    local selected = addonTable.locales[normalized] or base
    local dict = CopyTable(base)
    for k, v in pairs(selected) do dict[k] = v end
    return dict
end

addonTable.locales.enUS = {
    ANCHOR_MODE = "Anchor Mode",
    ANCHOR_SIDE = "Anchor Side",
    ASCENDING = "Ascending",
    AUTO = "Auto",
    BAR_HEIGHT = "Bar Height",
    BAR_WIDTH = "Bar Width",
    BOTTOM = "Bottom",
    CCS_PER_ROW = "CCs per Row",
    CC_HINT = "Use the filter to choose which CCs to show.",
    CC_SIZE = "CC Size",
    CLOSE = "Close",
    CLOSE_TEST = "Close Test",
    CREATE = "Create",
    DELETE = "Delete",
    DESCENDING = "Descending",
    DUNGEONS = "Dungeons",
    FILTERS = "Filters",
    FILTERS_TITLE = "Spell Filters",
    FONT = "Font:",
    GROW_DIR = "Growth Direction",
    HEADER_CCS = "CCs",
    HEADER_DEF = "Defensives",
    HEADER_GEN = "General",
    HEADER_KICKS = "Kicks",
    ICON_SIZE = "Icon Size",
    KICK_ORDER = "Kick Order",
    LANGUAGE = "Language:",
    LANG_AUTO = "Auto",
    LD = "Left-Down",
    LEFT = "Left",
    LOAD = "Load Profile:",
    LU = "Left-Up",
    MANUAL = "Manual",
    MARK_ALL = "Mark All",
    NAME_SIZE = "Name Size",
    NONE = "None",
    NO_RESULTS = "No results",
    OFFSET_X = "Offset X",
    OFFSET_Y = "Offset Y",
    OPEN_WORLD = "Open World",
    PER_ROW = "Per Row",
    PIXEL_PERFECT = "Pixel Perfect",
    PROFILE_SAVED = "Profile saved successfully.",
    PVP = "PvP",
    RAID = "Raid",
    RD = "Right-Down",
    READY = "Ready",
    RELOAD_REQ = "Please type /reload to apply the language change.",
    RIGHT = "Right",
    RU = "Right-Up",
    SEC_ANCHORS = "Anchors",
    SEC_BARS = "Bars",
    SEC_BEHAVIOR = "Behavior",
    SEC_CAT = "Categories",
    SEC_ICONS = "Icons",
    SEC_LAYOUT = "Layout",
    SEC_PROFILES = "Profiles",
    SEC_TEXT = "Text",
    SEC_VISUALS = "Visuals",
    SEC_ZONES = "Zones",
    SELECT = "Select",
    SHOW_CCS = "Show CCs",
    SHOW_DEF = "Show Defensives",
    SHOW_DEF_FIRST = "Show Defensives First",
    SHOW_KICKS = "Show Kicks",
    SHOW_OFF = "Show Offensives",
    SHOW_READY = "Show \"Ready\"",
    SPACING = "Spacing",
    TAB_CCS = "CCs",
    TAB_DEF = "Def",
    TAB_DEFOFF = "Def / Off",
    TAB_GEN = "General",
    TAB_KICKS = "Kicks",
    TAB_OFF = "Off",
    TEST = "Test",
    TEST_MODE = "Test Mode",
    TEXTURE = "Texture:",
    TIMER_SIZE = "Timer Size",
    TOP = "Top",
    UNMARK_ALL = "Unmark All",
    VOICE_ALERT = "Voice Alert",
}

addonTable.L = addonTable.GetDict and addonTable.GetDict() or addonTable.locales.enUS
