import { createApiClient } from '@bytelyst/api-client';
import { API_CONFIG } from './config';
import { getAuthClient } from './auth';

let apiClient: ReturnType<typeof createApiClient> | null = null;

export function getApiClient() {
  if (!apiClient) {
    apiClient = createApiClient({
      baseUrl: API_CONFIG.productBaseUrl,
      getToken: () => getAuthClient().getAccessToken(),
      defaultHeaders: {
        'x-product-id': API_CONFIG.productId,
      },
      timeoutMs: API_CONFIG.timeoutMs,
      retries: 1,
      retryDelayMs: 250,
    });
  }

  return apiClient;
}
