import { fetchSpecFromUrl as p, prettyPrintJson as l } from "@scalar/oas-utils/helpers";
import { ref as f, reactive as y, watch as u, toValue as a } from "vue";
import { parse as S } from "../helpers/parse.js";
import { isValidUrl as d } from "../helpers/isValidUrl.js";
import { createEmptySpecification as m } from "../helpers/createEmptySpecification.js";
const g = async ({ url: r, content: e }, i) => {
  if (r)
    return d(r) ? await p(r, i) : await p(r);
  const t = typeof e == "function" ? e() : e;
  if (typeof t == "string")
    return t;
  if (typeof t == "object")
    return l(t);
};
function E({
  specConfig: r,
  proxy: e
}) {
  const i = f(""), t = y(m()), c = f(null);
  function v(o) {
    return o ? S(o, {
      proxy: e ? a(e) : void 0
    }).then((n) => {
      c.value = null, Object.assign(t, {
        servers: [],
        ...n
      });
    }).catch((n) => {
      c.value = n.toString();
    }) : Object.assign(t, m());
  }
  return u(
    () => a(r),
    async (o) => {
      var n;
      if (o) {
        const s = (n = await g(o, a(e))) == null ? void 0 : n.trim();
        typeof s == "string" && (i.value = s);
      }
    },
    { immediate: !0, deep: !0 }
  ), u(i, () => {
    v(i.value);
  }), {
    rawSpec: i,
    parsedSpec: t,
    specErrors: c
  };
}
export {
  E as useReactiveSpec
};
