<script setup lang="ts">
import MessageItem from './message-item.vue'
import type { MessageItemInterface } from '../types'
import { useTemplateRef } from 'vue'
import { ScrollbarInst } from 'naive-ui'

interface Props {
  messageList: Map<string, MessageItemInterface>
}

defineProps<Props>()

defineExpose({
  scrollToBottom,
})

const scrollbarRef = useTemplateRef<ScrollbarInst | null>('scrollbarRef')

function scrollToBottom() {
  if (scrollbarRef.value) {
    scrollbarRef.value.scrollTo({ top: 999999999, behavior: 'smooth' })
  }
}
</script>

<template>
  <div class="flex-1 overflow-hidden overflow-y-auto py-[20px]">
    <n-scrollbar ref="scrollbarRef">
      <div class="pr-[10px]">
        <MessageItem v-for="[key, messageItem] in messageList" :key="key" :message-item="messageItem" />
      </div>
    </n-scrollbar>
  </div>
</template>
