import type { ChainID } from '../chains.js';
import { AUCTIONEER_URL } from '../constants.js';
import type { ApiResponse } from '../types/api.js';

export type FetchSiweMessageParams = {
  wallet: string;
  chainId: ChainID;
};

export type FetchJWTParams = {
  message: string;
  signature: string;
};

export async function fetchSiweMessage(params: FetchSiweMessageParams) {
  const url = `${AUCTIONEER_URL}/siwe?wallet=${params.wallet}&chainId=${params.chainId}`;
  const response = await fetch(url);
  const data: ApiResponse<string> = await response.json();
  return data;
}

export async function fetchJWTToken(params: FetchJWTParams, token?: string) {
  const url = `${AUCTIONEER_URL}/siwe`;
  const response = await fetch(url, {
    method: 'POST',
    body: JSON.stringify(params),
    headers: {
      Authorization: `Bearer ${token}`,
    },
  });

  const data: ApiResponse<string> = await response.json();
  return data;
}
