type Listener = () => void;

export class UIState {
  private listeners: Listener[] = [];

  activeView: string | number = 'dashboard';
  sidebarOpen = true;

  subscribe(fn: Listener): () => void {
    this.listeners.push(fn);
    return () => {
      this.listeners = this.listeners.filter(l => l !== fn);
    };
  }

  notify(): void {
    for (const fn of this.listeners) fn();
  }

  setActiveView(view: string | number): void {
    this.activeView = view;
    this.notify();
  }

  toggleSidebar(): void {
    this.sidebarOpen = !this.sidebarOpen;
    this.notify();
  }

  closeSidebar(): void {
    if (this.sidebarOpen) {
      this.sidebarOpen = false;
      this.notify();
    }
  }
}
