import { __internal_sha256 as shaUnbundledRegular } from "@huggingface/hub-unbundled-regular";
import { __internal_sha256 as shaUnbundled } from "@huggingface/hub-unbundled";
import { __internal_sha256 as shaRegular } from "@huggingface/hub-regular";
import { __internal_sha256 as shaEsm } from "@huggingface/hub-esm";
import { __internal_sha256 as sha } from "@huggingface/hub";

const lfsContent = "0123456789".repeat(1_000_000);

const shas = {
  "hub-unbundled-regular": shaUnbundledRegular,
  "hub-unbundled": shaUnbundled,
  "hub-regular": shaRegular,
  "hub-esm": shaEsm,
  "hub": sha
}

async function test(id) {
  const blob = new Blob([lfsContent]);

  try {
    const iterator = shas[id](blob, { useWebWorker: { minSize: 1 } });
    // Get return value of the generator
    while (1) {
        const { done, value } = await iterator.next();
        if (done) {
            document.getElementById(id).textContent = value;

            const builtInResult = await crypto.subtle.digest("SHA-256", await blob.arrayBuffer());
            const hex =
                builtInResult instanceof ArrayBuffer
                    ? new Uint8Array(builtInResult).reduce((acc, i) => acc + i.toString(16).padStart(2, "0"), "")
                    : builtInResult;

          
            break;
        }

    }
  } catch (err) {
    document.getElementById(id).textContent = err.message;
  }
}
window.document.addEventListener("DOMContentLoaded", () => {
  test("hub-unbundled-regular");
  test("hub-unbundled");
  test("hub-regular");
  test("hub-esm");
  test("hub");
});