Documentation for this module may be created at ಮಾಡ್ಯೂಲ್:Calendar/doc

local p = {}

local kannadaNumbers = {
    ["0"] = "೦", ["1"] = "೧", ["2"] = "೨", ["3"] = "೩", ["4"] = "೪",
    ["5"] = "೫", ["6"] = "೬", ["7"] = "೭", ["8"] = "೮", ["9"] = "೯"
}

-- Convert numbers to Kannada
local function convertToKannada(number)
    return tostring(number):gsub("%d", function(digit)
        return kannadaNumbers[digit] or digit
    end)
end

-- Convert Kannada numbers to English
local function convertToEnglish(number)
    local englishNumbers = {}
    for k, v in pairs(kannadaNumbers) do
        englishNumbers[v] = k
    end
    return tostring(number):gsub("[%d೦೧೨೩೪೫೬೭೮೯]", function(digit)
        return englishNumbers[digit] or digit
    end)
end

-- Build calendar
function p.build(frame)
    local month = tonumber(frame.args.month) or 1
    local year = tonumber(frame.args.year) or 2024
    local language = frame.args.language or "en"

    -- Validate inputs
    if month < 1 or month > 12 then return "Invalid month" end
    if year < 1 then return "Invalid year" end

    -- Calculate days in month and first day of the week
    local firstDay = os.time{year = year, month = month, day = 1}
    local daysInMonth = tonumber(os.date("%d", os.time{year = year, month = month + 1, day = 0}))
    local dayOfWeek = tonumber(os.date("%w", firstDay))

    -- Generate calendar rows
    local calendar = {}
    local row = {}

    -- Fill empty cells before the first day
    for _ = 1, dayOfWeek do
        table.insert(row, '<td style="border: 1px solid #ccc;"> </td>')
    end

    -- Add days
    for day = 1, daysInMonth do
        local dayStr = tostring(day)
        if language == "kannada" then
            dayStr = convertToKannada(day)
        end
        table.insert(row, '<td style="border: 1px solid #ccc;">' .. dayStr .. '</td>')
        if #row == 7 then
            table.insert(calendar, "<tr>" .. table.concat(row) .. "</tr>")
            row = {}
        end
    end

    -- Fill remaining empty cells
    while #row < 7 do
        table.insert(row, '<td style="border: 1px solid #ccc;"> </td>')
    end
    if #row > 0 then
        table.insert(calendar, "<tr>" .. table.concat(row) .. "</tr>")
    end

    -- Return calendar rows
    return table.concat(calendar, "\n")
end

return p