// file: libs/sdk/src/resource/resource.events.ts

import { type ResourceEntry } from '../common';

export type ResourceChangeKind = 'added' | 'updated' | 'removed' | 'reset';

/**
 * The scope of a change event.
 * - `global`: Change affects all sessions
 * - `session`: Change affects only a specific session
 */
export type ResourceChangeScope = 'global' | 'session';

export type ResourceChangeEvent = {
  kind: ResourceChangeKind;
  /** Whether the change affects all sessions or a specific session */
  changeScope: ResourceChangeScope;
  sessionId?: string;
  relatedRequestId?: string;
  version: number;
  snapshot: readonly ResourceEntry[];
  /** URI of the specific resource whose content changed (for kind: 'updated') */
  updatedUri?: string;
};

type Listener = (e: ResourceChangeEvent) => void;

export class ResourceEmitter {
  private listeners = new Set<Listener>();

  on(l: Listener): () => void {
    this.listeners.add(l);
    return () => this.listeners.delete(l);
  }

  emit(e: ResourceChangeEvent) {
    for (const l of [...this.listeners]) l(e);
  }
}
