package dev.ulfrx.recipe.shared.domain.model

import kotlin.jvm.JvmInline
import kotlinx.serialization.Serializable

/**
 * BCP-47 locale tag, e.g. `"pl"`, `"en"`, `"pl-PL"`. Held as a typed value
 * class so localized-name maps don't get confused with arbitrary string keys.
 */
@Serializable
@JvmInline
public value class LocaleTag(public val raw: String)

/**
 * A user-facing string in one or more locales. Stored as a flat JSON object
 * keyed by locale tag (e.g. `{"pl": "Śniadanie", "en": "Breakfast"}`). v1 seed
 * data ships with at least the Polish key populated; additional locales are
 * additive.
 */
public typealias LocalizedString = Map<LocaleTag, String>

/**
 * Resolve a localized string for the requested [locale], falling back to
 * [fallback] (default Polish), then to any populated value, then to an empty
 * string. The empty-string fallback is intentional — UI code should never crash
 * because a translation hasn't shipped yet.
 */
public fun LocalizedString.forLocale(
    locale: LocaleTag,
    fallback: LocaleTag = LocaleTag("pl"),
): String =
    this[locale]
        ?: this[fallback]
        ?: values.firstOrNull()
        ?: ""
