import { isAncestor } from "@likec4/core";
import { elementRef } from "../utils/elementRef.js";
import { tryOrLog } from "./_shared.js";
export const dynamicViewStep = (services) => {
  const fqnIndex = services.likec4.FqnIndex;
  return tryOrLog((el, accept) => {
    const sourceEl = elementRef(el.source);
    const source = sourceEl && fqnIndex.getFqn(sourceEl);
    if (!source) {
      accept("error", "Source not found (not parsed/indexed yet)", {
        node: el,
        property: "source"
      });
    }
    const targetEl = elementRef(el.target);
    const target = targetEl && fqnIndex.getFqn(targetEl);
    if (!target) {
      accept("error", "Target not found (not parsed/indexed yet)", {
        node: el,
        property: "target"
      });
    }
    if (source && target && (isAncestor(source, target) || isAncestor(target, source))) {
      accept("error", "Invalid parent-child relationship", {
        node: el
      });
    }
  });
};
