<script setup lang="ts">
import MessageItem from './message-item.vue'
import ContinueQuestion from './continue-question.vue'
import { useScroll } from '@/composables/useScroll'
import { PersonalAppConfigState } from '@/store/types/personal-app-config'
import { computed } from 'vue'

interface Props {
  messageList: Map<string, ConversationMessageItem>
  agentApplicationConfig: PersonalAppConfigState
  continuousQuestionStatus: 'default' | 'close'
  continuousQuestionList: string[]
  isAnswerResponseLoading: boolean
  createContinueQuestionsException: boolean
}

const props = defineProps<Props>()

defineEmits<{
  audioPlay: [messageItem: ConversationMessageItem]
  audioPause: []
}>()

const { scrollRef, scrollToBottom } = useScroll()

const isShowContinueQuestion = computed(() => {
  return (
    props.continuousQuestionStatus === 'default' &&
    props.messageList.size > 1 &&
    !props.isAnswerResponseLoading &&
    !props.createContinueQuestionsException
  )
})

defineExpose({
  scrollToBottom,
})
</script>

<template>
  <main ref="scrollRef" class="h-full overflow-y-auto px-5">
    <div>
      <MessageItem
        v-for="[key, messageItem] in messageList"
        :key="key"
        :role="messageItem.role"
        :message-item="messageItem"
        :agent-application-config="agentApplicationConfig"
        @audio-play="() => $emit('audioPlay', messageItem)"
        @audio-pause="() => $emit('audioPause')"
      />
    </div>

    <div v-show="isShowContinueQuestion">
      <ContinueQuestion :continuous-question-list="continuousQuestionList" />
    </div>
  </main>
</template>
