/**
 * Core SDK factory — creates and configures platform clients for React Native.
 */

export interface PlatformSDKConfig {
  /** Platform-service base URL (e.g. https://api.bytelyst.com) */
  baseURL: string;
  /** Product ID (e.g. 'nomgap', 'flowmonk') */
  productId: string;
  /** Function that returns the current access token */
  getAccessToken: () => string | null;
}

export interface PlatformSDK {
  config: PlatformSDKConfig;
  /** Generic authenticated fetch against platform-service */
  fetch: (path: string, init?: RequestInit) => Promise<Response>;
}

/**
 * Create a configured platform SDK instance.
 * Pass to providers to wire up auth, telemetry, flags, etc.
 */
export function createRNPlatformSDK(config: PlatformSDKConfig): PlatformSDK {
  const platformFetch = async (path: string, init?: RequestInit): Promise<Response> => {
    const url = `${config.baseURL}${path}`;
    const token = config.getAccessToken();
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      'x-product-id': config.productId,
      ...((init?.headers as Record<string, string>) ?? {}),
    };
    if (token) {
      headers['Authorization'] = `Bearer ${token}`;
    }
    return globalThis.fetch(url, { ...init, headers });
  };

  return { config, fetch: platformFetch };
}
