import firebase from 'firebase/app';
import { get } from 'idb-keyval';
import { deckSelector } from '@deckdeckgo/editor';
import { prepareAttributes } from '../data/firestore.utils';
import { deleteSlide, updateSlide } from '../../providers/data/slide.firebase';
import { uploadLocalCharts, uploadLocalImages } from './sync.storage.utils';
export const uploadSlides = ({ data, userId }) => {
  return new Promise(async (resolve, reject) => {
    if (!data || data.length <= 0) {
      resolve();
      return;
    }
    try {
      const promises = data.map(({ deckId, slideId }) => uploadSlide({ deckId, slideId, userId }));
      await Promise.all(promises);
      resolve();
    }
    catch (err) {
      reject(err);
    }
  });
};
const uploadSlide = async ({ deckId, slideId, userId }) => {
  await uploadSlideLocalUserAssets({ deckId, slideId, userId });
  await uploadSlideData(deckId, slideId);
};
const uploadSlideLocalUserAssets = ({ deckId, slideId, userId }) => {
  return new Promise(async (resolve, reject) => {
    const slideElement = document.querySelector(`${deckSelector} > *[slide_id="${slideId}"]`);
    if (!slideElement) {
      resolve();
      return;
    }
    try {
      await uploadLocalCharts({ element: slideElement, key: `/decks/${deckId}/slides/${slideId}`, userId });
      const updateContent = ({ data: slide, imgSrc, downloadUrl }) => {
        if (!slide.data.content) {
          return slide;
        }
        slide.data.content = slide.data.content.replace(`img-src="${imgSrc}"`, `img-src="${downloadUrl}"`);
        slide.data.content = slide.data.content.replace(`img-alt="${imgSrc}"`, `img-alt="${downloadUrl}"`);
        return slide;
      };
      await uploadLocalImages({ element: slideElement, key: `/decks/${deckId}/slides/${slideId}`, userId, updateContent });
      resolve();
    }
    catch (err) {
      reject(err);
    }
  });
};
const uploadSlideData = (deckId, slideId) => {
  return new Promise(async (resolve, reject) => {
    try {
      const slide = await get(`/decks/${deckId}/slides/${slideId}`);
      if (!slide || !slide.data) {
        // If upload process end up in error in a previous try, some slides might have already been uploaded correctly and remove from the local db
        resolve();
        return;
      }
      slide.data.attributes = prepareAttributes(slide.data.attributes);
      if (slide.data.content === null) {
        // @ts-ignore
        slide.data.content = firebase.firestore.FieldValue.delete();
      }
      await updateSlide(deckId, slide);
      resolve();
    }
    catch (err) {
      reject(err);
    }
  });
};
export const deleteSlides = async (data) => {
  if (!data || data.length <= 0) {
    return;
  }
  const promises = data.map(({ deckId, slideId }) => deleteSlide(deckId, slideId));
  await Promise.all(promises);
};
