<script setup>
import { computed, ref, nextTick, useSlots } from "vue";
import { useNamespace } from "@yh-ui/hooks";
import { useConfig } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
defineOptions({
  name: "YhTag"
});
const props = defineProps({
  type: { type: null, required: false, default: "primary" },
  size: { type: null, required: false, default: "default" },
  effect: { type: null, required: false, default: "light" },
  closable: { type: Boolean, required: false, default: false },
  disableTransitions: { type: Boolean, required: false, default: false },
  hit: { type: Boolean, required: false, default: false },
  color: { type: String, required: false },
  round: { type: Boolean, required: false, default: false },
  checkable: { type: Boolean, required: false, default: false },
  checked: { type: Boolean, required: false, default: false },
  editable: { type: Boolean, required: false, default: false },
  icon: { type: null, required: false },
  suffixIcon: { type: null, required: false },
  themeOverrides: { type: null, required: false }
});
const emit = defineEmits(["close", "click", "update:checked", "change", "edit"]);
const slots = useSlots();
const ns = useNamespace("tag");
const { globalSize } = useConfig();
const { themeStyle } = useComponentTheme(
  "tag",
  computed(() => props.themeOverrides)
);
const isEditing = ref(false);
const editValue = ref("");
const inputRef = ref();
const hasIcon = computed(() => !!props.icon || !!slots.icon);
const hasSuffixIcon = computed(() => !!props.suffixIcon || !!slots.suffixIcon);
const tagClasses = computed(() => [
  ns.b(),
  ns.m(props.type),
  ns.m(props.size || globalSize.value || "default"),
  ns.m(props.effect),
  ns.is("closable", props.closable),
  ns.is("hit", props.hit),
  ns.is("round", props.round),
  ns.is("checkable", props.checkable),
  ns.is("checked", props.checkable && props.checked),
  ns.is("editable", props.editable),
  ns.is("has-icon", hasIcon.value),
  ns.is("has-suffix-icon", hasSuffixIcon.value)
]);
const tagStyle = computed(() => {
  const base = themeStyle.value;
  if (!props.color) return base;
  const style = {};
  if (props.effect === "dark") {
    style["--yh-tag-bg-color"] = props.color;
    style["--yh-tag-border-color"] = props.color;
    style["--yh-tag-text-color"] = "#fff";
  } else if (props.effect === "light") {
    style["--yh-tag-bg-color"] = `${props.color}20`;
    style["--yh-tag-border-color"] = `${props.color}40`;
    style["--yh-tag-text-color"] = props.color;
  } else {
    style["--yh-tag-bg-color"] = "transparent";
    style["--yh-tag-border-color"] = props.color;
    style["--yh-tag-text-color"] = props.color;
  }
  return { ...base, ...style };
});
const handleClick = (event) => {
  if (props.checkable) {
    const newChecked = !props.checked;
    emit("update:checked", newChecked);
    emit("change", newChecked);
  }
  emit("click", event);
};
const handleClose = (event) => {
  event.stopPropagation();
  emit("close", event);
};
const startEdit = () => {
  if (!props.editable) return;
  isEditing.value = true;
  nextTick(() => {
    inputRef.value?.focus();
    inputRef.value?.select();
  });
};
const handleEditInput = (event) => {
  editValue.value = event.target.value;
};
const handleEditConfirm = () => {
  isEditing.value = false;
  if (editValue.value.trim()) {
    emit("edit", editValue.value.trim());
  }
  editValue.value = "";
};
const handleEditKeydown = (event) => {
  if (event.key === "Enter") {
    handleEditConfirm();
  } else if (event.key === "Escape") {
    isEditing.value = false;
    editValue.value = "";
  }
};
</script>

<template>
  <!-- 编辑模式 -->
  <input
    v-if="editable && isEditing"
    ref="inputRef"
    :class="[ns.e('input'), ns.m(size)]"
    :value="editValue"
    @input="handleEditInput"
    @blur="handleEditConfirm"
    @keydown="handleEditKeydown"
  />

  <!-- 普通模式 -->
  <span v-else :class="tagClasses" :style="tagStyle" @click="handleClick" @dblclick="startEdit">
    <!-- 左侧图标 -->
    <span v-if="hasIcon" :class="ns.e('icon')">
      <slot name="icon">
        <component v-if="icon && typeof icon !== 'string'" :is="icon" />
      </slot>
    </span>

    <!-- 内容 -->
    <span :class="ns.e('content')">
      <slot />
    </span>

    <!-- 右侧图标 -->
    <span v-if="hasSuffixIcon" :class="ns.e('suffix-icon')">
      <slot name="suffixIcon">
        <component v-if="suffixIcon && typeof suffixIcon !== 'string'" :is="suffixIcon" />
      </slot>
    </span>

    <!-- 关闭按钮 -->
    <span v-if="closable" :class="ns.e('close')" @click="handleClose">
      <slot name="closeIcon">
        <svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="1em" height="1em">
          <path
            fill="currentColor"
            d="M576 512l277.333333 277.333333-64 64-277.333333-277.333333L234.666667 853.333333 170.666667 789.333333l277.333333-277.333333L170.666667 234.666667 234.666667 170.666667l277.333333 277.333333L789.333333 170.666667 853.333333 234.666667 576 512z"
          />
        </svg>
      </slot>
    </span>
  </span>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
.yh-tag {
  --yh-tag-bg-color: var(--yh-color-primary-light-9);
  --yh-tag-border-color: var(--yh-color-primary-light-8);
  --yh-tag-text-color: var(--yh-color-primary);
  --yh-tag-hover-color: var(--yh-color-primary);
  display: inline-flex;
  justify-content: center;
  align-items: center;
  height: 24px;
  padding: 0 9px;
  font-size: var(--yh-font-size-xs);
  line-height: 1;
  color: var(--yh-tag-text-color);
  background-color: var(--yh-tag-bg-color);
  border: 1px solid var(--yh-tag-border-color);
  border-radius: var(--yh-radius-sm);
  box-sizing: border-box;
  white-space: nowrap;
  transition: all 0.2s ease;
  /* 尺寸修饰符 */
}
.yh-tag--large {
  height: 32px;
  padding: 0 12px;
  font-size: var(--yh-font-size-base);
}

.yh-tag--small {
  height: 20px;
  padding: 0 6px;
  font-size: 12px;
}

.yh-tag {
  /* 类型修饰符 */
}
.yh-tag--success {
  --yh-tag-bg-color: var(--yh-color-success-light-9);
  --yh-tag-border-color: var(--yh-color-success-light-8);
  --yh-tag-text-color: var(--yh-color-success);
}

.yh-tag--warning {
  --yh-tag-bg-color: var(--yh-color-warning-light-9);
  --yh-tag-border-color: var(--yh-color-warning-light-8);
  --yh-tag-text-color: var(--yh-color-warning);
}

.yh-tag--danger {
  --yh-tag-bg-color: var(--yh-color-danger-light-9);
  --yh-tag-border-color: var(--yh-color-danger-light-8);
  --yh-tag-text-color: var(--yh-color-danger);
}

.yh-tag--info {
  --yh-tag-bg-color: var(--yh-color-info-light-9);
  --yh-tag-border-color: var(--yh-color-info-light-8);
  --yh-tag-text-color: var(--yh-color-info);
}

.yh-tag {
  /* 主题效果 */
}
.yh-tag--dark {
  --yh-tag-bg-color: var(--yh-color-primary);
  --yh-tag-border-color: var(--yh-color-primary);
  --yh-tag-text-color: #ffffff;
}

.yh-tag--plain {
  --yh-tag-bg-color: transparent;
}

.yh-tag {
  /* 可选中标签 */
}
.yh-tag.is-checkable {
  cursor: pointer;
  user-select: none;
}
.yh-tag.is-checkable:hover {
  opacity: 0.85;
}
.yh-tag.is-checkable:active {
  opacity: 0.7;
}

.yh-tag {
  /* 选中状态 */
}
.yh-tag.is-checked {
  background-color: var(--yh-tag-text-color);
  border-color: var(--yh-tag-text-color);
  color: #ffffff;
}
.yh-tag.is-checked:hover {
  opacity: 0.9;
}

.yh-tag {
  /* 圆角标签 */
}
.yh-tag.is-round {
  border-radius: var(--yh-radius-round);
}

.yh-tag {
  /* 可关闭标签 */
}
.yh-tag.is-closable {
  padding-right: 6px;
}

.yh-tag__close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 4px;
  width: 14px;
  height: 14px;
  cursor: pointer;
  border-radius: 50%;
  transition: all 0.15s ease;
}
.yh-tag__close svg {
  width: 10px;
  height: 10px;
}
.yh-tag__close:hover {
  color: #ffffff;
  background-color: var(--yh-tag-text-color);
}

.yh-tag__content {
  display: inline-block;
  vertical-align: middle;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 150px;
}

.yh-tag__icon {
  margin-right: 4px;
  display: inline-flex;
}

.yh-tag__suffix-icon {
  margin-left: 4px;
  display: inline-flex;
}
</style>
