<!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->

<script setup lang="ts" generic="T">
import { computed } from 'vue'

import type { ObjectLike } from '#shared/types/utils.ts'

import CommonShowMoreButton from '#desktop/components/CommonShowMoreButton/CommonShowMoreButton.vue'
import entityModules from '#desktop/components/CommonSimpleEntityList/plugins/index.ts'
import {
  type Entity,
  EntityType,
} from '#desktop/components/CommonSimpleEntityList/types.ts'

interface Props {
  entity: Entity<ObjectLike>
  type: EntityType
  label?: string
}

const props = defineProps<Props>()

defineEmits<{
  'load-more': []
}>()

const entitySetup = computed(() => {
  const { component, ...context } = entityModules[props.type]
  return {
    component,
    context,
    array: props.entity.array,
  }
})
</script>

<template>
  <div class="flex flex-col gap-1.5">
    <CommonLabel
      v-if="label"
      class="-:inline-flex items-center text-xs leading-snug text-stone-200 dark:text-neutral-500"
    >
      {{ label }}
    </CommonLabel>

    <ul v-if="entity.array?.length" class="flex flex-col gap-1.5">
      <li v-for="item in entitySetup.array" :key="`entity-${item.id}`">
        <component
          :is="entitySetup.component"
          :entity="item"
          :context="entitySetup.context"
        />
      </li>
    </ul>

    <CommonLabel v-if="!entity.array?.length" class="block"
      >{{ entitySetup.context.emptyMessage }}
    </CommonLabel>

    <CommonShowMoreButton
      v-if="entity"
      class="self-end"
      :entities="entity.array"
      :total-count="entity.totalCount"
      @click="$emit('load-more')"
    />
  </div>
</template>
