import { BASE_PATH } from "@/const";

/**
 * Resolves a root-relative path (e.g. "/uploads/audio/x.mp3", "/logo.jpg")
 * against the app's deployment base path (e.g. "/app" on cPanel).
 *
 * Without this, an `<img src="/logo.jpg">` or `<audio src="/uploads/...">`
 * always points at the domain root — which works in local dev (BASE_PATH =
 * "") but 404s in sub-path deployments, because only requests under
 * BASE_PATH (e.g. "/app/*") reach this app; everything else is handled by
 * a different site/document root on the same domain.
 *
 * External URLs (http/https/data/blob) and already-relative paths are
 * returned unchanged.
 */
export function assetUrl(path?: string | null): string | undefined {
  if (!path) return undefined;
  if (!path.startsWith("/")) return path;
  return `${BASE_PATH}${path}`;
}
