import { nextTick } from 'vue'

import { normalizeInlineAttachmentOcrDetails } from './workbenchAiMessageModel.js'

export function useWorkbenchAiMessageExpansion({
  attachmentOcrExpandedMessageIds,
  inlineConversationAutoScrollPinned,
  scrollInlineConversationToBottom,
  thinkingCollapsedMessageIds,
  thinkingExpandedMessageIds
}) {
  function resolveInlineThinkingEvents(message) {
    return Array.isArray(message?.stewardPlan?.thinkingEvents) ? message.stewardPlan.thinkingEvents : []
  }

  function hasInlineThinking(message) {
    return resolveInlineThinkingEvents(message).length > 0
  }

  function isInlineThinkingExpanded(message) {
    if (!message?.id) {
      return Boolean(message?.pending)
    }
    if (thinkingCollapsedMessageIds.value.has(message.id)) {
      return false
    }
    return Boolean(message.pending || thinkingExpandedMessageIds.value.has(message.id))
  }

  function toggleInlineThinking(message) {
    if (!message?.id) {
      return
    }
    const nextExpandedIds = new Set(thinkingExpandedMessageIds.value)
    const nextCollapsedIds = new Set(thinkingCollapsedMessageIds.value)
    if (isInlineThinkingExpanded(message)) {
      nextExpandedIds.delete(message.id)
      nextCollapsedIds.add(message.id)
    } else {
      nextCollapsedIds.delete(message.id)
      nextExpandedIds.add(message.id)
    }
    thinkingExpandedMessageIds.value = nextExpandedIds
    thinkingCollapsedMessageIds.value = nextCollapsedIds
  }

  function hasInlineAttachmentOcrDetails(message = {}) {
    const details = normalizeInlineAttachmentOcrDetails(message?.attachmentOcrDetails || null)
    return Boolean(details?.documents?.length || details?.fileNames?.length)
  }

  function resolveInlineAttachmentOcrDocuments(message = {}) {
    return normalizeInlineAttachmentOcrDetails(message?.attachmentOcrDetails || null)?.documents || []
  }

  function resolveInlineAttachmentOcrFileCount(message = {}) {
    const details = normalizeInlineAttachmentOcrDetails(message?.attachmentOcrDetails || null)
    return Math.max(details?.documents?.length || 0, details?.fileNames?.length || 0)
  }

  function isInlineAttachmentOcrExpanded(message = {}) {
    return Boolean(message?.id && attachmentOcrExpandedMessageIds.value.has(message.id))
  }

  function toggleInlineAttachmentOcrDetails(message = {}, forceExpanded = null) {
    if (!message?.id || !hasInlineAttachmentOcrDetails(message)) {
      return
    }
    const nextExpandedIds = new Set(attachmentOcrExpandedMessageIds.value)
    const shouldExpand = forceExpanded === null
      ? !nextExpandedIds.has(message.id)
      : Boolean(forceExpanded)
    if (shouldExpand) {
      nextExpandedIds.add(message.id)
    } else {
      nextExpandedIds.delete(message.id)
    }
    attachmentOcrExpandedMessageIds.value = nextExpandedIds
    nextTick(() => {
      scrollInlineConversationToBottom({ force: inlineConversationAutoScrollPinned.value })
    })
  }

  return {
    hasInlineAttachmentOcrDetails,
    hasInlineThinking,
    isInlineAttachmentOcrExpanded,
    isInlineThinkingExpanded,
    resolveInlineAttachmentOcrDocuments,
    resolveInlineAttachmentOcrFileCount,
    resolveInlineThinkingEvents,
    toggleInlineAttachmentOcrDetails,
    toggleInlineThinking
  }
}
