<script setup>
import { computed } from "vue";
import { useI18n } from "vue-i18n";

const { t } = useI18n();

const props = defineProps({
  tool: {
    type: Object,
    required: true,
  },
  activeTool: {
    type: String,
    default: "",
  },
  canUndo: {
    type: Boolean,
    default: true,
  },
  canRedo: {
    type: Boolean,
    default: true,
  },
});

const emit = defineEmits(["click"]);

// 计算按钮是否激活
const isActive = computed(() => {
  return (
    props.tool.id === props.activeTool ||
    props.tool.id === props.activeTool.toLowerCase() ||
    props.tool?.activeList?.includes(props.activeTool)
  );
});

// 计算按钮是否禁用
const isDisabled = computed(() => {
  return (
    (props.tool.id === "undo" && !props.canUndo) ||
    (props.tool.id === "redo" && !props.canRedo)
  );
});

// 处理按钮点击
const handleClick = () => {
  if (isDisabled.value) return;
  emit("click", props.tool);
};
</script>

<template>
  <div
    :class="[
      'tool-btn',
      tool.class,
      {
        active: isActive,
        disabled: isDisabled,
      },
    ]"
    :style="tool.style"
    @click="handleClick"
  >
    <SvgIcon :name="tool.icon.name" :size="tool.icon.size"></SvgIcon>
    <div class="tool-tooltip">{{ t(tool.title) }}</div>
  </div>
</template>

<style scoped>
.tool-btn {
  position: relative;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  color: #333;
  transition: all 0.2s ease;
}

.tool-btn:hover {
  background-color: #f0f0f0;
}

.tool-btn:hover .tool-tooltip {
  display: block;
}

.tool-btn.active {
  background-color: #e6f7ff;
  color: #1890ff;
}

.tool-btn.disabled {
  cursor: not-allowed;
  color: #e0e0e0;
}

.tool-tooltip {
  display: none;
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  margin-left: 8px;
  white-space: nowrap;
  font-size: 12px;
  z-index: 10;
}

.tool-tooltip:before {
  content: "";
  position: absolute;
  top: 50%;
  right: 100%;
  margin-top: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: transparent rgba(0, 0, 0, 0.7) transparent transparent;
}
</style>
