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

-- Module:Reference
-- Templates for reference lists, bibliographies, and footnotes

local p = {}

-- Helper: get frame arguments
local function getArgs(frame)
    local args = {}
    local parent = frame:getParent()
    for k, v in pairs(parent.args) do
        if v ~= '' then args[k] = v end
    end
    for k, v in pairs(frame.args) do
        if v ~= '' then args[k] = v end
    end
    return args
end

-- Reflist: displays references
-- Parameters: columns, colwidth, group
function p.reflist(frame)
    local args = getArgs(frame)
    local columns = args.columns or args[1]
    local colwidth = args.colwidth or args[2]
    local group = args.group

    -- Build div opening tag with classes and styles
    local divOpen = '<div class="reflist'
    local style = ''

    if colwidth then
        divOpen = divOpen .. ' reflist-columns'
        style = 'column-width:' .. colwidth .. ';'
    elseif columns then
        divOpen = divOpen .. ' reflist-columns'
        style = 'column-count:' .. columns .. ';'
    end

    divOpen = divOpen .. '"'
    if style ~= '' then
        divOpen = divOpen .. ' style="' .. style .. '"'
    end
    divOpen = divOpen .. '>'

    -- Build references tag using frame:extensionTag for proper parsing
    local refArgs = {}
    if group then
        refArgs.group = group
    end
    local refContent = frame:extensionTag('references', '', refArgs)

    return divOpen .. refContent .. '</div>'
end

-- Refbegin: starts a formatted bibliography
-- Parameters: columns (1), colwidth, indent, liststyle
function p.refbegin(frame)
    local args = getArgs(frame)
    local columns = args[1]
    local colwidth = args.colwidth
    local indent = args.indent
    local liststyle = args.liststyle

    local html = mw.html.create('div')
        :addClass('refbegin')

    -- Column styling
    if colwidth then
        html:addClass('refbegin-columns')
        html:css('column-width', colwidth)
    elseif columns then
        html:addClass('refbegin-columns')
        html:css('column-count', columns)
    end

    -- Custom indent
    if indent then
        html:css('margin-left', indent)
    end

    -- Custom list style
    if liststyle then
        html:css('list-style-type', liststyle)
    end

    -- Return opening tag only (will be closed by refend)
    return '<div class="refbegin' ..
           (colwidth and ' refbegin-columns' or (columns and ' refbegin-columns' or '')) ..
           '"' ..
           (colwidth and ' style="column-width:' .. colwidth .. ';"' or
            (columns and ' style="column-count:' .. columns .. ';"' or '')) ..
           (indent and ' style="margin-left:' .. indent .. ';"' or '') ..
           '>'
end

-- Refend: closes a bibliography started by refbegin
function p.refend(frame)
    return '</div>'
end

-- Efn: explanatory footnote (separate from citations)
-- Parameters: 1 (note text), name, group
function p.efn(frame)
    local args = getArgs(frame)
    local text = args[1] or ''
    local name = args.name
    local group = args.group or 'note'

    if text == '' then
        return '<span class="error">Efn: note text is required</span>'
    end

    local refTag = '<ref group="' .. group .. '"'
    if name then
        refTag = refTag .. ' name="' .. name .. '"'
    end
    refTag = refTag .. '>' .. text .. '</ref>'

    return frame:preprocess(refTag)
end

-- Notelist: displays explanatory notes (shortcut for reflist with group=note)
function p.notelist(frame)
    local args = getArgs(frame)
    local group = args.group or 'note'

    -- Use frame:extensionTag for proper parsing
    local refContent = frame:extensionTag('references', '', {group = group})

    return '<div class="reflist reflist-notes">' .. refContent .. '</div>'
end

return p