import { useState, useCallback } from 'react';

export type AppMode = 'chat' | 'sidebar' | 'search';

export interface UseAppModeReturn {
  mode: AppMode;
  setMode: (mode: AppMode) => void;
  toggleSidebar: () => void;
  sidebarOpen: boolean;
}

export function useAppMode(): UseAppModeReturn {
  const [mode, setModeState] = useState<AppMode>('chat');
  const [sidebarOpen, setSidebarOpen] = useState(false);

  const setMode = useCallback((next: AppMode) => {
    setModeState(next);
    if (next === 'sidebar') {
      setSidebarOpen(true);
    }
  }, []);

  const toggleSidebar = useCallback(() => {
    setSidebarOpen((prev) => {
      if (prev) {
        // Closing sidebar — return to chat
        setModeState('chat');
        return false;
      }
      // Opening sidebar — set mode to sidebar
      setModeState('sidebar');
      return true;
    });
  }, []);

  return { mode, setMode, toggleSidebar, sidebarOpen };
}
