/**
 * UI store — global UI overlay/drawer/panel state.
 *
 * Single source of truth for ephemeral UI state that multiple components
 * need to read or write (e.g., palette commands opening settings from anywhere).
 *
 * Components are pure view layers: they read from this store and call
 * its methods to mutate state. No component should own $state that
 * other components need.
 */

// ── Settings drawer ──────────────────────────────────────────────────────

type SettingsCategory =
  | 'appearance' | 'agents' | 'security' | 'projects'
  | 'orchestration' | 'machines' | 'advanced' | 'marketplace'
  | 'keyboard' | 'diagnostics';

let _settingsOpen = $state(false);
let _settingsCategory = $state<SettingsCategory>('appearance');

export function getSettingsOpen(): boolean { return _settingsOpen; }
export function setSettingsOpen(v: boolean): void { _settingsOpen = v; }
export function toggleSettings(): void { _settingsOpen = !_settingsOpen; }

export function getSettingsCategory(): SettingsCategory { return _settingsCategory; }
export function setSettingsCategory(c: SettingsCategory): void { _settingsCategory = c; }

/** Open settings drawer directly to a specific category. */
export function openSettingsCategory(category: SettingsCategory): void {
  _settingsCategory = category;
  _settingsOpen = true;
}

// ── Command palette ──────────────────────────────────────────────────────

let _paletteOpen = $state(false);

export function getPaletteOpen(): boolean { return _paletteOpen; }
export function setPaletteOpen(v: boolean): void { _paletteOpen = v; }
export function togglePalette(): void { _paletteOpen = !_paletteOpen; }

// ── Search overlay ───────────────────────────────────────────────────────

let _searchOpen = $state(false);

export function getSearchOpen(): boolean { return _searchOpen; }
export function setSearchOpen(v: boolean): void { _searchOpen = v; }
export function toggleSearch(): void { _searchOpen = !_searchOpen; }

// ── Notification drawer ──────────────────────────────────────────────────

let _notifDrawerOpen = $state(false);

export function getNotifDrawerOpen(): boolean { return _notifDrawerOpen; }
export function setNotifDrawerOpen(v: boolean): void { _notifDrawerOpen = v; }
export function toggleNotifDrawer(): void { _notifDrawerOpen = !_notifDrawerOpen; }

// ── Project wizard ───────────────────────────────────────────────────────

let _showWizard = $state(false);

export function getShowWizard(): boolean { return _showWizard; }
export function setShowWizard(v: boolean): void { _showWizard = v; }
export function toggleWizard(): void { _showWizard = !_showWizard; }

// ── Delete project confirmation ──────────────────────────────────────────

let _projectToDelete = $state<string | null>(null);

export function getProjectToDelete(): string | null { return _projectToDelete; }
export function setProjectToDelete(id: string | null): void { _projectToDelete = id; }

// ── Add group form ───────────────────────────────────────────────────────

let _showAddGroup = $state(false);
let _newGroupName = $state('');

export function getShowAddGroup(): boolean { return _showAddGroup; }
export function setShowAddGroup(v: boolean): void { _showAddGroup = v; }
export function toggleAddGroup(): void { _showAddGroup = !_showAddGroup; }

export function getNewGroupName(): string { return _newGroupName; }
export function setNewGroupName(v: string): void { _newGroupName = v; }

/** Reset add-group form after submission. */
export function resetAddGroupForm(): void {
  _showAddGroup = false;
  _newGroupName = '';
}

// ── Type re-export ───────────────────────────────────────────────────────

export type { SettingsCategory };
