/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import { x as D } from "./interfaces.js";
import { getItemRelatedItems as g } from "@esri/solution-common";
async function R(o, r) {
  const s = await h(r, o);
  let l = [], a = [], d = [];
  const c = s.map(async (e) => {
    const m = await S(e.id, "reverse", "Solution2Item", o);
    return { template: e, relatedItems: m };
  }), p = await Promise.all(c);
  for (const { template: e, relatedItems: m } of p)
    for (const f of m) {
      const I = [];
      for (const t of f.relatedItemIds)
        I.push({
          controlItemId: e.id,
          dependencyItemId: t
        });
      const y = I.reduce((t, n) => (t[n.controlItemId] || (t[n.controlItemId] = /* @__PURE__ */ new Set()), t[n.controlItemId].add(n.dependencyItemId), t), {}), i = Object.entries(y).filter(([t, n]) => n.size > 1).flatMap(
        ([t, n]) => Array.from(n).map((w) => ({
          controlItemId: t,
          dependencyItemId: w
        }))
      );
      i.length > 0 && (d = d.concat(i));
    }
  a = Array.from(
    new Map(
      [
        ...d
      ].map((e) => [`${e.controlItemId}:${e.dependencyItemId}`, e])
    ).values()
  );
  const u = a.map((e) => e.controlItemId);
  return l = [...new Set(u)], {
    uniqueIitemIds: l,
    _nonSolutionDependencyItems: a,
    templateInfos: s
  };
}
async function S(o, r, s, l) {
  const a = await g(o, s, r, l);
  return [{
    relationshipType: s,
    relatedItemIds: a.relatedItems.map((d) => d.id)
  }];
}
async function h(o, r) {
  return await D(o.id, r);
}
export {
  R as g,
  h as p
};
