<script setup>
import { useNamespace, useLocale } from "@yh-ui/hooks";
import { computed } from "vue";
import { aiActionGroupProps, aiActionGroupEmits } from "./ai-action-group";
import { YhIcon } from "../../icon";
import { YhTooltip } from "../../tooltip";
import { useComponentTheme } from "@yh-ui/theme";
defineOptions({
  name: "YhAiActionGroup"
});
const props = defineProps(aiActionGroupProps);
const emit = defineEmits(aiActionGroupEmits);
const ns = useNamespace("ai-action-group");
const { t } = useLocale();
const { themeStyle } = useComponentTheme("ai-action-group", props.themeOverrides);
const defaultIcons = {
  copy: "copy",
  refresh: "refresh",
  regenerate: "refresh",
  share: "share",
  "thumb-up": "thumb-up",
  "thumb-down": "thumb-down",
  edit: "edit",
  delete: "delete"
};
const defaultLabels = computed(() => ({
  copy: t("ai.action.copy"),
  refresh: t("ai.action.regenerate"),
  regenerate: t("ai.action.regenerate"),
  share: t("ai.action.share"),
  "thumb-up": t("ai.action.like"),
  "thumb-down": t("ai.action.dislike"),
  edit: t("ai.action.edit"),
  delete: t("ai.action.delete")
}));
const actionItems = computed(() => {
  return props.items.map((item) => {
    if (typeof item === "string") {
      return {
        key: item,
        icon: defaultIcons[item] || "more",
        label: "",
        // 通常泡泡底座不需要文字标签
        tooltip: defaultLabels.value[item] || item,
        _original: item
      };
    }
    return {
      ...item,
      icon: item.icon || defaultIcons[item.key] || "more",
      _original: item
    };
  });
});
const handleClick = (item) => {
  if (typeof item === "string") {
    emit("click", item, item);
    return;
  }
  if (item.disabled) return;
  emit("click", item.key, item._original);
};
</script>

<template>
  <div :class="[ns.b(), ns.m(direction), ns.m(size), ns.m(props.variant)]" :style="themeStyle">
    <template v-for="(item, index) in actionItems" :key="index">
      <div :class="[ns.e('item'), ns.is('disabled', item.disabled)]" @click="handleClick(item)">
        <!-- 使用 Tooltip 装饰 -->
        <YhTooltip v-if="item.tooltip" :content="item.tooltip" placement="top">
          <div :class="ns.e('item-inner')">
            <YhIcon :class="ns.e('item-icon')" :name="item.icon" />
            <span v-if="item.label" :class="ns.e('item-label')">{{ item.label }}</span>
          </div>
        </YhTooltip>

        <template v-else>
          <YhIcon :class="ns.e('item-icon')" :name="item.icon" />
          <span v-if="item.label" :class="ns.e('item-label')">{{ item.label }}</span>
        </template>
      </div>
    </template>

    <!-- 为插槽预留尾部 -->
    <slot></slot>
  </div>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
.yh-ai-action-group {
  --yh-ai-action-gap: 8px;
  --yh-ai-action-icon-size: 16px;
  --yh-ai-action-btn-padding: 4px;
  --yh-ai-action-btn-radius: 6px;
  --yh-ai-action-hover-bg: var(--yh-fill-color-light);
  display: inline-flex;
  gap: var(--yh-ai-action-gap);
  flex-wrap: wrap;
  align-items: center;
}
.yh-ai-action-group--vertical {
  flex-direction: column;
  align-items: flex-start;
}

.yh-ai-action-group__item {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  transition: all 0.2s;
  border-radius: var(--yh-ai-action-btn-radius);
  color: var(--yh-text-color-secondary);
  padding: var(--yh-ai-action-btn-padding);
  background-color: transparent;
  border: none;
}
.yh-ai-action-group__item:hover {
  color: var(--yh-color-primary);
  background-color: var(--yh-ai-action-hover-bg);
}
.yh-ai-action-group__item:active {
  transform: scale(0.92);
}
.yh-ai-action-group__item.is-disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
.yh-ai-action-group__item.is-disabled:hover {
  color: var(--yh-text-color-secondary);
  background-color: transparent;
}

.yh-ai-action-group__item-icon {
  font-size: var(--yh-ai-action-icon-size);
}

.yh-ai-action-group__item-label {
  font-size: 12px;
  font-weight: 400;
}

.yh-ai-action-group {
  /* Variations by size */
}
.yh-ai-action-group--small {
  --yh-ai-action-icon-size: 14px;
  --yh-ai-action-gap: 4px;
}

.yh-ai-action-group--large {
  --yh-ai-action-icon-size: 20px;
  --yh-ai-action-gap: 12px;
}

.yh-ai-action-group {
  /* Variations by variant */
}
.yh-ai-action-group--outlined {
  border: 1px solid var(--yh-border-color, #e4e7ed);
  border-radius: calc(var(--yh-ai-action-btn-radius) + 4px);
  padding: 2px 6px;
  background-color: var(--yh-bg-color-overlay, #ffffff);
}

.yh-ai-action-group--ghost {
  border: 1px solid var(--yh-border-color, #e4e7ed);
  border-radius: calc(var(--yh-ai-action-btn-radius) + 4px);
  padding: 2px 6px;
  background-color: transparent;
}

.yh-ai-action-group--text {
  border: none;
  background-color: transparent;
  padding: 0;
}
</style>
