Документацію для цього модуля можна створити у Модуль:FetchData2/документація

local p = {}

--- Функція для виведення помилки на сторінку
local function error_output(context, error_details)
    local safe_details = error_details or "N/A" 
    return string.format("<span style='color:red; font-weight:bold;'>[FD2 Error: %s. Page: %s]</span>", context or "Unknown Context", safe_details)
end

--- Видаляє вікі-посилання [[link|text]] і [[link]]
local function clean_wikilinks(text)
    if not text then return nil end
    -- [[link|text]] -> text
    text = mw.ustring.gsub(text, "%[%[([^%]|]+)|([^%]]+)%]%]", "%2")
    -- [[link]] -> link
    text = mw.ustring.gsub(text, "%[%[([^%]]+)%]%]", "%1")
    return mw.text.trim(text)
end

----------------------------------------------------------------------
-- ГОЛОВНИЙ ПАРСЕР ДЛЯ ГОРИЗОНТАЛЬНИХ ТАБЛИЦЬ
----------------------------------------------------------------------

local function fetch_from_table(page_title, player_name, column_index)
    
    if not player_name or player_name == "" then
        return error_output("No Player Name", page_title) 
    end
    
    local title = mw.title.new(page_title)
    if not title or not title.exists then 
        return error_output("Page Missing", page_title)
    end

    local content = title:getContent()
    if not content then return error_output("Content Read Fail", page_title) end

    local table_content = mw.ustring.match(content, "{|.-([|].-.-[|]})")
    if not table_content then 
        return error_output("Table Missing", page_title)
    end

    local escaped = mw.ustring.gsub(player_name, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1")
    local player_pattern = "%[%[" .. escaped .. "%]%]" 
    local result = nil
    
    local pattern_parts = {
        "^%s*|%s*",             
        "(.-)",                 
        "%s*[|][|]%s*",         
        player_pattern,         
        "%s*[|][|]%s*",         
        "(.+)$",                
    }
    local start_pattern = table.concat(pattern_parts)

    for row in mw.ustring.gmatch(table_content, "\n%s*|-[^%s]*\n(.-)") do
        
        local c1, rest_of_cells = mw.ustring.match(row, start_pattern)
        
        if c1 then
            local cell_data = {}
            table.insert(cell_data, mw.text.trim(c1)) 
            table.insert(cell_data, "[[" .. player_name .. "]]") 

            local remaining_cells = mw.text.split(rest_of_cells or "", '||')
            for _, cell in ipairs(remaining_cells) do
                 table.insert(cell_data, mw.text.trim(cell))
            end
            
            if column_index <= #cell_data and cell_data[column_index] then
                result = clean_wikilinks(cell_data[column_index])
                break 
            end
        end
    end

    return result
end

----------------------------------------------------------------------
-- ФУНКЦІЇ, ЩО ВИКЛИКАЮТЬСЯ З ШАБЛОНУ
----------------------------------------------------------------------

function p.recruiter(frame)
    local name = frame.args.player
    local raw = fetch_from_table("Гравці", name, 4) 
    
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
    
    if raw == "Відсутній" or raw == "-" then return "Не вказано" end
    return raw or "Не вказано"
end

function p.date_added(frame)
    local name = frame.args.player
    local raw = fetch_from_table("Гравці", name, 3) 
    
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
    
    return raw or "Невідомо"
end

function p.prize_pool(frame)
    local name = frame.args.player
    local raw = fetch_from_table("Призовий_фонд", name, 3) 
    
    if type(raw) == "string" and mw.ustring.find(raw, "Error") then return raw end
    
    if raw then 
        raw = mw.ustring.gsub(raw, "[^%d%s]", "")
        raw = mw.text.trim(raw)
    end
    return (raw or "0") .. " ₴"
end

return p