import { stringify as $ } from "flatted";
function s(t) {
  if (!t)
    return null;
  if ("title" in t && t.title)
    return t.title;
  if ("name" in t && t.name)
    return t.name;
  if ("$ref" in t) {
    const n = t.$ref.match(/\/([^\/]+)$/);
    if (n)
      return n[1];
  }
  if ("type" in t && t.type === "array" && "items" in t && t.items)
    return `Array of ${"type" in t.items && t.items.type || "any"}`;
  if ("type" in t && t.type)
    return Array.isArray(t.type) ? t.type.join(" | ") : t.type;
  if (typeof t == "object") {
    const r = Object.keys(t);
    if (r.length > 0)
      return r[0];
  }
  return null;
}
function b(t, r) {
  var n, y;
  if (!t || !r || typeof r != "object" || t.type !== "array" && t.type !== "object")
    return null;
  for (const [e, i] of Object.entries(r))
    if (i.type === t.type && (t.type === "array" && ((n = i.items) == null ? void 0 : n.type) === ((y = t.items) == null ? void 0 : y.type) || t.type === "object" && i.properties && t.properties && $(i.properties) === $(t.properties)))
      return e;
  return null;
}
function p(t, r) {
  return t === "array" ? `${t} ${r}[]` : `${t} ${r}`;
}
function u(t, r, n = !1, y) {
  var o;
  if (!(t != null && t.type))
    return null;
  if (n)
    return t.type === "array" && ((o = t.items) != null && o.type) ? `array ${t.items.type}[]` : null;
  const e = s(t);
  if (e && (t.title || t.name))
    return t.type === "array" ? `array ${e}[]` : e;
  const i = b(t, r);
  if (i)
    return t.type === "array" ? `array ${i}[]` : i;
  if (t.type === "array" && t.items) {
    if (y) {
      const m = y(t.items, r);
      if (m)
        return p(t.type, m);
    }
    if (t.items.title || t.items.name)
      return p(t.type, t.items.title || t.items.name);
    const f = s(t.items);
    return f && f !== t.items.type ? p(t.type, f) : t.items.type ? p(t.type, t.items.type) : p(t.type, "object");
  }
  return e && e !== t.type ? e.startsWith("Array of ") ? `array ${e.replace("Array of ", "")}[]` : e : null;
}
export {
  p as formatTypeWithModel,
  u as getModelName,
  s as getModelNameFromSchema,
  b as getSchemaNameFromSchemas
};
