import { z as i } from "../jszip.min-BZhlzntC.js";
import u from "./oldZipLoad.js";
import d from "./newZipLoad.js";
import w from "./loadHF.js";
import { loadZipMeta as s } from "./loadZipMeta.js";
import { load_safetensors as g } from "../utilities/safetensors.js";
const j = 2;
async function h(e) {
  const t = await fetch(e);
  if (!t.ok)
    throw new Error(`Failed to fetch ${e}: ${t.statusText}`);
  return t.arrayBuffer();
}
async function p(e, t, o) {
  const a = e.file("model.safetensors");
  if (!a)
    return;
  const r = await a.async("arraybuffer"), c = await g(r), n = /* @__PURE__ */ new Map();
  for (const [l, m] of Object.entries(c))
    n.set(l, [m]);
  t.weightStore.loadWeights(n, o);
}
async function y(e, t) {
  const o = e.file("config.json");
  if (!o)
    return;
  const a = await o.async("string"), r = JSON.parse(a);
  if (r.loraName) {
    if (t.hasLoRA())
      throw new Error("Model already has LoRA attached");
    t.attachLoRA(r.loraName);
  }
}
async function f(e, t) {
  if (t.reference) {
    const o = await L(t.reference);
    return await p(e, o.model, !!t.url), await y(e, o.model), o.model.config.loraName && o.model.attachLoRA(o.model.config.loraName), {
      ...o,
      metaData: {
        ...o.metaData,
        ...t
      }
    };
  } else {
    if (e.file("manifest.json"))
      return u(e, t);
    {
      const o = await d(e, t);
      return o.model.config.loraName && o.model.attachLoRA(o.model.config.loraName), o;
    }
  }
}
async function L(e, t) {
  if (typeof e == "string")
    if (e.startsWith("http://") || e.startsWith("https://")) {
      const o = await h(e), a = await i.loadAsync(o), r = await s(a);
      return r.url = e, f(a, r);
    } else
      return w(e);
  else {
    const o = await i.loadAsync(e), a = await s(o);
    return a.url = t?.sourceURL || void 0, f(o, a);
  }
}
export {
  j as VERSION,
  L as loadModel
};
