import { uploadChunkV2, toFile } from '@kevisual/resources/index.ts';
import { toast } from 'react-toastify';

type UploadFileOpts = {
  appKey: string;
  version: string;
  filename: string;
  text: string;
};
const getFilenameExtension = (filename: string) => {
  return filename.split('.').pop() || '';
};
const allowFilesName = ['js', 'css', 'json', 'html'];
export const uploadFile = async (uploadFileOpts: UploadFileOpts) => {
  const { appKey, version, filename, text } = uploadFileOpts;
  const extension = getFilenameExtension(filename);
  if (!allowFilesName.includes(extension)) {
    toast.error('文件类型不支持');
    return;
  }
  const file = toFile(text, filename);
  const res = await uploadChunkV2(file, {
    appKey,
    version,
    filename,
    noCheckAppFiles: false,
  });
  return res as any;
};
