// lib/ipconfig.ts
// export function getRuntimeEnv(key: string): string | undefined {
//   if (typeof window !== "undefined" && typeof window.RUNTIME_ENV !== "undefined") {
//     return window.RUNTIME_ENV[key];
//   }
//   return process.env[key];
// }


let _env: Record<string, string> | null = null

export function getRuntimeEnv(key: string): string | undefined {
  console.log("============>>Getting Supabase API call URL.")

  if (typeof window !== "undefined") {
    if (!_env && typeof window.RUNTIME_ENV !== "undefined") {
      _env = window.RUNTIME_ENV
      console.log("[browser-side] Retrieved API endpoint from window.RUNTIME_ENV:", _env);
    }

    if (_env) {
      console.log("[browser-side]Returning cached API from _env:", _env)
      return _env[key]
    }

    console.log("[browser-side]No window.RUNTIME_ENV found in browser")
    return undefined
  }

  // 服务端始终动态读取 process.env
  const val = process.env[key]
  console.log("[server-side] Falling back to process.env for key:", key, "value:", val)
  return val
}

