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