import { ComputedNode, ComputedView } from "@likec4/core";
import { find, isNullish } from "remeda";
export function assignNavigateTo(views) {
  const allElementViews = /* @__PURE__ */ new Map();
  for (const v of views) {
    if (ComputedView.isElement(v) && v.viewOf && isNullish(v.extends)) {
      const viewsOf = allElementViews.get(v.viewOf) ?? [];
      viewsOf.push(v.id);
      allElementViews.set(v.viewOf, viewsOf);
    }
  }
  for (const { id, nodes } of views) {
    for (const node of nodes) {
      const modelRef = ComputedNode.modelRef(node);
      if (node.navigateTo || !modelRef) {
        continue;
      }
      const navigateTo = find(allElementViews.get(modelRef) ?? [], (v) => v !== id);
      if (navigateTo) {
        node.navigateTo = navigateTo;
      }
    }
  }
  return views;
}
