import { AstUtils } from "langium";
import { isNullish } from "remeda";
import { ast } from "../ast.js";
export function instanceRef(deploymentRef) {
  let referenceable;
  while (referenceable = deploymentRef.value?.ref) {
    if (ast.isDeploymentNode(referenceable)) {
      return null;
    }
    if (ast.isDeployedInstance(referenceable)) {
      return referenceable;
    }
    if (isNullish(deploymentRef.parent)) {
      return null;
    }
    deploymentRef = deploymentRef.parent;
  }
  return null;
}
export function deploymentNodeRef(deploymentRef) {
  let referenceable = deploymentRef.value.ref ?? null;
  if (!referenceable || ast.isDeploymentNode(referenceable)) {
    return referenceable;
  }
  const artifact = instanceRef(deploymentRef);
  return artifact ? AstUtils.getContainerOfType(artifact, ast.isDeploymentNode) ?? null : null;
}
export function isReferenceToLogicalModel(node) {
  while (node.parent) {
    node = node.parent;
  }
  return ast.isElement(node.value.ref);
}
export function isReferenceToDeploymentModel(node) {
  let referenceable;
  while (referenceable = node.value?.ref) {
    if (ast.isDeploymentElement(referenceable)) {
      return true;
    }
    if (isNullish(node.parent)) {
      return false;
    }
    node = node.parent;
  }
  return false;
}
