/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { EventEmitter } from 'node:events';
import type { ConcreteNode } from './graph/types.js';

export interface ProcessorResultEvent {
  processorId: string;
  targets: readonly ConcreteNode[];
  returnedNodes: readonly ConcreteNode[];
}

export interface PristineHistoryUpdatedEvent {
  nodes: readonly ConcreteNode[];
  newNodes: Set<string>;
}

export interface ContextConsolidationEvent {
  nodes: readonly ConcreteNode[];
  targetDeficit: number;
  targetNodeIds: Set<string>;
}

export interface ChunkReceivedEvent {
  nodes: readonly ConcreteNode[];
  targetNodeIds: Set<string>;
}

export interface TokenGroundTruthEvent {
  actualTokens: number;
  promptBaseUnits: number;
}

export interface NormalizeNeededEvent {
  nodes: readonly ConcreteNode[];
  targetDeficit: number;
  targetNodeIds: Set<string>;
}

export class ContextEventBus extends EventEmitter {
  emitTokenGroundTruth(event: TokenGroundTruthEvent) {
    this.emit('TOKEN_GROUND_TRUTH', event);
  }

  onTokenGroundTruth(listener: (event: TokenGroundTruthEvent) => void) {
    this.on('TOKEN_GROUND_TRUTH', listener);
  }

  emitPristineHistoryUpdated(event: PristineHistoryUpdatedEvent) {
    this.emit('PRISTINE_HISTORY_UPDATED', event);
  }

  onPristineHistoryUpdated(
    listener: (event: PristineHistoryUpdatedEvent) => void,
  ) {
    this.on('PRISTINE_HISTORY_UPDATED', listener);
  }

  emitChunkReceived(event: ChunkReceivedEvent) {
    this.emit('IR_CHUNK_RECEIVED', event);
  }

  onChunkReceived(listener: (event: ChunkReceivedEvent) => void) {
    this.on('IR_CHUNK_RECEIVED', listener);
  }

  emitConsolidationNeeded(event: ContextConsolidationEvent) {
    this.emit('BUDGET_RETAINED_CROSSED', event);
  }

  onConsolidationNeeded(listener: (event: ContextConsolidationEvent) => void) {
    this.on('BUDGET_RETAINED_CROSSED', listener);
  }

  emitNormalizeNeeded(event: NormalizeNeededEvent) {
    this.emit('BUDGET_NORMALIZED_CROSSED', event);
  }

  onNormalizeNeeded(listener: (event: NormalizeNeededEvent) => void) {
    this.on('BUDGET_NORMALIZED_CROSSED', listener);
  }

  emitProcessorResult(event: ProcessorResultEvent) {
    this.emit('PROCESSOR_RESULT', event);
  }

  onProcessorResult(listener: (event: ProcessorResultEvent) => void) {
    this.on('PROCESSOR_RESULT', listener);
  }
}
