/**
 * edgeTypeUtils - Shared edge type determination logic
 * 
 * Provides a single source of truth for computing edge types based on
 * edge data (condition, annotation). Used by useConfiguration, useDragAndDrop,
 * and any other module that needs to determine edge types.
 */

interface EdgeData {
  condition?: string | null;
  conditionLabel?: string | null;
  conditionConfiguration?: Record<string, any> | null;
  annotation?: string | null;
  [key: string]: any;
}

/**
 * Determine the edge type based on its data.
 * 
 * Priority:
 * - If edge has a condition (any condition-related field), returns 'condition'
 * - If edge has an annotation, returns 'annotation'
 * - Otherwise returns 'default'
 * 
 * Note: Condition edges can also display annotations, so condition takes priority.
 */
export function getEdgeType(data: EdgeData | undefined | null): string {
  if (!data) return 'default';

  const hasCondition = data.condition || data.conditionLabel || data.conditionConfiguration;
  const hasAnnotation = data.annotation;

  if (hasCondition) {
    return 'condition';
  }
  if (hasAnnotation) {
    return 'annotation';
  }
  return 'default';
}

/**
 * Determine edge type when setting a new condition on an edge.
 * Takes the new condition value and existing edge data into account.
 */
export function getEdgeTypeWithCondition(
  newCondition: string | null | undefined,
  existingData: EdgeData | undefined | null
): string {
  const hasCondition = newCondition || existingData?.condition || existingData?.conditionLabel || existingData?.conditionConfiguration;
  const hasAnnotation = existingData?.annotation;

  if (hasCondition) {
    return 'condition';
  }
  if (hasAnnotation) {
    return 'annotation';
  }
  return 'default';
}
