import { createAuthClient } from '@bytelyst/auth-client';
import type { TokenStorage } from '@bytelyst/auth-client';
import { API_CONFIG } from './config';
import { mmkvStorage } from '../store/mmkv-storage';

const tokenStorage: TokenStorage = {
  getItem: (key: string) => mmkvStorage.getItem(key),
  setItem: (key: string, value: string) => mmkvStorage.setItem(key, value),
  removeItem: (key: string) => mmkvStorage.removeItem(key),
};

let authClient: ReturnType<typeof createAuthClient> | null = null;

export function getAuthClient() {
  if (!authClient) {
    authClient = createAuthClient({
      baseUrl: API_CONFIG.platformBaseUrl,
      productId: API_CONFIG.productId,
      storage: tokenStorage,
      storagePrefix: 'bytelyst-notes',
      timeoutMs: API_CONFIG.timeoutMs,
    });
  }

  return authClient;
}
