import Constants from "expo-constants";

const PROD_BASE_URL = "https://negopoly.fr";
const DEV_BASE_URL =
  process.env.EXPO_PUBLIC_DEV_API_BASE_URL ||
  (Constants.expoConfig?.extra as { devApiBaseUrl?: string } | undefined)?.devApiBaseUrl ||
  "http://<this-computer's-ip>:3000";

function normalizeBaseUrl(value: string) {
  return value.replace(/\/$/, "");
}

export function getApiBaseUrl() {
  return normalizeBaseUrl(__DEV__ ? DEV_BASE_URL : PROD_BASE_URL);
}

export function getWsUrl(sessionId: string, playerId: string) {
  const base = getApiBaseUrl().replace(/^http/, "ws");
  const params = new URLSearchParams({ sessionId, playerId });
  return `${base}/ws?${params.toString()}`;
}
