import * as path from 'path';
import type { PerfDiagEntry } from './diagnostics.models';

export function pushRingBuffer<T>(items: readonly T[], item: T, capacity: number): T[] {
  const next = [...items, item];

  if (next.length <= capacity) {
    return next;
  }

  return next.slice(next.length - capacity);
}

export function formatPerfDiagLine(entry: PerfDiagEntry): string {
  return JSON.stringify(entry);
}

export function resolveDiagnosticsFilePath(userDataPath: string, sessionId: string): string {
  return path.join(userDataPath, 'diagnostics', `perf-${sessionId}.jsonl`);
}

export function resolveDiagnosticsDirectory(userDataPath: string): string {
  return path.join(userDataPath, 'diagnostics');
}
