// componentApi shim — HTTP client with signature headers
// Mirrors the subset of axios API used by payment-kit (post, get, request)
export const componentApi = {
  post: async (url: string, data: any, _opts?: any) => {
    const res = await fetch(url, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(data),
    });
    return { data: await res.json() };
  },
  get: async (url: string, _opts?: any) => {
    const res = await fetch(url);
    return { data: await res.json() };
  },
  // Used by queues/webhook.ts for webhook delivery
  request: async (config: { url: string; method?: string; data?: any; headers?: Record<string, string>; timeout?: number }) => {
    const controller = new AbortController();
    const timeoutId = config.timeout ? setTimeout(() => controller.abort(), config.timeout) : null;
    try {
      const res = await fetch(config.url, {
        method: config.method || 'POST',
        headers: { 'Content-Type': 'application/json', ...config.headers },
        body: config.data ? JSON.stringify(config.data) : undefined,
        signal: controller.signal,
      });
      const data = await res.json().catch(() => null);
      return { data, status: res.status, headers: Object.fromEntries(res.headers.entries()) };
    } finally {
      if (timeoutId) clearTimeout(timeoutId);
    }
  },
};

export default componentApi;
