<script setup lang="ts">
import type {AiChatMessage, AiToolApprovalPayload} from './types';

import {ref, watch} from 'vue';

import {ChatDotRound} from '@element-plus/icons-vue';
import {ElEmpty} from 'element-plus';

import AiMessage from './AiMessage.vue';
import {useAiChatScroll} from './composables/useAiChatScroll';

const props = defineProps<{
  approvalLoading?: boolean;
  emptyText?: string;
  messages: AiChatMessage[];
}>();

const emit = defineEmits<{
  approve: [payload: AiToolApprovalPayload];
  reject: [payload: AiToolApprovalPayload];
}>();

const containerRef = ref<HTMLElement>();
const { scrollToBottom } = useAiChatScroll(containerRef);

watch(
  () => props.messages,
  () => scrollToBottom(),
  { deep: true, immediate: true },
);
</script>

<template>
  <div ref="containerRef" class="ai-conversation">
    <ElEmpty
      v-if="messages.length === 0"
      :image-size="88"
      :description="emptyText || '开始试用当前草稿'"
    >
      <template #image>
        <div class="ai-conversation__empty-icon">
          <ChatDotRound />
        </div>
      </template>
    </ElEmpty>
    <AiMessage
      v-for="message in messages"
      v-else
      :key="message.id"
      :message="message"
      :approval-loading="approvalLoading"
      @approve="emit('approve', $event)"
      @reject="emit('reject', $event)"
    />
  </div>
</template>

<style scoped>
.ai-conversation {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 16px;
  overflow: auto;
}

.ai-conversation__empty-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 72px;
  height: 72px;
  margin: 0 auto;
  color: var(--el-color-primary);
  background: var(--el-color-primary-light-9);
  border-radius: 8px;
}

.ai-conversation__empty-icon svg {
  width: 32px;
  height: 32px;
}
</style>
