// Shared utilities for observability pages

/**
 * Determines the source of an LLM call based on event_type and context
 */
export function getCallSource(
  eventType: string | null,
  context: Record<string, unknown> | null,
): string {
  if (
    context &&
    typeof context === "object" &&
    !Array.isArray(context) &&
    "source" in context
  ) {
    return String(context.source)
  }
  if (eventType) {
    if (eventType === "pr_created" || eventType === "pr_merged" || eventType === "pr_closed") {
      return "GitHub Action"
    }
    if (eventType === "no-pr") {
      return "CLI/VSCode"
    }
    return eventType
  }
  return "Unknown"
}

/**
 * Safely extracts cost and tokens from nullable values
 */
export function safeCostTokens(cost: number | null, tokens: number | null) {
  return {
    cost: cost ?? 0,
    tokens: tokens ?? 0,
  }
}
