<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]" :src="avatarUrl" alt="Avatar" />

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

          <div v-else class="flex h-[21px] items-center justify-center">
            <MessageBubbleLoading :active-color="isAgentMessage ? '#fff' : '#192338'" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
