package com.fabledsword.minstrel.shared

/**
 * Resolves a relative server URL into something a Coil / OkHttp client
 * can actually load.
 *
 * The server returns relative paths (e.g. `/api/albums/{id}/cover`,
 * `/api/tracks/{id}/stream`) — host-less URIs that `OkHttpDataSource`
 * and Coil's `AsyncImage` can't resolve on their own. Prepending the
 * `placeholder.invalid` host lets `BaseUrlInterceptor` rewrite the
 * request to the live server with the auth cookie. Absolute `http(s)`
 * URLs pass through unchanged; blank input returns null so callers
 * render their own fallback (or, for playback, the queue entry fails
 * fast rather than handing Media3 a malformed URI).
 *
 * This is the single place a server URL becomes loadable, mirroring
 * Flutter's `ServerImage._resolve` (which prepends the base URL).
 * Any code handing a server-provided URL to a network client must
 * route through here — covers, stream URLs, anything `/api/...`.
 */
fun resolveServerUrl(raw: String?): String? = when {
    raw.isNullOrBlank() -> null
    raw.startsWith("http://") || raw.startsWith("https://") -> raw
    raw.startsWith("/") -> "http://placeholder.invalid$raw"
    else -> "http://placeholder.invalid/$raw"
}
