// Status bar : compteur des sessions Claude en attente d'input (`activity === 'waiting'`).
// Clic → quick-pick des sessions en attente (commande arboretum.showWaiting). Masqué si déconnecté
// ou s'il n'y a rien en attente.
import * as vscode from 'vscode';
import type { Store } from './state/store.js';

export class StatusBar implements vscode.Disposable {
  private readonly item: vscode.StatusBarItem;
  private connected = false;
  private readonly unsubscribe: () => void;

  constructor(private readonly store: Store) {
    this.item = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
    this.item.command = 'arboretum.showWaiting';
    this.unsubscribe = store.onDidChange(() => this.update());
    this.update();
  }

  setConnected(connected: boolean): void {
    this.connected = connected;
    this.update();
  }

  private update(): void {
    const count = this.connected ? this.store.waitingSessions().length : 0;
    if (count === 0) {
      this.item.hide();
      return;
    }
    this.item.text = `$(bell-dot) ${count}`;
    this.item.tooltip = `Arboretum: ${count} session(s) waiting for input`;
    this.item.color = new vscode.ThemeColor('statusBarItem.warningForeground');
    this.item.show();
  }

  dispose(): void {
    this.unsubscribe();
    this.item.dispose();
  }
}
