export type ExtendedFile =
  | {
      file: File;
      filetype: 'image';
      preview: string;
      blob?: Blob | null;
    }
  | {
      file: File;
      filetype: 'file';
    };

/**
 * ファイルが画像 (HEIC/HEIF を含む) かどうかを MIME タイプで判定する。
 */
export const isImageFile = (file: File): boolean => /(heic|heif)/i.test(file.type) || /image.*/.test(file.type);

export default function readFiles(files: File[] | FileList): Promise<ExtendedFile[]> {
  const promises: Promise<ExtendedFile>[] = [];
  Array.from(files).forEach((f) => {
    const promise = new Promise<ExtendedFile>((resolve, reject) => {
      const objFileReader = new FileReader();
      if (/(heic|heif)/i.test(f.type)) {
        let convertedBlob: Blob | null = null;
        objFileReader.onload = () => {
          resolve({
            file: f,
            filetype: 'image',
            preview: objFileReader.result as string,
            blob: convertedBlob,
          });
        };
        (async () => {
          const { heicTo } = await import(/* webpackChunkName: "heic-to" */ 'heic-to');
          const blob = await heicTo({
            blob: f,
            type: 'image/jpeg',
          });
          if (blob instanceof Blob) {
            convertedBlob = blob;
            objFileReader.readAsDataURL(blob);
          }
        })();
      } else if (isImageFile(f)) {
        objFileReader.onload = () => {
          resolve({
            file: f,
            filetype: 'image',
            preview: objFileReader.result as string,
          });
        };
        objFileReader.readAsDataURL(f);
      } else {
        objFileReader.onload = () => {
          resolve({
            file: f,
            filetype: 'file',
          });
        };
        objFileReader.readAsDataURL(f);
      }
      objFileReader.onerror = () => {
        reject(new Error(`Failed to read file: ${f.name}`));
      };
    });
    promises.push(promise);
  });
  return Promise.all(promises);
}
