import { del, get, set } from 'idb-keyval';
import { uploadFile } from '../../providers/storage/storage.firebase';
export const uploadLocalCharts = ({ element, key, userId }) => {
  return new Promise(async (resolve, reject) => {
    try {
      if (element.tagName && element.tagName.toUpperCase() !== 'deckgo-slide-chart'.toUpperCase()) {
        resolve();
        return;
      }
      const src = element.src;
      if (!src || src === undefined || src === '') {
        resolve();
        return;
      }
      const file = await get(src);
      if (!file) {
        // We didn't the corresponding image. Instead of crashing an error we go through, user will notice that nothing is displayed.
        // Better than blocking the all process and reaching an intermediate state.
        resolve();
        return;
      }
      // 1. We upload the file to the storage cloud
      const storageFile = await uploadFile({
        data: file,
        folder: 'data',
        maxSize: 10485760,
        userId
      });
      if (!storageFile) {
        reject(`Chart ${src} upload has failed.`);
        return;
      }
      // 2. We update the indexedDB stored slide or paragraph with the new downloadUrl. This stored slide or paragraph will be later updated back to the database.
      const data = await get(key);
      if (!data) {
        reject('Data not found and that is really weird here.');
        return;
      }
      data['data'].attributes.src = storageFile.downloadUrl;
      await set(key, data);
      // 3. We update the DOM
      element.src = storageFile.downloadUrl;
      // 4. All good, we don't need the image in the indexedDB anymore
      await del(src);
      resolve();
    }
    catch (err) {
      reject(err);
    }
  });
};
export const uploadLocalImages = ({ element, key, userId, updateContent }) => {
  return new Promise(async (resolve, reject) => {
    try {
      const childrenImages = element.querySelectorAll('deckgo-lazy-img');
      const nodeImage = element.nodeName.toLowerCase() === 'deckgo-lazy-img' ? element : undefined;
      const imgs = [...Array.from(childrenImages), ...(nodeImage ? [nodeImage] : [])];
      if (!imgs || imgs.length <= 0) {
        resolve();
        return;
      }
      // Filter online images (http...) and deck background (which are cloned from the deck to the slides)
      const list = Array.from(imgs).filter((img) => {
        return (img.imgSrc !== undefined &&
          img.imgSrc !== '' &&
          img.imgSrc.indexOf('http') === -1 &&
          !(img.parentElement && img.parentElement.getAttribute('slot') === 'background' && !element.hasAttribute('custom-background')));
      });
      if (!list || list.length <= 0) {
        resolve();
        return;
      }
      const promises = list.map((img) => {
        return uploadLocalImage({ img, key, userId, updateContent });
      });
      await Promise.all(promises);
      resolve();
    }
    catch (err) {
      reject(err);
    }
  });
};
const uploadLocalImage = ({ img, key, userId, updateContent }) => {
  return new Promise(async (resolve, reject) => {
    try {
      const file = await get(img.imgSrc);
      if (!file) {
        // We didn't the corresponding image. Instead of crashing an error we go through, user will notice that nothing is displayed.
        // Better than blocking the all process and reaching an intermediate state.
        resolve();
        return;
      }
      // 1. We upload the file to the storage cloud
      const storageFile = await uploadFile({
        data: file,
        folder: 'images',
        maxSize: 10485760,
        userId
      });
      if (!storageFile) {
        reject(`Image ${img.imgSrc} upload has failed.`);
        return;
      }
      // 2. We update the indexedDB stored slide or paragraph with the new downloadUrl. This stored slide or paragraph will be later uploaded back to the database.
      const data = await get(key);
      if (!data) {
        reject('Data not found and that is really weird here.');
        return;
      }
      const updateData = updateContent({ data, imgSrc: img.imgSrc, downloadUrl: storageFile.downloadUrl });
      await set(key, updateData);
      // 3. We update the DOM
      img.imgSrc = storageFile.downloadUrl;
      img.imgAlt = storageFile.downloadUrl;
      // 4. All good, we don't need the image in the indexedDB anymore
      await del(img.imgSrc);
      resolve();
    }
    catch (err) {
      reject(err);
    }
  });
};
