Documentation for this module may be created at Module:UpdateExcerpt/doc

local p = {}

-- ==========================================
-- 1. CORE LOGIC (Private internal function)
-- ==========================================
-- This replaces or wraps your original render logic.
-- It accepts raw strings instead of a MediaWiki frame object.
local function _render(month, day, year, height)
    -- -------------------------------------------------------------
    -- [PASTE YOUR EXISTING EXCERPT RENDER LOGIC HERE]
    -- 
    -- For example, fetching the page "Update:Month Day, Year", 
    -- cropping it to the given height, processing tags, etc.
    -- -------------------------------------------------------------
    
    -- (Mock placeholder returning a string; replace with your actual logic)
    return ""
end


-- ==========================================
-- 2. OLD ENTRY POINT (For backwards compatibility)
-- ==========================================
-- Keeps old template calls working. It parses sequential arguments.
function p.render(frame)
    local args = frame.args
    -- Fallback to parent args if called directly or via wrapper template
    if not args[1] then args = frame:getParent().args end
    
    local month  = args[1] or ""
    local day    = args[2] or ""
    local year   = args[3] or ""
    local height = args[4] or "15"
    
    return _render(month, day, year, height)
end


-- ==========================================
-- 3. NEW OPTIMIZED WIDGET ENTRY POINT
-- ==========================================
-- The ultra-fast function that renders the entire HTML frame at once.
function p.drawWidget(frame)
    -- Gather arguments from the template call template {{UpdateExcerpt|month=...}}
    local parentArgs = frame:getParent().args
    local args = frame.args
    
    local month  = parentArgs.month or args.month or ""
    local day    = parentArgs.day or args.day or ""
    local year   = parentArgs.year or args.year or ""
    local height = parentArgs.height or args.height or "15"
    local lang   = parentArgs.lang or args.lang or ""

    -- Dynamic Fallbacks: If dates aren't provided, fetch them via preprocess.
    -- This safely queries Module:LatestUpdate only if absolutely necessary.
    if month == "" then month = frame:preprocess("{{#invoke:LatestUpdate|month}}") end
    if day == ""   then day   = frame:preprocess("{{#invoke:LatestUpdate|day}}") end
    if year == ""  then year  = frame:preprocess("{{#invoke:LatestUpdate|year}}") end

    -- Evaluate the external "days since" text and template layout elements
    local daysSinceText = frame:preprocess("{{#invoke:LatestUpdate|days_since_text|lang=" .. lang .. "}}")
    local translateText = frame:expandTemplate{ title = "Translate", args = { "Latest Update" } }
    local updateLink    = frame:expandTemplate{ title = "Update link", args = { month, day, year } }

    -- Run our internal core render function to get the actual excerpt body
    local excerptBody = _render(month, day, year, height)

    -- Build the HTML utilizing MediaWiki's highly optimized html library
    local html = mw.html.create("div")
        :addClass("nav-bg update-excerpt")

    -- Header
    html:tag("div")
        :addClass("update-excerpt__header")
        :wikitext(translateText .. ": " .. updateLink .. " (" .. daysSinceText .. ")")

    -- Body (The excerpt itself)
    html:wikitext(excerptBody)

    -- Footer
    local footer = html:tag("div")
        :addClass("update-excerpt__footer nav-bg")
        
    footer:tag("span")
        :addClass("update-excerpt__readmore")
        :wikitext("Read more")
        
    footer:wikitext("[[Update:" .. month .. " " .. day .. ", " .. year .. "|<span class=\"update-excerpt__footer-overlay\"></span>]]")

    return tostring(html)
end

return p