import { fetchDocument as l, prettyPrintJson as d } from "@scalar/oas-utils/helpers";
import { ref as f, watch as p, toValue as u } from "vue";
import { parse as h } from "../helpers/parse.js";
import { createEmptySpecification as m } from "../helpers/createEmptySpecification.js";
const v = async ({ url: r, content: e }, n) => {
  if (r) {
    const i = performance.now();
    try {
      const c = await l(r, n), o = performance.now();
      return console.log(`fetch: ${Math.round(o - i)} ms (${r})`), console.log("size:", Math.round(c.length / 1024), "kB"), c;
    } catch (c) {
      console.error("Failed to fetch OpenAPI document from URL:", c);
    }
  }
  const t = typeof e == "function" ? e() : e;
  if (typeof t == "string")
    return t;
  if (typeof t == "object")
    return d(t);
};
function E({
  specConfig: r,
  proxyUrl: e
}) {
  const n = f(""), t = f(m()), i = f(null);
  function c(o) {
    var s;
    if (!o) {
      t.value = m();
      return;
    }
    h(o, {
      proxyUrl: e ? u(e) : void 0,
      source: (s = u(r)) == null ? void 0 : s.url
    }).then((a) => {
      i.value = null, t.value = a;
    }).catch((a) => {
      i.value = a.toString();
    });
  }
  return p(
    () => u(r),
    async (o) => {
      var s;
      if (o) {
        const a = (s = await v(o, u(e))) == null ? void 0 : s.trim();
        typeof a == "string" && (n.value = a);
      }
    },
    { immediate: !0, deep: !0 }
  ), p(n, () => {
    c(n.value);
  }), {
    rawSpec: n,
    parsedSpec: t,
    specErrors: i
  };
}
export {
  E as useReactiveSpec
};
