<script setup lang="ts">
import { computed, readonly } from 'vue'
import { useI18n } from 'vue-i18n'
import type { MessageItemInterface } from '../types'
import MarkdownRender from '@/components/markdown-render/markdown-render.vue'
import MessageBubbleLoading from './message-bubble-loading.vue'

interface Props {
  messageItem: MessageItemInterface
}

const props = defineProps<Props>()

const { t } = useI18n()

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

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

const assistantAvatarUrl = computed(() => {
  return props.messageItem.avatar || agentAvatarUrl.url
})
</script>

<template>
  <div class="mb-[20px] last:mb-0">
    <div class="flex">
      <img
        class="h-[36px] w-[36px] rounded-full"
        :src="isAssistant ? assistantAvatarUrl : props.messageItem.avatar"
        alt="Avatar"
      />

      <div class="ml-[11px]">
        <div class="mb-[7px] line-clamp-1 break-all text-[12px] text-[#999]">
          {{ props.messageItem.nickName }}
        </div>
        <div
          class="box-content min-h-[21px] min-w-[10px] rounded-[10px] border border-[#9EA3FF] px-[15px] py-[12px] text-justify"
          :class="{
            'bg-[#777EF9]': isAssistant,
            'text-[#fff]': isAssistant,
            '!min-w-[80px]': messageItem.isTextContentLoading,
          }"
        >
          <div v-if="messageItem.isTextContentLoading" class="flex h-[21px] w-[30px] items-center justify-center">
            <MessageBubbleLoading :active-color="isAssistant ? '#fff' : '#192338'" />
          </div>

          <div v-else>
            <MarkdownRender
              :raw-text-content="
                messageItem.content ? messageItem.content : t('common_module.dialogue_module.empty_message_content')
              "
              :color="isAssistant ? '#fff' : '#192338'"
            />

            <div
              v-show="isAssistant && messageItem.isAnswerResponseLoading"
              class="mt-2.5 flex h-[21px] w-[30px] items-center justify-center"
            >
              <MessageBubbleLoading :active-color="isAssistant ? '#fff' : '#192338'" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
