Module:DateFromYear

local module = {}

-- Get date of Naw-Ruz for this year. See https://bahai-library.com/pdf/uhj/uhj_bahai_dates_172-221.pdf
local yeartoNawRuz_map = {
    [2020]="20", 
    [2021]="20",
    [2022]="21",
    [2023]="21", 
    [2024]="20",
    [2025]="20",
    [2026]="21",
    [2027]="21",
    [2028]="20",
    [2029]="20",
    [2030]="20",
    [2031]="21",
    [2032]="20",
    [2033]="20",
    [2034]="20",
    [2035]="21",
    [2036]="20",
    [2037]="20",
    [2038]="20",
    [2039]="21",
    [2040]="20",
    [2041]="20",
    [2042]="20",
    [2043]="21",
    [2044]="20",
    [2045]="20",
    [2046]="20",
    [2047]="21",
    [2048]="20",
    [2049]="20",
    [2050]="20",
    [2051]="21",
    -- Continue to fill in for other years as needed
}

-- Map for birth dates from 2022 to 2050
local yeartoBirth = {
    [2022] = "October 27",
    [2023] = "October 17", 
    [2024] = "November 3",
    [2025] = "October 23",
    [2026] = "November 11",
    [2027] = "October 31",
    [2028] = "October 20",
    [2029] = "November 8",
    [2030] = "October 29",
    [2031] = "October 18",
    [2032] = "November 5",
    [2033] = "October 25",
    [2034] = "November 13",
    [2035] = "November 2",
    [2036] = "October 21",
    [2037] = "November 9",
    [2038] = "October 30",
    [2039] = "October 20",
    [2040] = "November 7",
    [2041] = "October 27",
    [2042] = "October 16",
    [2043] = "November 4",
    [2044] = "October 23",
    [2045] = "November 11",
    [2046] = "October 31",
    [2047] = "October 21",
    [2048] = "November 8",
    [2049] = "October 29",
    [2050] = "October 18",
    [2051] = "November 6",
    -- Continue to fill in for other years as needed
}

-- Accept the input as year, return Naw-Ruz date
function module.nawRuz(frame)
    local year = tonumber(frame.args[1])
    return yeartoNawRuz_map[year]
end

-- Return the birth date of Baha'u'llah as is from yeartoBirth
function module.getBirthBaha(frame)
    local year = tonumber(frame.args[1])
    return yeartoBirth[year]
end

-- Return the birth date of the Bab by subtracting one day from the date in yeartoBirth
function module.getBirthBab(frame)
    local year = tonumber(frame.args[1])
    local dateStr = yeartoBirth[year]
    if dateStr then
        local pattern = "(%w+) (%d+)"
        local month, day = dateStr:match(pattern)
        day = tonumber(day) - 1 -- Subtract one day
        if day == 0 then
            -- For now assuming no month change needed; handle differently if needed
            day = 30 -- Example fallback, actual calculation may vary based on month
        end
        return month .. " " .. day
    else
        return nil
    end
end

return module