import { del, get } from 'idb-keyval';
import { SlideTemplate } from '@deckdeckgo/editor';
import { uploadFileIC } from '../providers/storage/storage.ic';
const imagesRegex = /((<deckgo-lazy-img.*?)(img-src=)(.*?")(.*?[^"]*)(.*?"))/g;
export const uploadDeckBackgroundAssets = async ({ deck, identity, host, syncWindow }) => {
  const { background } = deck.data;
  if (!background) {
    return {
      src: undefined,
      storageFile: undefined
    };
  }
  const results = [...background.matchAll(imagesRegex)];
  // Only one image in the background is currently supported
  if ((results === null || results === void 0 ? void 0 : results.length) !== 1) {
    return {
      src: undefined,
      storageFile: undefined
    };
  }
  const imgSrc = results[0][5];
  return uploadData({
    src: imgSrc,
    deckId: deck.id,
    identity,
    host,
    syncWindow,
    msg: 'deckdeckgo_sync_deck_background',
    folder: 'images'
  });
};
export const uploadSlideAssets = async ({ deckId, slide, identity, host, syncWindow }) => {
  const images = await uploadSlideImages({
    deckId,
    slide,
    identity,
    host,
    syncWindow
  });
  const chart = await uploadSlideChart({ deckId, slide, identity, host, syncWindow });
  return {
    images,
    chart
  };
};
const uploadSlideImages = async ({ deckId, slide, identity, host, syncWindow }) => {
  const { content } = slide.data;
  if (!content) {
    return undefined;
  }
  const results = [...content.matchAll(imagesRegex)];
  const promises = results === null || results === void 0 ? void 0 : results.map((result) => {
    const imgSrc = result[5];
    return uploadData({
      src: imgSrc,
      deckId,
      slideId: slide.id,
      identity,
      host,
      syncWindow,
      msg: 'deckdeckgo_sync_slide_image',
      folder: 'images'
    });
  });
  return Promise.all(promises);
};
const uploadSlideChart = async ({ deckId, slide, identity, host, syncWindow }) => {
  const { content, template, attributes } = slide.data;
  if (!content || template !== SlideTemplate.CHART || !attributes) {
    return undefined;
  }
  const { src } = attributes;
  return uploadData({ src, deckId, slideId: slide.id, identity, host, syncWindow, msg: 'deckdeckgo_sync_slide_chart', folder: 'data' });
};
const uploadData = ({ src, deckId, slideId, host, identity, syncWindow, msg, folder }) => {
  return new Promise(async (resolve, reject) => {
    try {
      if (!src || src === '' || /^https?:\/\/.+\/.+$/.test(src)) {
        resolve({
          src: undefined,
          storageFile: undefined
        });
        return;
      }
      const data = await get(src);
      if (!data) {
        // 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({
          src: undefined,
          storageFile: undefined
        });
        return;
      }
      // 1. We upload the file to the storage cloud
      const storageFile = await uploadFileIC({
        data,
        folder,
        maxSize: 10485760,
        identity,
        host
      });
      if (!storageFile) {
        reject(`Data ${src} upload has failed.`);
        return;
      }
      // 2. We update the DOM and IDB (currently saved data)
      await syncWindow({
        msg,
        data: {
          src,
          deckId,
          slideId,
          storageFile
        }
      });
      // 3. All good, we don't need the image in the indexedDB anymore
      await del(src);
      resolve({
        src,
        storageFile
      });
    }
    catch (err) {
      reject(err);
    }
  });
};
