/* eslint-disable */
// @ts-ignore
import request from '@/http/vue-query';
import { CustomRequestOptions } from '@/http/types';

import * as API from './types';

/** 取消上传 取消文件上传会话，清理已上传的切片 POST /files/chunk/cancel */
export async function filesChunkCancelUsingPost({
  body,
  options,
}: {
  body: API.FilesChunkCancelUsingPostBody;
  options?: CustomRequestOptions;
}) {
  return request<
    API.Response & {
      message?: string;
    }
  >('/files/chunk/cancel', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 创建文件下载会话 创建文件分片下载会话 POST /files/chunk/create-download-session */
export async function filesChunkCreateDownloadSessionUsingPost({
  body,
  options,
}: {
  body: API.CreateDownloadSessionRequest;
  options?: CustomRequestOptions;
}) {
  return request<
    API.Response & {
      data?: API.FileDownloadSession;
    }
  >('/files/chunk/create-download-session', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 创建文件上传会话 创建文件分片上传会话，返回会话ID POST /files/chunk/create-session */
export async function filesChunkCreateSessionUsingPost({
  body,
  options,
}: {
  body: API.CreateUploadSessionRequest;
  options?: CustomRequestOptions;
}) {
  return request<
    API.Response & {
      data?: API.FileUploadSession;
    }
  >('/files/chunk/create-session', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 下载文件切片 下载单个文件切片 GET /files/chunk/download */
export async function filesChunkDownloadUsingGet({
  params,
  options,
}: {
  // 叠加生成的Param类型 (非body参数openapi默认没有生成对象)
  params: API.filesChunkDownloadUsingGetParams;
  options?: CustomRequestOptions;
}) {
  return request<string>('/files/chunk/download', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 合并文件切片 合并所有已上传的切片，生成完整文件 POST /files/chunk/merge */
export async function filesChunkMergeUsingPost({
  body,
  options,
}: {
  body: API.FilesChunkMergeUsingPostBody;
  options?: CustomRequestOptions;
}) {
  return request<
    API.Response & {
      data?: API.FileInfo;
    }
  >('/files/chunk/merge', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: body,
    ...(options || {}),
  });
}

/** 获取上传进度 获取文件分片上传进度 GET /files/chunk/progress */
export async function filesChunkProgressUsingGet({
  params,
  options,
}: {
  // 叠加生成的Param类型 (非body参数openapi默认没有生成对象)
  params: API.filesChunkProgressUsingGetParams;
  options?: CustomRequestOptions;
}) {
  return request<
    API.Response & {
      data?: API.UploadProgress;
    }
  >('/files/chunk/progress', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 恢复上传 恢复中断的文件上传，返回已上传的切片信息 GET /files/chunk/resume */
export async function filesChunkResumeUsingGet({
  params,
  options,
}: {
  // 叠加生成的Param类型 (非body参数openapi默认没有生成对象)
  params: API.filesChunkResumeUsingGetParams;
  options?: CustomRequestOptions;
}) {
  return request<
    API.Response & {
      data?: API.UploadProgress;
    }
  >('/files/chunk/resume', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 上传文件切片 上传单个文件切片 POST /files/chunk/upload */
export async function filesChunkUploadUsingPost({
  body,
  chunk,
  options,
}: {
  body: API.FilesChunkUploadUsingPostBody;
  chunk?: File;
  options?: CustomRequestOptions;
}) {
  const formData = new FormData();

  if (chunk) {
    formData.append('chunk', chunk);
  }

  Object.keys(body).forEach((ele) => {
    const item = (body as { [key: string]: any })[ele];

    if (item !== undefined && item !== null) {
      if (typeof item === 'object' && !(item instanceof File)) {
        if (item instanceof Array) {
          item.forEach((f) => formData.append(ele, f || ''));
        } else {
          formData.append(ele, JSON.stringify(item));
        }
      } else {
        formData.append(ele, item);
      }
    }
  });

  return request<
    API.Response & {
      message?: string;
    }
  >('/files/chunk/upload', {
    method: 'POST',
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    data: formData,
    ...(options || {}),
  });
}
