import { p as Me } from "./chunk-AFVJZPLS.js";
import { Kind } from "@sinclair/typebox";
/*! Upstart.gg - Copyright (C) 2024 Flippable - https://github.com/upstart-gg/upstart/blob/main/LICENSE */
function c(e) {
  if (!("enum" in e)) return e.anyOf ?? e.oneOf;
  let { enum: o, enumNames: a, "ui:icons": t } = e;
  return o.map((n, i) => ({ const: n, title: a ? a[i] : n, "ui:icon": t ? t[i] : void 0 }));
}
function S(e, o) {
  if (e.type === "object" && "properties" in e) {
    let a = e, t = {};
    for (let [n, i] of Object.entries(a.properties)) {
      let r = u(i, o);
      r !== void 0 && (t[n] = r);
    }
    return t;
  }
  if (e.type === "array" && "items" in e) {
    let a = e;
    return o && typeof a[`default:${o}`] < "u" ? a[`default:${o}`] : a.default ? a.default : [];
  }
  throw new Error("Schema must be either TObject or TArray");
}
function u(e, o) {
  if (o && typeof e[`default:${o}`] < "u") return e[`default:${o}`];
  if ("default" in e && e.default !== void 0) return e.default;
  if (e.type === "object" && "properties" in e) {
    let a = e, t = {};
    for (let [n, i] of Object.entries(a.properties)) {
      let r = u(i, o);
      r !== void 0 && (t[n] = r);
    }
    return Object.keys(t).length > 0 ? t : void 0;
  }
  e.type === "array" && "items" in e;
}
function T(e, o) {
  function a(t) {
    let n = {};
    for (let [i, r] of Object.entries(t.properties)) if (o(r)) n[i] = r;
    else if (r.type === "object" && r.properties) {
      let s = a(r);
      Object.keys(s).length > 0 && Object.assign(n, s);
    }
    return n;
  }
  return a(e);
}
function b(e) {
  return f(e, /* @__PURE__ */ new Set());
}
function f(e, o) {
  if (!e || typeof e != "object") return e;
  if ("$ref" in e && typeof e.$ref == "string") {
    if (o.has(e.$ref) || e[Kind] === "This") return e;
    let t = Me.getSchema(e.$ref)?.schema;
    if (!t) return console.error("Schema not found for reference:", e), e;
    o.add(e.$ref);
    let { $ref: n, ...i } = e, r = { ...f(t, o), ...i };
    return o.delete(e.$ref), r;
  }
  if ("type" in e && e.type === "object" && "properties" in e) {
    let t = {};
    if (e.properties && typeof e.properties == "object") for (let [i, r] of Object.entries(e.properties)) r && typeof r == "object" ? t[i] = f(r, o) : t[i] = r;
    let n = { ...e };
    if (Object.keys(t).length > 0 && (n.properties = t), "additionalProperties" in e && e.additionalProperties && typeof e.additionalProperties == "object" && e.additionalProperties !== true && (n.additionalProperties = f(e.additionalProperties, o)), "patternProperties" in e && e.patternProperties && typeof e.patternProperties == "object") {
      let i = {};
      for (let [r, s] of Object.entries(e.patternProperties)) s && typeof s == "object" ? i[r] = f(s, o) : i[r] = s;
      n.patternProperties = i;
    }
    return n;
  }
  if ("type" in e && e.type === "array") {
    let t = { ...e };
    return "items" in e && e.items && (Array.isArray(e.items) ? t.items = e.items.map((n) => f(n, o)) : typeof e.items == "object" && (t.items = f(e.items, o))), "additionalItems" in e && e.additionalItems && typeof e.additionalItems == "object" && e.additionalItems !== true && (t.additionalItems = f(e.additionalItems, o)), t;
  }
  if ("anyOf" in e && Array.isArray(e.anyOf)) {
    let t = e.anyOf.map((n) => f(n, o));
    return { ...e, anyOf: t };
  }
  if ("oneOf" in e && Array.isArray(e.oneOf)) {
    let t = e.oneOf.map((n) => f(n, o));
    return { ...e, oneOf: t };
  }
  if ("allOf" in e && Array.isArray(e.allOf)) {
    let t = e.allOf.map((n) => f(n, o));
    return { ...e, allOf: t };
  }
  if ("not" in e && e.not && typeof e.not == "object") {
    let t = f(e.not, o);
    return { ...e, not: t };
  }
  if ("if" in e || "then" in e || "else" in e) {
    let t = { ...e };
    return "if" in e && e.if && typeof e.if == "object" && (t.if = f(e.if, o)), "then" in e && e.then && typeof e.then == "object" && (t.then = f(e.then, o)), "else" in e && e.else && typeof e.else == "object" && (t.else = f(e.else, o)), t;
  }
  if ("definitions" in e || "$defs" in e) {
    let t = { ...e };
    if ("definitions" in e && e.definitions && typeof e.definitions == "object") {
      let n = {};
      for (let [i, r] of Object.entries(e.definitions)) r && typeof r == "object" ? n[i] = f(r, o) : n[i] = r;
      t.definitions = n;
    }
    if ("$defs" in e && e.$defs && typeof e.$defs == "object") {
      let n = {};
      for (let [i, r] of Object.entries(e.$defs)) r && typeof r == "object" ? n[i] = f(r, o) : n[i] = r;
      t.$defs = n;
    }
    return t;
  }
  let a = { ...e };
  for (let [t, n] of Object.entries(e)) n && typeof n == "object" && !Array.isArray(n) ? ("type" in n || "$ref" in n || "properties" in n || "items" in n) && (a[t] = f(n, o)) : Array.isArray(n) && n.some((r) => r && typeof r == "object" && ("type" in r || "$ref" in r || "properties" in r || "items" in r)) && (a[t] = n.map((r) => r && typeof r == "object" ? f(r, o) : r));
  return a;
}
export {
  c as a,
  S as b,
  T as c,
  b as d
};
//# sourceMappingURL=chunk-QGYZ7CG5.js.map
