package app.voltplan.cable.data

import java.util.Locale

/** Metric (mm², m) or imperial (AWG, ft). Mirrors the iOS `UnitSystem` enum. */
enum class UnitSystem(val rawValue: String) {
    METRIC("metric"),
    IMPERIAL("imperial");

    val wireAreaUnit: String get() = if (this == METRIC) "mm²" else "AWG"
    val lengthUnit: String get() = if (this == METRIC) "m" else "ft"

    companion object {
        fun fromRaw(raw: String?): UnitSystem? = entries.firstOrNull { it.rawValue == raw }

        /** Default for the device locale — US measurement system implies imperial. */
        fun deviceDefault(): UnitSystem {
            val country = Locale.getDefault().country.uppercase()
            // Countries that customarily use US/imperial measurements.
            return if (country in setOf("US", "LR", "MM")) IMPERIAL else METRIC
        }
    }
}

/** Locale-aware electrical defaults. Mirrors the iOS `LocaleDefaults`. */
object LocaleDefaults {
    private val lowVoltageRegions = setOf("US", "CA", "MX", "JP", "TW", "CO", "VE", "BR")

    val mainsVoltage: Double
        get() {
            val region = Locale.getDefault().country.uppercase()
            return if (region in lowVoltageRegions) 120.0 else 230.0
        }
}
