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