// Shared terminal constants, types, and prefs persistence. Kept in a non-component
// module so the context file can export only its provider/hook (React Fast Refresh
// requires component files to export components exclusively).

const GOLD = '#C8A434'

export interface TermTheme {
  name: string
  background: string
  foreground: string
  cursor: string
}

export const TERM_THEMES: TermTheme[] = [
  { name: 'ArchNest Dark', background: '#15161A', foreground: '#E8E6E0', cursor: GOLD },
  { name: 'Matrix', background: '#000000', foreground: '#33FF66', cursor: '#33FF66' },
  { name: 'Solarized', background: '#002B36', foreground: '#93A1A1', cursor: '#CB4B16' },
  { name: 'Midnight Blue', background: '#0B1021', foreground: '#C7D3F2', cursor: '#5FA8FF' },
]

export interface TerminalPrefs {
  themeName: string
  fontSize: number
  fontFamily: string
}

export interface PaneState {
  id: number
  hostId: number | null
}

export interface TabState {
  id: number
  name: string
  panes: PaneState[]
}

const PREFS_KEY = 'archnest-terminal-prefs'

export function defaultPrefs(): TerminalPrefs {
  return { themeName: TERM_THEMES[0].name, fontSize: 13, fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace' }
}

export function loadPrefs(): TerminalPrefs {
  try {
    const raw = localStorage.getItem(PREFS_KEY)
    if (raw) return { ...defaultPrefs(), ...JSON.parse(raw) }
  } catch {
    /* ignore malformed local storage */
  }
  return defaultPrefs()
}

export function savePrefs(prefs: TerminalPrefs) {
  localStorage.setItem(PREFS_KEY, JSON.stringify(prefs))
}
