local getArgs = require('Module:Arguments').getArgs
local p = {}

local maasa = mw.loadData("Module:තිථි මං සළකුණු/දත්ත").maasa
local paksha = mw.loadData("Module:තිථි මං සළකුණු/දත්ත").paksha
local thithi = mw.loadData("Module:තිථි මං සළකුණු/දත්ත").thithi

local function scan_table(table, item)
    local idxM = -1
    for k, v in pairs( table ) do
        -- process each key-value pair
        if item == v then
            idxM = k
            valM = v
            break
        end
    end
    return idxM
end

local function succession(frame, args)
    local bef = args["පෙර"]
	local aft = args["පසු"]
    local title = args["තිථිය"]

    local idxM = 0
    local valM = ""

    local idxMaasa = -1
    local idxPaksha = -1
    local idxThithi = -1

    local next_thithi_maasa = ""
    local next_thithi_thithi = ""
    local next_thithi_paksha = ""
    local prev_thithi_maasa = ""
    local prev_thithi_thithi = ""
    local prev_thithi_paksha = ""

    if title == nil then
        title = mw.title.getCurrentTitle().text
    end

    -- සියළුම අගයන් ලබා දී ඇත්නම් කිරීමට කිසිවක් නොමැත
    if bef == nil or aft == nil then
        local s = title
        local items = mw.text.split( s, "%s+" )

        -- අමාවක සහ පසළොස්වක සඳහා පක්ෂය දැක්වීම අනිවාර්ය නොවේ
        if items[3] == nil then
            if items[2] == "අමාවක" then
                items[3] = "පසළොස්වක"
                items[2] = "අව"
            else
                items[3] = items[2]
                items[2] = "පුර"
            end
        end

        idxMaasa = scan_table(maasa, items[1])
        idxPaksha = scan_table(paksha, items[2])
        idxThithi = scan_table(thithi, items[3])

        prev_thithi_paksha = items[2]
        next_thithi_paksha = items[2]

        if idxThithi == 14 then
            if idxMaasa == 11 then
                next_thithi_maasa = maasa[0]
            else
                next_thithi_maasa = maasa[idxMaasa+1]
            end
            if idxMaasa == 0 then
                prev_thithi_maasa = maasa[11]
            else
                prev_thithi_maasa = maasa[idxMaasa-1]
            end
            prev_thithi_thithi = thithi[idxThithi-1]
            prev_thithi_maasa = maasa[idxMaasa]
            next_thithi_paksha = "පුර"
            next_thithi_thithi = thithi[0]
        elseif idxThithi == 13 then
            next_thithi_maasa = maasa[idxMaasa]
            if idxPaksha == 1 then
                next_thithi_thithi = "අමාවක"
            else
                next_thithi_thithi = thithi[idxThithi+1]
            end
            prev_thithi_thithi = thithi[idxThithi-1]
            prev_thithi_maasa = maasa[idxMaasa]
        elseif idxThithi == 0 then
            prev_thithi_thithi = "අමාවක"
            if idxMaasa == 0 then
                prev_thithi_maasa = maasa[11]
            else
                prev_thithi_maasa = maasa[idxMaasa-1]
            end
        else
            next_thithi_maasa = maasa[idxMaasa]
            next_thithi_thithi = thithi[idxThithi+1]
            prev_thithi_maasa = maasa[idxMaasa]
            prev_thithi_thithi = thithi[idxThithi-1]
        end

--        idxMaasa = scan_table(maasa, items[1])
--        idxPaksha = scan_table(paksha, items[2])
--        idxThithi = scan_table(thithi, items[3])

        -- අගය ලබා ගැනීම
        bef=prev_thithi_maasa .. " " .. prev_thithi_paksha .. " " .. prev_thithi_thithi
        aft=next_thithi_maasa .. " " .. next_thithi_paksha .. " " .. next_thithi_thithi

        if bef == nil then
            bef = "බක් අව තෙළෙස්වක"
        end
        if aft == nil then
            aft = "බක් අමාවක"
        end
    end

    local retVal = frame:expandTemplate{ title = 'තිථි මං සළකුණු/පෙර-පසු-තිථි', args = { bef, aft, title } }
	return retVal
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(frame, args)
end

function p._main(frame, args)
	return succession(frame, args)
end

return p
"https://si.wikipedia.org/w/index.php?title=Module:තිථි_මං_සළකුණු&oldid=676158" වෙතින් සම්ප්‍රවේශනය කෙරිණි