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