-- New Applicants!! - Sound Engine
-- Version: 1.1.5

local NA = _G["NewApplicants"]
if not NA then return end

local SOUNDS_DIR = "Interface\\AddOns\\NewApplicants\\Sounds\\"

-- Built-in sound list (add a new line here when adding a file)
local BUILTIN_SOUNDS = {
    { id = "xylophone", label = "Xylophone (Default)", path = SOUNDS_DIR .. "Xylophone_alarm.mp3" },
}

function NA:GetSoundList()
    if self.testSoundList then return self.testSoundList end

    -- Always pin built-in sounds to the top
    local list = {}
    for _, s in ipairs(BUILTIN_SOUNDS) do
        table.insert(list, s)
    end

    local LSM = LibStub and LibStub("LibSharedMedia-3.0", true)
    if LSM then
        local builtinIds = {}
        for _, s in ipairs(BUILTIN_SOUNDS) do builtinIds[s.id] = true end
        for _, name in ipairs(LSM:List("sound")) do
            if not builtinIds[name] then
                table.insert(list, { id = name, label = name, path = LSM:Fetch("sound", name) })
            end
        end
    end

    return list
end

-- Returns the sound path for the currently selected id (falls back to default)
function NA:GetSelectedSoundPath()
    if self.db.customSoundPath and self.db.customSoundPath ~= "" then
        return self.db.customSoundPath
    end
    local id = self.db.selectedSound or BUILTIN_SOUNDS[1].id
    for _, s in ipairs(BUILTIN_SOUNDS) do
        if s.id == id then return s.path end
    end
    local LSM = LibStub and LibStub("LibSharedMedia-3.0", true)
    if LSM then
        local path = LSM:Fetch("sound", id)
        if path then return path end
    end
    return BUILTIN_SOUNDS[1].path
end

-- Play notification sound
function NA:PlayRingSound()
    PlaySoundFile(self:GetSelectedSoundPath(), "Master") --luacheck: ignore
end
