import type { AxiosRequestConfig } from '@vben/request';

import { requestClient } from '#/api/request';

/**
 * Axios上传进度事件
 */
export type AxiosProgressEvent = AxiosRequestConfig['onUploadProgress'];

/**
 * 通过单文件上传接口, 增加额外参数，是否匿名上传
 * @param file 上传的文件
 * @param onUploadProgress 上传进度事件 非必传
 * @param anonymous 是否匿名上传 非必传
 * @returns 上传结果
 */
export function uploadApi(
  file: Blob | File,
  onUploadProgress?: AxiosProgressEvent,
  anonymous?: null | number | undefined,
) {
  if (anonymous === undefined || anonymous === null) {
    anonymous = 0;
  }
  return requestClient.upload(
    '/common/upload',
    { file, anonymous },
    { onUploadProgress, timeout: 60_000 },
  );
}
/**
 * 默认上传结果
 */
export interface UploadResult {
  url: string;
  fileName: string;
  newFileName: string;
  originalFilename: string;
}
