local p = {}
local lang = mw.getContentLanguage()

function p.forslag(frame)
    local diff = frame.args[1]
    local week = tonumber(lang:formatDate('W', diff..' week', true))
    local year = tonumber(lang:formatDate('o', diff..' week', true))
    local modname = 'Modul:Ukens konkurranse-liste ' .. year
    local topics = require(modname)
    local topic = topics[week]
	local pagename = string.format("Wikipedia:Konkurranser/Ukens konkurranse %04d-%02d", year, week)

    -- er tema bestemt?
    if topic == "" then
        return "''Ikke bestemt ennå'' <small>([[Modul:Ukens konkurranse-liste " .. year .. "|føy til forslag]])</small>"
    end

    -- sjekk om siden eksisterer
    local page = mw.title.new(pagename)
    if not page.exists then
        local s = topic
        s = s .. frame:preprocess(string.format("&#32;''<small>[{{fullurl:Wikipedia:Konkurranser/Ukens konkurranse %04d-%02d|action=edit&preload=Mal:Ukens_konkurranse_preload}} (opprett)]</small>''", year, week))
        return s
    end
    
    return "[[" .. pagename .. "|" .. topic .. "]]"
end


function p.forslag_mk(frame)
    local diff = frame.args[1]
    local month = tonumber(lang:formatDate('n', diff..' month', true))
    local year = tonumber(lang:formatDate('o', diff..' month', true))
    local key = string.format('%04d-%02d', year, month)
    local modname = 'Modul:Månedens konkurranse'
    local topics = require(modname)
    local topic = topics[key]
	local pagename = string.format("Wikipedia:Konkurranser/Månedens konkurranse %s", key)

    -- er tema bestemt?
    if topic == nil or topic == "" then
        return "''Ikke bestemt ennå'' <small>([[Modul:Månedens konkurranse|føy til forslag]])</small>"
    end

    -- sjekk om siden eksisterer
    local page = mw.title.new(pagename)
    if not page.exists then
        local s = topic
        s = s .. frame:preprocess(string.format("&#32;''<small>[{{fullurl:Wikipedia:Konkurranser/Månedens konkurranse %s|action=edit&preload=Mal:Ukens_konkurranse_preload}} (opprett)]</small>''", key))
        return s
    end
    
    return "[[" .. pagename .. "|" .. topic .. "]]"
end

return p