import { getDownloadURL, getStorage, ref, uploadBytes } from "firebase/storage";
import getExtension from "./getExtension";
import resizeImage from "./resizeImage";
export default async function uploadFiles(app, files, options) {
  var _a;
  const results = [];
  const storage = getStorage(app);
  for (const file of files) {
    if (typeof (options === null || options === void 0 ? void 0 : options.onBeforeUpload) === "function")
      options.onBeforeUpload(file);
    try {
      const storageRef = ref(storage, `${(options === null || options === void 0 ? void 0 : options.path) || ""}${new Date().toISOString()}.${await getExtension(file === null || file === void 0 ? void 0 : file.name)}`);
      const upload = await uploadBytes(storageRef, ((_a = file === null || file === void 0 ? void 0 : file.type) === null || _a === void 0 ? void 0 : _a.match(/image.*/)) ? await resizeImage(file, 1080) : file);
      const url = await getDownloadURL(upload.ref);
      results.push({
        success: true,
        name: file === null || file === void 0 ? void 0 : file.name,
        url,
      });
    }
    catch (error) {
      results.push({ succes: false, error });
    }
    if (typeof (options === null || options === void 0 ? void 0 : options.onAfterUpload) === "function")
      options.onAfterUpload(file);
  }
  return results;
}
