import { createDeckBucketActor, createManagerActor, initDeckBucket } from '../utils/deck.utils';
import { initIdentity } from '../utils/identity.utils';
import { toArray, toTimestamp } from '../utils/did.utils';
export const uploadWorker = async ({ internetIdentity: { delegationChain, identityKey }, syncData, host }) => {
  if (!syncData) {
    return;
  }
  if (!delegationChain || !identityKey) {
    return;
  }
  const identity = initIdentity({ identityKey, delegationChain });
  const { updateDecks, updateSlides, deleteSlides: slidesToDelete } = syncData;
  const managerActor = await createManagerActor({ identity, host });
  await uploadDecks({ updateDecks, identity, managerActor, host });
  await uploadSlides({ updateSlides, identity, managerActor, host });
  await deleteSlides({ deleteSlides: slidesToDelete, identity, managerActor, host });
  // TODO: handle delete decks here?
};
const uploadDecks = async ({ updateDecks, identity, managerActor, host }) => {
  if (!updateDecks || updateDecks.length <= 0) {
    return;
  }
  const promises = updateDecks.map(({ deck }) => uploadDeck({ deck, managerActor: managerActor, identity, host }));
  await Promise.all(promises);
  console.log('C synced');
};
const uploadDeck = async ({ deck, managerActor, identity, host }) => {
  if (!deck) {
    return;
  }
  console.log('Deck IC about to SET');
  const t0 = performance.now();
  const bucket = await initDeckBucket({ managerActor, deckId: deck.id });
  const deckBucket = await createDeckBucketActor({ identity, bucket, host });
  await deckBucket.set({
    deckId: deck.id,
    data: await toArray(deck.data),
    created_at: toTimestamp(deck.data.created_at || new Date()),
    updated_at: toTimestamp(deck.data.updated_at || new Date())
  });
  const t1 = performance.now();
  console.log('Deck IC SET done', t1 - t0);
  const t2 = performance.now();
  // TODO: remove, just for test
  console.log('Deck IC Get:', await deckBucket.get(), performance.now() - t2);
};
const uploadSlides = async ({ updateSlides, identity, managerActor, host }) => {
  if (!updateSlides || updateSlides.length <= 0) {
    return;
  }
  const promises = updateSlides.map(({ slide, deckId }) => uploadSlide({ slide, deckId, managerActor: managerActor, identity, host }));
  await Promise.all(promises);
};
const deleteSlides = async ({ deleteSlides, identity, managerActor, host }) => {
  if (!deleteSlides || deleteSlides.length <= 0) {
    return;
  }
  const promises = deleteSlides.map(({ deckId, slideId }) => deleteSlide({ slideId, deckId, identity, managerActor: managerActor, host }));
  await Promise.all(promises);
};
const uploadSlide = async ({ slide, deckId, managerActor, identity, host }) => {
  if (!slide) {
    return;
  }
  console.log('Slide IC about to SET');
  const t0 = performance.now();
  const t4 = performance.now();
  const bucket = await initDeckBucket({ managerActor, deckId });
  const t5 = performance.now();
  console.log('Bucket retrieved', t5 - t4);
  const deckBucket = await createDeckBucketActor({ identity, bucket, host });
  await deckBucket.setSlide({
    slideId: slide.id,
    data: await toArray(slide.data),
    created_at: toTimestamp(slide.data.created_at || new Date()),
    updated_at: toTimestamp(slide.data.updated_at || new Date())
  });
  const t1 = performance.now();
  console.log('Slide IC SET done', t1 - t0);
  const t2 = performance.now();
  // TODO: remove, just for test
  console.log('Slide IC Get:', await deckBucket.getSlide(slide.id), performance.now() - t2);
};
const deleteSlide = async ({ slideId, deckId, managerActor, identity, host }) => {
  if (!slideId) {
    return;
  }
  console.log('Slide IC about to DEL');
  const t0 = performance.now();
  const bucket = await initDeckBucket({ managerActor, deckId });
  const deckBucket = await createDeckBucketActor({ identity, bucket, host });
  await deckBucket.delSlide(slideId);
  const t1 = performance.now();
  console.log('Slide IC DEL done', t1 - t0);
};
