/**
 * Absolute URL for `vibn-preview-bridge.js` served by this dashboard.
 * Tunnel previews load it via `<Script src={…} />` / `<script>` — cross-origin script tags are allowed.
 *
 * Optional override: NEXT_PUBLIC_VIBN_BRIDGE_URL (e.g. CDN or alternate host).
 */
export function dashboardBridgeScriptUrl(): string {
  const explicit = (process.env.NEXT_PUBLIC_VIBN_BRIDGE_URL ?? "").trim();
  if (explicit) return explicit;

  if (typeof window !== "undefined") {
    return `${window.location.origin}/vibn-preview-bridge.js`;
  }

  const base = (process.env.NEXT_PUBLIC_APP_URL ?? "").trim().replace(/\/$/, "");
  if (base) return `${base}/vibn-preview-bridge.js`;

  return "/vibn-preview-bridge.js";
}
