import type { App } from '../shared/types'

// UI state (survives re-renders)
export let selectedApp: string | null = localStorage.getItem('selectedApp')
export let sidebarCollapsed: boolean = localStorage.getItem('sidebarCollapsed') === 'true'
export let sidebarSection: 'apps' | 'tools' = (localStorage.getItem('sidebarSection') as 'apps' | 'tools') || 'apps'

// Server state (from SSE)
export let apps: App[] = []

// Tab state
export let selectedTab: string = localStorage.getItem('selectedTab') || 'overview'

// State setters
export function setSelectedApp(name: string | null) {
  selectedApp = name
  if (name) {
    localStorage.setItem('selectedApp', name)
  } else {
    localStorage.removeItem('selectedApp')
  }
}

export function setSidebarCollapsed(collapsed: boolean) {
  sidebarCollapsed = collapsed
  localStorage.setItem('sidebarCollapsed', String(collapsed))
}

export function setSidebarSection(section: 'apps' | 'tools') {
  sidebarSection = section
  localStorage.setItem('sidebarSection', section)
}

export function setApps(newApps: App[]) {
  apps = newApps
}

export function setSelectedTab(tab: string) {
  selectedTab = tab
  localStorage.setItem('selectedTab', tab)
}
