/**
 * GraphQL client configuration using urql.
 *
 * ⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
 */

import { createClient, cacheExchange, fetchExchange, subscriptionExchange } from 'urql';
import { createClient as createWSClient } from 'graphql-ws';

// Configure based on your environment
// Uses relative URLs by default for Vite proxy support, with env var overrides for Docker/Traefik
const GRAPHQL_URL = import.meta.env.VITE_GRAPHQL_URL || '/graphql';
const GRAPHQL_WS_URL = import.meta.env.VITE_GRAPHQL_WS_URL || `ws://${window.location.host}/graphql/ws`;

// WebSocket client for subscriptions
const wsClient = createWSClient({
  url: GRAPHQL_WS_URL,
});

// Create the urql client
export const client = createClient({
  url: GRAPHQL_URL,
  exchanges: [
    cacheExchange,
    fetchExchange,
    subscriptionExchange({
      forwardSubscription: (request) => ({
        subscribe: (sink) => ({
          unsubscribe: wsClient.subscribe(
            request as { query: string; variables?: Record<string, unknown> },
            sink
          ),
        }),
      }),
    }),
  ],
});

export default client;
