function n(f, i, p = 0) {
  if (p >= 20 || !Array.isArray(f) || f.length === 0)
    return i || {};
  const e = f.reduce((t, r) => {
    if (!r || typeof r != "object")
      return t;
    if (r.allOf) {
      const s = n(r.allOf, void 0, p + 1);
      return n([t, s], void 0, p + 1);
    }
    const o = typeof t == "object" ? { ...t } : {};
    if (r.properties && (o.properties = l(o.properties || {}, r.properties, p + 1)), r.items) {
      if (r.type === "array")
        o.items = d(o.items || {}, r.items, p + 1);
      else if (typeof r.type == "string" && ["object", "string"].includes(r.type) && r.items.allOf) {
        const s = n(r.items.allOf, void 0, p + 1);
        o.properties = l(
          o.properties || {},
          s.properties || {},
          p + 1
        );
      }
    }
    return O(o, r, p + 1);
  }, {});
  return i ? O(e, i, p + 1) : e;
}
function l(f, i, p = 0) {
  if (p >= 20)
    return f;
  const e = typeof f == "object" ? { ...f } : {};
  return Object.entries(i).forEach(([t, r]) => {
    var o;
    if (!r || typeof r != "object") {
      e[t] = r;
      return;
    }
    if (!e[t]) {
      r.type === "array" && ((o = r.items) != null && o.allOf) ? e[t] = {
        ...r,
        items: n(r.items.allOf, void 0, p + 1)
      } : r.allOf ? e[t] = n(r.allOf, void 0, p + 1) : e[t] = r;
      return;
    }
    if (r.allOf)
      e[t] = n([e[t], ...r.allOf], void 0, p + 1);
    else if (r.type === "array" && r.items)
      e[t] = {
        ...e[t],
        type: "array",
        items: d(e[t].items || {}, r.items, p + 1)
      };
    else {
      const s = e[t].properties || r.properties ? l(e[t].properties || {}, r.properties || {}, p + 1) : void 0;
      e[t] = {
        ...e[t],
        ...r
      }, s && (e[t].properties = s);
    }
  }), e;
}
function d(f, i, p = 0) {
  if (p >= 20)
    return f;
  if (f.allOf || i.allOf) {
    const t = [...f.allOf || [f], ...i.allOf || [i]];
    return n(t, void 0, p + 1);
  }
  const e = {
    ...f,
    ...i
  };
  return f.properties && i.properties && (e.properties = l(f.properties, i.properties, p + 1)), e;
}
const O = (f, i, p = 0) => {
  if (p >= 20)
    return f;
  const e = typeof f == "object" ? { ...f } : {};
  if (i.required && Array.isArray(i.required)) {
    const t = f.required || [], r = i.required;
    e.required = [.../* @__PURE__ */ new Set([...t, ...r])];
  }
  return i.type && !f.type && (e.type = i.type), i.title && !f.title && (e.title = i.title), i.name && !f.name && (e.name = i.name), i.description && !f.description && (e.description = i.description), i.properties && (e.properties = l(e.properties || {}, i.properties, p + 1)), ["oneOf", "anyOf"].forEach((t) => {
    const r = i[t];
    r && r.forEach((o) => {
      o.properties && (e.properties = l(e.properties || {}, o.properties, p + 1));
    });
  }), e;
};
export {
  n as mergeAllOfSchemas
};
