import { Upload } from 'ant-design-vue';

import { prepareImageForUpload } from '#/utils/prepare-image-upload';

const pendingFiles = new Map<string, File>();

export async function beforeImageUpload(file: File) {
  const { file: prepared } = await prepareImageForUpload(file);
  if (!prepared) {
    return Upload.LIST_IGNORE;
  }
  if (prepared !== file) {
    pendingFiles.set(file.uid, prepared);
  }
  return true;
}

export function resolveUploadFile(file: File & { uid?: string }): File {
  const uid = file.uid ?? '';
  const actualFile = pendingFiles.get(uid) ?? file;
  pendingFiles.delete(uid);
  return actualFile;
}
