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

import { ApolloClient, InMemoryCache, createHttpLink, split } from '@apollo/client';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { getMainDefinition } from '@apollo/client/utilities';
import { createClient } 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`;

// HTTP link for queries and mutations
const httpLink = createHttpLink({
  uri: GRAPHQL_URL,
});

// WebSocket link for subscriptions
const wsLink = new GraphQLWsLink(
  createClient({
    url: GRAPHQL_WS_URL,
  })
);

// Split link based on operation type
const splitLink = split(
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === 'OperationDefinition' &&
      definition.operation === 'subscription'
    );
  },
  wsLink,
  httpLink
);

// Create the Apollo client
export const client = new ApolloClient({
  link: splitLink,
  cache: new InMemoryCache(),
});

export default client;
