import { EventEmitter } from 'node:events';

export class ApprovalManager extends EventEmitter {
  constructor() {
    super();
    this.pending = new Map();
  }

  request(info) {
    return new Promise((resolve) => {
      this.pending.set(info.id, { info, resolve });
      this.emit('approval', info);
    });
  }

  resolve(id, approved) {
    const entry = this.pending.get(id);
    if (!entry) return false;
    this.pending.delete(id);
    entry.resolve({ approved });
    return true;
  }

  resolveAll(approved) {
    for (const [, entry] of this.pending) entry.resolve({ approved });
    this.pending.clear();
  }

  get pendingCount() {
    return this.pending.size;
  }
}
