import { SegmentEvent, SegmentEventType } from './event'
import { User } from './user'


export type EventStage = 'triggered' | 'enriched' | 'delivered'

type PhaseDefinition<T extends EventStage, PhaseProperties extends Record<string, unknown>> = {
  /** An ID that uniquely identifies a trace. Must remain the same through all phases */
  id: string
  stage: T
  timestamp: string
  /** The latest event object as of the current phase */
  event: SegmentEvent
} & PhaseProperties

export type TriggeredPhase = PhaseDefinition<'triggered', Record<string, unknown>>
export type EnrichedPhase = PhaseDefinition<'enriched', Record<string, unknown>>
export type DeliveredPhase = PhaseDefinition<
  'delivered',
  {
    destinations: string[]
  }
>

export type EventPhase = TriggeredPhase | EnrichedPhase | DeliveredPhase

export type EventPhaseLog = {
  [E in EventPhase as E['stage']]?: E
}

export type EventRecord = {
  id: string
  event: SegmentEvent
  phases: EventPhaseLog
}

export type InitConfig = {
  user?: User
}

export type Inspector = {
  start: (config: InitConfig) => void
  trace: (eventPhase: EventPhase) => void
}

export type EventsFilter = {
  query: string
  eventTypes: SegmentEventType[]
}

export type UIState = {
  eventsFilter: EventsFilter
  selectedEventRecordId: EventRecord['id']
}
