export function useWorkbenchAiSessionCommands({
  activeConversationTitle,
  attachmentOcrExpandedMessageIds,
  conversationId,
  conversationMessages,
  conversationStarted,
  createInlineMessage,
  currentUser,
  deleteAiWorkbenchConversation,
  emit,
  focusAiModeInput,
  inlineConversationAutoScrollPinned,
  normalizeRuntimeMessage,
  refreshConversationHistory,
  resetInlineConversationState,
  scrollInlineConversationToBottom,
  stewardState,
  thinkingCollapsedMessageIds,
  thinkingExpandedMessageIds,
  toast
}) {
  function startNewInlineConversation() {
    resetInlineConversationState()
    emit('conversation-change', { id: '', title: '' })
    refreshConversationHistory()
    focusAiModeInput()
  }

  function openInlineSearchConversation(activateInlineConversation) {
    conversationMessages.value = [
      createInlineMessage('assistant', '您可以输入关键词搜索历史对话，也可以直接描述要继续处理的费用事项。')
    ]
    stewardState.value = null
    thinkingExpandedMessageIds.value = new Set()
    thinkingCollapsedMessageIds.value = new Set()
    attachmentOcrExpandedMessageIds.value = new Set()
    conversationId.value = 'ai-search'
    activateInlineConversation({ id: 'ai-search', title: '查询对话' })
    focusAiModeInput()
    scrollInlineConversationToBottom()
  }

  function openInlineRecentConversation(item = {}) {
    const title = String(item.title || '最近对话').trim()
    conversationId.value = String(item.id || `recent-${Date.now()}`).trim()
    activeConversationTitle.value = title
    stewardState.value = item.stewardState || null
    thinkingExpandedMessageIds.value = new Set()
    thinkingCollapsedMessageIds.value = new Set()
    attachmentOcrExpandedMessageIds.value = new Set()
    inlineConversationAutoScrollPinned.value = true
    conversationMessages.value = Array.isArray(item.messages) && item.messages.length
      ? item.messages.map((message) => normalizeRuntimeMessage(message))
      : [
          createInlineMessage(
            'assistant',
            '这条历史对话没有保存完整消息。您可以继续输入新的问题，小财管家会接着处理。'
          )
        ]
    conversationStarted.value = true
    emit('conversation-change', { id: conversationId.value, title })
    focusAiModeInput()
    scrollInlineConversationToBottom()
  }

  function requestDeleteCurrentConversation(deleteDialogOpen) {
    if (!conversationMessages.value.length) {
      return
    }
    deleteDialogOpen.value = true
  }

  function cancelDeleteConversation(deleteDialogOpen) {
    deleteDialogOpen.value = false
  }

  function confirmDeleteConversation(deleteDialogOpen) {
    const nextHistory = conversationId.value
      ? deleteAiWorkbenchConversation(currentUser.value || {}, conversationId.value)
      : refreshConversationHistory()
    emit('conversation-history-change', nextHistory)
    resetInlineConversationState()
    deleteDialogOpen.value = false
    emit('conversation-change', { id: '', title: '' })
    toast('已删除当前对话。')
    focusAiModeInput()
  }

  return {
    cancelDeleteConversation,
    confirmDeleteConversation,
    openInlineRecentConversation,
    openInlineSearchConversation,
    requestDeleteCurrentConversation,
    startNewInlineConversation
  }
}
