package com.applemand.zmanim.core.jewcal

import kotlinx.datetime.LocalDate

data class OmerCount(
    val day: Int,                    // 1..49
    val weeksAndDays: Pair<Int, Int>,
    val displayEn: String,
    val displayHe: String,
)

class OmerCalculator {
    fun forDate(date: LocalDate): OmerCount? {
        val omer = date.toJewishCalendar().dayOfOmer.takeIf { it in 1..49 } ?: return null
        val weeks = omer / 7
        val extraDays = omer % 7
        val en = buildString {
            append("Today is ").append(omer).append(" day")
            if (omer != 1) append("s")
            append(" of the Omer")
            if (weeks > 0) {
                append(", which is ").append(weeks).append(" week")
                if (weeks != 1) append("s")
                if (extraDays > 0) {
                    append(" and ").append(extraDays).append(" day")
                    if (extraDays != 1) append("s")
                }
            }
        }
        val he = buildString {
            append("היום ").append(HebrewNumerals.format(omer)).append(" לעומר")
            if (weeks > 0) {
                append(" — ").append(weeks).append(" שבוע")
                if (extraDays > 0) append(" ו־").append(extraDays).append(" ימים")
            }
        }
        return OmerCount(
            day = omer,
            weeksAndDays = weeks to extraDays,
            displayEn = en,
            displayHe = he,
        )
    }
}
