import type { TimelineItem } from '@/types/timeline'
import type { TraceNode } from '@/types/trace'
import type { CytoscapeTooltipSource, NodeTooltipData } from '@/types/traceTooltip'

type TooltipSource = TraceNode | TimelineItem | CytoscapeTooltipSource

function isTraceNode(source: TooltipSource): source is TraceNode {
  return 'name' in source && 'children' in source
}

function isTimelineItem(source: TooltipSource): source is TimelineItem {
  return 'label' in source && 'group' in source && 'icon' in source
}

export function toNodeTooltipData(source: TooltipSource): NodeTooltipData {
  if (isTraceNode(source)) {
    return {
      name: source.name,
      start: source.begin,
      end: source.end,
      timestamp: source.timestamp,
    }
  }

  if (isTimelineItem(source)) {
    return {
      name: source.label,
      start: source.start,
      end: source.end,
      timestamp: source.timestamp,
    }
  }

  return {
    name: source.label ?? '',
    start: source.start,
    end: source.end,
    timestamp: source.timestamp,
  }
}
