import { ConvexHttpClient } from "convex/browser"

import { env } from "@/lib/env"

export class ConvexConfigurationError extends Error {
  constructor(message = "Convex não configurado.") {
    super(message)
    this.name = "ConvexConfigurationError"
  }
}

export function requireConvexUrl(): string {
  const url = env.NEXT_PUBLIC_CONVEX_URL
  if (!url) {
    throw new ConvexConfigurationError()
  }
  return url
}

export function createConvexClient(): ConvexHttpClient {
  const url = requireConvexUrl()
  return new ConvexHttpClient(url)
}

