<script setup lang="ts">
import type {ChatInputTriggerItem} from './input-triggers/types';

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

defineProps<{
  activeIndex?: number;
  groupLabel?: string;
  items?: ChatInputTriggerItem[];
  keyword?: string;
}>();

const emit = defineEmits<{
  select: [item: ChatInputTriggerItem, index: number];
  setActive: [index: number];
}>();
</script>

<template>
  <div class="chat-input-trigger-panel">
    <div v-if="groupLabel" class="chat-input-trigger-panel__head">
      {{ groupLabel }}
    </div>
    <div
      v-if="items && items.length > 0"
      class="chat-input-trigger-panel__list"
    >
      <button
        v-for="(item, index) in items"
        :key="item.id"
        type="button"
        class="chat-input-trigger-panel__item"
        :class="{ 'is-active': index === activeIndex }"
        :disabled="item.disabled"
        @mouseenter="emit('setActive', index)"
        @mousedown.prevent="emit('select', item, index)"
      >
        <component
          :is="item.icon || Collection"
          class="chat-input-trigger-panel__icon"
        />
        <span class="chat-input-trigger-panel__content">
          <span class="chat-input-trigger-panel__label">{{ item.label }}</span>
        </span>
      </button>
    </div>
    <div v-else class="chat-input-trigger-panel__empty">
      {{ keyword ? '没有匹配项' : '暂无可选项' }}
    </div>
  </div>
</template>

<style scoped>
.chat-input-trigger-panel {
  width: 100%;
  padding: 8px;
  background: var(--el-fill-color-extra-light);
  border: 1px solid var(--el-border-color-lighter);
  border-radius: 18px;
  box-shadow: var(--el-box-shadow-light);
  box-sizing: border-box;
}

.chat-input-trigger-panel__head {
  min-height: 32px;
  padding: 0 10px;
  font-size: 13px;
  font-weight: 500;
  line-height: 32px;
  color: var(--el-text-color-secondary);
}

.chat-input-trigger-panel__list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 240px;
  overflow: auto;
}

.chat-input-trigger-panel__item {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 40px;
  gap: 10px;
  padding: 0 10px;
  color: var(--el-text-color-primary);
  text-align: left;
  cursor: pointer;
  background: transparent;
  border: 0;
  border-radius: 12px;
}

.chat-input-trigger-panel__item:hover:not(:disabled),
.chat-input-trigger-panel__item.is-active {
  background: var(--el-fill-color-light);
}

.chat-input-trigger-panel__item:focus-visible {
  outline: 2px solid var(--el-color-primary-light-5);
  outline-offset: 2px;
}

.chat-input-trigger-panel__item:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.chat-input-trigger-panel__icon {
  width: 18px;
  height: 18px;
  flex: none;
  color: var(--el-color-primary);
}

.chat-input-trigger-panel__content {
  display: inline-flex;
  min-width: 0;
  align-items: baseline;
}

.chat-input-trigger-panel__label {
  min-width: 0;
  overflow: hidden;
  font-size: 14px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-input-trigger-panel__empty {
  padding: 18px 10px;
  font-size: 13px;
  color: var(--el-text-color-secondary);
}
</style>
