<script setup lang="ts">
import { computed, readonly } from 'vue'
import type { MessageItemInterface } from '../types'
import { useUserStore } from '@/store/modules/user'
import MessageBubbleLoading from './message-bubble-loading.vue'
import { useI18n } from 'vue-i18n'

interface Props {
  messageItem: MessageItemInterface
}

const props = defineProps<Props>()

const { t } = useI18n()
const userStore = useUserStore()

const agentDefaultAvatarUrl = readonly({ url: 'https://gsst-poe-sit.gz.bcebos.com/icon/agent-avatar.png' })

const isAgentMessage = computed(() => {
  return props.messageItem.role === 'assistant'
})

const avatarUrl = computed(() => {
  return isAgentMessage.value ? props.messageItem.avatar || agentDefaultAvatarUrl.url : userStore.userInfo.avatarUrl
})

const name = computed(() => {
  return isAgentMessage.value ? props.messageItem.name || 'AI助理' : userStore.userInfo.nickName
})
</script>

<template>
  <div class="mb-[20px] last:mb-0">
    <div class="flex">
      <img class="h-[36px] w-[36px] rounded-[6px] object-cover" :src="avatarUrl" alt="Avatar" />

      <div class="ml-[11px] overflow-hidden">
        <div class="mb-[7px] text-[12px] text-[#999]">
          {{ name }}
        </div>
        <div
          class="box-content min-h-[21px] min-w-[10px] max-w-full rounded-[10px] border border-[#9EA3FF] px-[15px] py-[14px] text-justify"
          :class="{
            'bg-[#777EF9]': isAgentMessage,
            'text-[#fff]': isAgentMessage,
            '!min-w-[80px]': messageItem.isAnswerLoading,
          }"
        >
          <div
            v-if="messageItem.isAnswerLoading && !messageItem.content"
            class="flex h-[21px] items-center justify-center"
          >
            <MessageBubbleLoading :active-color="isAgentMessage ? '#fff' : '#192338'" />
          </div>
          <template v-else>
            <MarkdownRender
              :raw-text-content="
                messageItem.content ? messageItem.content : t('common_module.dialogue_module.empty_message_content')
              "
              :color="isAgentMessage ? '#fff' : '#192338'"
            />

            <div v-if="messageItem.isAnswerLoading" class="ml-[15px] pt-[12px]">
              <MessageBubbleLoading active-color="#fff" width="5px" />
            </div>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>
