package com.applemand.zmanim.core.location

import us.dustinj.timezonemap.TimeZoneMap
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Offline timezone lookup backed by `us.dustinj.timezonemap`.
 *
 * For custom lat/lng the caller wants an IANA id without any network lookup; for
 * city-DB coordinates, prefer the row's `tzid` which is already correct.
 *
 * The TimeZoneMap instance bundles ~25 MB of Shapefile data and is expensive to
 * construct; we lazy-initialize a globe-wide instance once per process.
 */
@Singleton
class TimezoneResolver @Inject constructor() {

    private val fullMap: TimeZoneMap by lazy {
        TimeZoneMap.forEverywhere()
    }

    fun resolve(lat: Double, lng: Double): String? =
        fullMap.getOverlappingTimeZone(lat, lng)?.zoneId

    /** Free the (memory-heavy) backing data once the user has picked a city. */
    fun dispose() {
        // no-op today; provided so callers can clearly signal lifetime intent.
    }
}
