/**
 * Phase 2: 聊天模式管理Hook
 */

import { useState, useCallback } from 'react'
import {
  ChatPageState,
  ChatBaseMode,
  KnowledgeBaseMode,
  FullTextModeState,
  DeepReadModeState,
} from '../types/chat'

export function useChatMode() {
  const [state, setState] = useState<ChatPageState>({
    baseMode: 'general',
  })

  // 切换基础模式
  const setBaseMode = useCallback((mode: ChatBaseMode) => {
    setState(prev => ({
      ...prev,
      baseMode: mode,
      // 切换到通用模式时清空知识库配置
      ...(mode === 'general' && {
        selectedKbId: undefined,
        kbMode: undefined,
        fullTextState: undefined,
        deepReadState: undefined,
      }),
    }))
  }, [])

  // 选择知识库
  const selectKnowledgeBase = useCallback((kbId: string) => {
    setState(prev => ({
      ...prev,
      baseMode: 'knowledge_base',
      selectedKbId: kbId,
      kbMode: 'full_text', // 默认全文阅读模式
    }))
  }, [])

  // 切换知识库模式
  const setKnowledgeBaseMode = useCallback((mode: KnowledgeBaseMode) => {
    setState(prev => ({
      ...prev,
      kbMode: mode,
    }))
  }, [])

  // 加载全文阅读模式数据
  const loadFullTextMode = useCallback((data: FullTextModeState) => {
    setState(prev => ({
      ...prev,
      fullTextState: data,
    }))
  }, [])

  // 加载逐篇精读模式数据
  const loadDeepReadMode = useCallback((data: DeepReadModeState) => {
    setState(prev => ({
      ...prev,
      deepReadState: data,
    }))
  }, [])

  return {
    state,
    setBaseMode,
    selectKnowledgeBase,
    setKnowledgeBaseMode,
    loadFullTextMode,
    loadDeepReadMode,
  }
}






