// The absolute origin the client talks to. A packaged native build (Capacitor) loads the SPA from
// its own local origin (https://localhost / file://), so an absolute URL built from location.origin
// would point at the app itself, not the gateway. The native build therefore sets VITE_GATEWAY_URL
// to the real gateway; web/mock leave it empty and fall back to the page origin. Centralised so every
// absolute-URL construction resolves identically. transport.ts computes the same fallback inline for
// the Connect base URL and is intentionally left untouched.

/**
 * gatewayOrigin returns the absolute origin every client build talks to: the configured
 * VITE_GATEWAY_URL when set (the native build), otherwise the current page origin. It returns an
 * empty string when there is no location (server-side), so callers appending a path degrade to a
 * relative URL.
 */
export function gatewayOrigin(): string {
  const configured = import.meta.env.VITE_GATEWAY_URL ?? '';
  return configured || (typeof location !== 'undefined' ? location.origin : '');
}
