import { toNullable } from './did.utils';
import { getStorageActor, upload } from './storage.utils';
const kitPath = 'https://raw.githubusercontent.com/deckgo/ic-kit/main/dist';
const kit = [
  {
    src: `${kitPath}/workbox-c837f436.js`,
    mimeType: 'text/javascript'
  },
  {
    src: `${kitPath}/service-worker.js`,
    mimeType: 'text/javascript'
  },
  {
    src: `${kitPath}/robots.txt`,
    mimeType: 'text/plain'
  },
  {
    src: `${kitPath}/manifest.webmanifest`,
    mimeType: 'application/manifest+json',
    updateContent: ({ content, meta }) => { var _a; return content.replace('{{DECKDECKGO_AUTHOR}}', ((_a = meta === null || meta === void 0 ? void 0 : meta.author) === null || _a === void 0 ? void 0 : _a.name) || 'DeckDeckGo'); }
  },
  {
    src: `${kitPath}/build/index-M763JKRZ.css`,
    mimeType: 'text/css'
  },
  {
    src: `${kitPath}/build/index-55VE2P7H.js`,
    mimeType: 'text/javascript'
  },
  {
    src: `${kitPath}/build/deck/index-ELSPH57U.js`,
    mimeType: 'text/javascript'
  },
  {
    src: `${kitPath}/build/deck/index-HPLMJ2FO.css`,
    mimeType: 'text/css'
  },
  {
    src: `${kitPath}/build/doc/index-RZB27UR5.css`,
    mimeType: 'text/css'
  }
].map((resource) => {
  const { pathname } = new URL(resource.src);
  return Object.assign(Object.assign({}, resource), { filename: pathname.split('/').pop(), headers: [['Cache-Control', 'max-age=31536000']] });
});
export const uploadResources = async ({ meta }) => {
  // 1. Get actor
  const { actor } = await getStorageActor();
  const assetKeys = await actor.list(toNullable('resources'));
  const keys = assetKeys.map(({ name }) => name);
  // We only upload resources that have not been yet uploaded. In other words: we upload the resources the first time or if hashes are modified.
  const kitFiles = kit.filter(({ filename }) => !keys.includes(filename));
  if (!kitFiles || kitFiles.length <= 0) {
    return;
  }
  const promises = kitFiles.map((kit) => addKitIC({ kit, actor, meta }));
  await Promise.all(promises);
  // If there was an update, we ensure we also update the sw list
  await addSwKitIC({ kitFiles, actor, meta });
};
const addKitIC = async ({ kit, actor, meta }) => {
  const { src, filename, mimeType, updateContent, headers } = kit;
  const content = await downloadKit(src);
  const updatedContent = updateContent ? updateContent({ content, meta }) : content;
  await uploadKit({ filename, content: updatedContent, actor, mimeType, headers, fullPath: src.replace(kitPath, '') });
};
const addSwKitIC = async ({ kitFiles, actor, meta }) => {
  const sw = kitFiles.find(({ filename }) => filename === 'service-worker.js');
  if (sw !== undefined) {
    return;
  }
  const swKit = kit.find(({ filename }) => filename === 'service-worker.js');
  if (!swKit !== undefined) {
    return;
  }
  await addKitIC({ kit: swKit, actor, meta });
};
const uploadKit = async ({ filename, fullPath, content, actor, mimeType, headers }) => {
  await upload({
    data: new Blob([content], { type: mimeType }),
    filename,
    folder: 'resources',
    storageActor: actor,
    fullPath,
    headers
  });
};
const downloadKit = async (src) => {
  const htmlTemplate = await fetch(src);
  return htmlTemplate.text();
};
