import server from "./index";
import axios, { AxiosResponse } from 'axios';

interface InputData<T> {
  image: File;
}

interface PredictRes<T> {
  code: number;
  response_id: string;
  data: T;
}

interface OcrInput {
  FormData: FormData;
}

interface MagciColorRes {
  image: string;
  cost: number;
}

interface ConvertRes {
  file_path: string;
  cost: number;
}

interface DewarpRes {
  image: string;
  points: any;
  cost: number;
}

interface DetectionRes {
  code: number;
  response_id: string;
  data: any;
}

interface OcrRecRes {
  code: number;
  response_id: string;
  data: any;
}

interface TableRecRes {
  code: number;
  response_id: string;
  data: any;
}

export const GetVersion = (api: string): Promise<string> => server.get(api);

export const GetBuildTag = (api: string): Promise<string> => server.get(api);

export const GetModelInfo = (api: string): Promise<string> => server.get(api);

export const IMMagicColor = (
  api: string,
  data: FormData
): Promise<PredictRes<MagciColorRes>> => server.post(api, data);

export const IMDewarp = (
  api: string,
  data: FormData
): Promise<PredictRes<DewarpRes>> => server.post(api, data);

export const Detection = (api: string, data: FormData): Promise<DetectionRes> =>
  server.post(api, data);

export const OcrRec = (api: string, data: FormData): Promise<OcrRecRes> =>
  server.post(api, data);

export const TableRec = (api: string, data: FormData): Promise<TableRecRes> =>
  server.post(api, data);

export const SubmitBug = (api: string, data: any): Promise<OcrRecRes> =>
  server.get(api, data);

// export const Download = (api: string, data: FormData): Promise<Blob> => server.post(api, data);

export const Download = async (api: string, file_path: string): Promise<Blob> => {
  const formData = new FormData();
  formData.append('file_path', file_path);
  const response: AxiosResponse = await axios.post(api, formData, {
    responseType: 'blob',
  });
  return response.data;
};

export const PDFToWord = (
  api: string,
  data: FormData
): Promise<PredictRes<ConvertRes>> => server.post(api, data);

export {};
