import EntityRelationshipType from "../../Types/Telemetry/EntityRelationshipType";
import EntityType from "../../Types/Telemetry/EntityType";
/*
 * Directed rules: a `from`-type that co-occurs with a `to`-type implies the
 * given relationship. Only the meaningful direction has a rule (e.g. pod →
 * cluster is `member-of`; cluster → pod yields nothing), so iterating all
 * ordered pairs and keeping the non-null results produces a correctly
 * directed edge set without double-counting. Extend as new entity types
 * graduate from membership-only to first-class.
 *
 * `depends-on` is deliberately absent: a caller service and its callee
 * never share one resource, so it cannot be co-occurrence-inferred. Those
 * edges are derived from cross-service parent/child span pairs by the
 * TelemetryEntity:ComputeServiceDependencies worker cron.
 */
const RULES = {
    [`${EntityType.ServiceInstance}|${EntityType.Service}`]: EntityRelationshipType.InstanceOf,
    [`${EntityType.Service}|${EntityType.Host}`]: EntityRelationshipType.HostedOn,
    [`${EntityType.Service}|${EntityType.KubernetesPod}`]: EntityRelationshipType.RunsOn,
    [`${EntityType.Container}|${EntityType.KubernetesPod}`]: EntityRelationshipType.PartOf,
    [`${EntityType.Container}|${EntityType.Host}`]: EntityRelationshipType.PartOf,
    [`${EntityType.Process}|${EntityType.Container}`]: EntityRelationshipType.PartOf,
    [`${EntityType.Process}|${EntityType.Host}`]: EntityRelationshipType.RunsOn,
    [`${EntityType.KubernetesPod}|${EntityType.KubernetesNode}`]: EntityRelationshipType.RunsOn,
    [`${EntityType.KubernetesPod}|${EntityType.KubernetesNamespace}`]: EntityRelationshipType.MemberOf,
    [`${EntityType.KubernetesPod}|${EntityType.KubernetesCluster}`]: EntityRelationshipType.MemberOf,
    [`${EntityType.KubernetesNode}|${EntityType.KubernetesCluster}`]: EntityRelationshipType.MemberOf,
    [`${EntityType.KubernetesNamespace}|${EntityType.KubernetesCluster}`]: EntityRelationshipType.MemberOf,
    [`${EntityType.KubernetesDeployment}|${EntityType.KubernetesNamespace}`]: EntityRelationshipType.MemberOf,
    [`${EntityType.KubernetesDeployment}|${EntityType.KubernetesCluster}`]: EntityRelationshipType.MemberOf,
};
/** The relationship `fromType` → `toType` implies, or null if none. */
export function inferRelationshipType(fromType, toType) {
    return RULES[`${fromType}|${toType}`] || null;
}
/**
 * Derive every directed relationship edge implied by an entity set (one
 * resource batch). Deduped; self-pairs and same-key pairs skipped.
 */
export function deriveRelationships(entities) {
    const edges = [];
    const seen = new Set();
    for (let i = 0; i < entities.length; i++) {
        for (let j = 0; j < entities.length; j++) {
            if (i === j) {
                continue;
            }
            const from = entities[i];
            const to = entities[j];
            if (from.entityKey === to.entityKey) {
                continue;
            }
            const relationshipType = inferRelationshipType(from.entityType, to.entityType);
            if (!relationshipType) {
                continue;
            }
            const dedupeKey = `${from.entityKey}|${to.entityKey}|${relationshipType}`;
            if (seen.has(dedupeKey)) {
                continue;
            }
            seen.add(dedupeKey);
            edges.push({
                fromEntityKey: from.entityKey,
                toEntityKey: to.entityKey,
                relationshipType,
            });
        }
    }
    return edges;
}
//# sourceMappingURL=EntityRelationship.js.map