/**
 * Resolve a chat-media path (/api/chat/voice/:id, /api/chat/image/:id) to a URL the browser can load
 * directly in an <img>/<audio> element.
 *
 * Web build: just the relative path (same-origin, cookie-authed).
 * Bundled app: an element load does NOT pass through the fetch wrapper (which rebases to apiBase +
 *   injects the bearer), so the URL must be ABSOLUTE and carry the token itself. The serve endpoints
 *   accept ?access_token= as a fallback for exactly this case. (Token lands in access logs — same
 *   trade-off as the WebSocket URL; scrub the param server-side if that's a concern.)
 */

// Injected by Vite only in the app build (see nuxt.config.ts).
declare const __CAP_BUILD__: boolean | undefined

export const chatMediaUrl = (path: string): string => {
  if (!path) return ''
  if (typeof __CAP_BUILD__ === 'undefined') return path // web → relative + cookie auth
  const base = String(useRuntimeConfig().public.apiBase || '')
  if (!base) return path
  const token = useAuthSession().token?.value
  return base + path + (token ? (path.includes('?') ? '&' : '?') + 'access_token=' + encodeURIComponent(token) : '')
}
