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