import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import cookies from 'js-cookie';
import type { AppRouter } from '@ibcornelsen/api';
import { Buffer } from 'buffer';

export const client = createTRPCProxyClient<AppRouter>({
  links: [
    httpBatchLink({
      url: 'http://localhost:3001/',
      headers() {
				const accessToken = cookies.get('accessToken');
				if (!accessToken) return {};

				const buffer = Buffer.from(accessToken, 'utf-8');
				const base64 = buffer.toString('base64')

        return {
          'Authorization': `Bearer ${base64}`,
        };
      },
    }),
  ],
});