import { isDereferenced as h } from "@scalar/openapi-types/helpers";
import { getResolvedRef as c } from "@scalar/workspace-store/helpers/get-resolved-ref";
function d(i, s) {
  var r, n;
  let o = i;
  const e = (r = s.required) == null ? void 0 : r.includes(i);
  o += e ? " REQUIRED " : " optional ";
  const t = c((n = s.properties) == null ? void 0 : n[i]);
  return t && (o += t.type, t.description && (o += " " + t.description)), o;
}
function y(i) {
  const s = ["Body"], o = c(i == null ? void 0 : i.schema), e = o == null ? void 0 : o.properties;
  return e && Object.keys(e).forEach((t) => {
    if (!i.schema)
      return;
    s.push(d(t, o));
    const r = c(e[t]);
    r.type === "object" && !!r.properties && r.properties && Object.keys(r.properties).forEach((f) => {
      var p, u;
      s.push(`${f} ${(u = c((p = r.properties) == null ? void 0 : p[f])) == null ? void 0 : u.type}`);
    });
  }), s;
}
function q(i) {
  var s, o;
  try {
    const e = (o = (s = c(i == null ? void 0 : i.requestBody)) == null ? void 0 : s.content) == null ? void 0 : o["application/json"];
    if (!e)
      throw new Error("Body not found");
    return y(e);
  } catch {
    return !1;
  }
}
function a(i) {
  var o, e;
  if (!i)
    return {};
  const s = (
    // OpenAPI 3.x
    Object.keys(((o = i == null ? void 0 : i.components) == null ? void 0 : o.schemas) ?? {}).length ? (e = i == null ? void 0 : i.components) == null ? void 0 : e.schemas : (
      // Fallback
      {}
    )
  );
  return Object.keys(s ?? {}).forEach((t) => {
    var r, n;
    (((r = s[t]) == null ? void 0 : r["x-internal"]) === !0 || ((n = s[t]) == null ? void 0 : n["x-scalar-ignore"]) === !0) && delete s[t];
  }), s;
}
function l(i, s) {
  for (const [o, e] of Object.entries(i))
    e !== null && typeof e == "object" ? (s[o] ?? (s[o] = new e.__proto__.constructor()), l(e, s[o])) : typeof e < "u" && (s[o] = e);
  return s;
}
function b(i) {
  return l(i ?? {}, {
    info: {
      title: "",
      description: "",
      termsOfService: "",
      version: "",
      license: {
        name: "",
        url: ""
      },
      contact: {
        email: ""
      }
    },
    servers: [],
    tags: []
  });
}
function v(i) {
  const s = {
    path: [],
    query: [],
    header: [],
    cookie: [],
    body: [],
    formData: []
  }, o = i.parameters ?? [];
  return o && o.forEach((e) => {
    h(e) && (typeof e == "object" && e !== null && "$ref" in e || (e.in === "path" ? s.path.push(e) : e.in === "query" ? s.query.push(e) : e.in === "header" ? s.header.push(e) : e.in === "cookie" ? s.cookie.push(e) : e.in === "body" ? s.body.push(e) : e.in === "formData" && s.formData.push(e)));
  }), s;
}
export {
  b as createEmptySpecification,
  v as createParameterMap,
  l as deepMerge,
  q as extractRequestBody,
  d as formatProperty,
  a as getModels
};
