function hasValue(value: string | undefined) {
  return typeof value === "string" && value.trim().length > 0
}

export function getSupabaseUrl() {
  const value = process.env.NEXT_PUBLIC_SUPABASE_URL
  if (hasValue(value)) return value
  throw new Error("Falta configurar NEXT_PUBLIC_SUPABASE_URL")
}

export function getSupabasePublishableKey() {
  const anonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
  if (hasValue(anonKey)) return anonKey

  // Local/dev tolerance for teams using alternate naming.
  if (process.env.NODE_ENV !== "production") {
    const publishableDefaultKey = process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY
    if (hasValue(publishableDefaultKey)) return publishableDefaultKey
  }

  if (process.env.NODE_ENV === "production") {
    throw new Error("Falta configurar NEXT_PUBLIC_SUPABASE_ANON_KEY")
  }

  throw new Error(
    "Falta configurar la clave publicable de Supabase. Define NEXT_PUBLIC_SUPABASE_ANON_KEY o NEXT_PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY",
  )
}
