<script setup>
import { computed, ref, useSlots } from "vue";
import { useNamespace, useConfig } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
defineOptions({
  name: "YhButton"
});
const props = defineProps({
  type: { type: null, required: false, default: "default" },
  size: { type: null, required: false },
  disabled: { type: Boolean, required: false, default: false },
  loading: { type: Boolean, required: false, default: false },
  plain: { type: Boolean, required: false, default: false },
  round: { type: Boolean, required: false, default: false },
  circle: { type: Boolean, required: false, default: false },
  text: { type: Boolean, required: false, default: false },
  link: { type: Boolean, required: false, default: false },
  nativeType: { type: null, required: false, default: "button" },
  autofocus: { type: Boolean, required: false, default: false },
  icon: { type: null, required: false },
  suffixIcon: { type: null, required: false },
  iconPosition: { type: null, required: false, default: "left" },
  color: { type: String, required: false },
  tag: { type: null, required: false, default: "button" },
  block: { type: Boolean, required: false, default: false },
  themeOverrides: { type: null, required: false }
});
const emit = defineEmits(["click"]);
const slots = useSlots();
const ns = useNamespace("button");
const { globalSize } = useConfig();
const { themeStyle } = useComponentTheme(
  "button",
  computed(() => props.themeOverrides)
);
const actualSize = computed(() => props.size || globalSize.value || "default");
const buttonRef = ref();
const hasIcon = computed(() => props.icon || slots.icon);
const hasSuffixIcon = computed(() => props.suffixIcon || slots.suffixIcon);
const isVertical = computed(() => props.iconPosition === "top" || props.iconPosition === "bottom");
const buttonClasses = computed(() => [
  ns.b(),
  props.type !== "default" && ns.m(props.type),
  actualSize.value !== "default" && ns.m(actualSize.value),
  ns.is("disabled", props.disabled),
  ns.is("loading", props.loading),
  ns.is("plain", props.plain),
  ns.is("round", props.round),
  ns.is("circle", props.circle),
  ns.is("text", props.text),
  ns.is("link", props.link),
  ns.is("block", props.block),
  ns.is("vertical", isVertical.value),
  ns.is("custom-color", !!props.color)
]);
const buttonStyles = computed(() => {
  const base = themeStyle.value;
  if (!props.color) return base;
  const color = props.color;
  return {
    ...base,
    "--yh-button-bg-color": props.plain ? "transparent" : color,
    "--yh-button-text-color": props.plain ? color : "var(--yh-color-white)",
    "--yh-button-border-color": color,
    "--yh-button-hover-bg-color": color,
    "--yh-button-hover-text-color": "var(--yh-color-white)",
    "--yh-button-hover-border-color": color,
    "--yh-button-active-bg-color": color,
    "--yh-button-active-border-color": color
  };
});
const handleClick = (event) => {
  if (props.disabled || props.loading) {
    event.preventDefault();
    return;
  }
  emit("click", event);
};
defineExpose({
  ref: buttonRef.value,
  size: actualSize.value,
  type: props.type,
  disabled: props.disabled
});
</script>

<template>
  <component
    :is="tag"
    ref="buttonRef"
    :class="buttonClasses"
    :style="buttonStyles"
    :type="tag === 'button' ? nativeType : void 0"
    :disabled="disabled || loading"
    :autofocus="autofocus"
    @click="handleClick"
  >
    <!-- 1. 加载图标 (优先级最高，存在时通常隐藏原图标) -->
    <template v-if="loading">
      <slot name="loading">
        <span :class="ns.e('loading-icon')">
          <svg :class="ns.e('loading-svg')" viewBox="0 0 1024 1024">
            <path
              fill="currentColor"
              d="M512 64a32 32 0 0 1 32 32v192a32 32 0 0 1-64 0V96a32 32 0 0 1 32-32zm0 640a32 32 0 0 1 32 32v192a32 32 0 1 1-64 0V736a32 32 0 0 1 32-32zm448-192a32 32 0 0 1-32 32H736a32 32 0 1 1 0-64h192a32 32 0 0 1 32 32zm-640 0a32 32 0 0 1-32 32H96a32 32 0 0 1 0-64h192a32 32 0 0 1 32 32zM195.2 195.2a32 32 0 0 1 45.248 0L376.32 331.008a32 32 0 0 1-45.248 45.248L195.2 240.448a32 32 0 0 1 0-45.248zm452.544 452.544a32 32 0 0 1 45.248 0L828.8 783.552a32 32 0 0 1-45.248 45.248L647.744 692.992a32 32 0 0 1 0-45.248zM828.8 195.264a32 32 0 0 1 0 45.184L692.992 376.32a32 32 0 0 1-45.248-45.248l135.808-135.808a32 32 0 0 1 45.248 0zm-452.544 452.48a32 32 0 0 1 0 45.248L240.448 828.8a32 32 0 0 1-45.248-45.248l135.808-135.808a32 32 0 0 1 45.248 0z"
            />
          </svg>
        </span>
      </slot>
    </template>

    <!-- 2. 前置/上置图标 -->
    <span
      v-if="!loading && hasIcon && (iconPosition === 'left' || iconPosition === 'top')"
      :class="ns.e('icon')"
    >
      <slot name="icon">
        <component :is="icon" v-if="icon && typeof icon !== 'string'" />
      </slot>
    </span>

    <!-- 3. 内容区 -->
    <span v-if="$slots.default" :class="ns.e('text')">
      <slot />
    </span>

    <!-- 4. 后置/下置图标 -->
    <span
      v-if="!loading && hasIcon && (iconPosition === 'right' || iconPosition === 'bottom')"
      :class="ns.e('icon')"
    >
      <slot name="icon">
        <component :is="icon" v-if="icon && typeof icon !== 'string'" />
      </slot>
    </span>

    <!-- 5. 独立的后缀图标（suffixIcon 插槽） -->
    <span v-if="!loading && hasSuffixIcon" :class="ns.e('suffix-icon')">
      <slot name="suffixIcon">
        <component :is="suffixIcon" v-if="suffixIcon && typeof suffixIcon !== 'string'" />
      </slot>
    </span>
  </component>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
/* 尺寸变量映射 */
/* 基础类型混入 */
.yh-button {
  /* 基础变量与默认行为 */
  --yh-button-height: var(--yh-component-size-default);
  --yh-button-bg-color: var(--yh-fill-color-blank);
  --yh-button-text-color: var(--yh-text-color-regular);
  --yh-button-border-color: var(--yh-border-color);
  --yh-button-hover-bg-color: var(--yh-color-primary-light-9);
  --yh-button-hover-text-color: var(--yh-color-primary);
  --yh-button-hover-border-color: var(--yh-color-primary-light-8);
  --yh-button-active-bg-color: var(--yh-color-primary-light-8);
  --yh-button-active-border-color: var(--yh-color-primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  box-sizing: border-box;
  height: var(--yh-button-height);
  padding: var(--yh-button-padding, 0 var(--yh-component-size-default-padding));
  font-size: var(--yh-button-font-size, var(--yh-component-size-default-font));
  font-weight: var(--yh-font-weight-medium);
  line-height: 1;
  white-space: nowrap;
  appearance: none;
  cursor: pointer;
  user-select: none;
  color: var(--yh-button-text-color);
  background-color: var(--yh-button-bg-color);
  border: var(--yh-border-width) var(--yh-border-style) var(--yh-button-border-color);
  border-radius: var(--yh-radius-base);
  outline: none;
  transition: var(--yh-transition-fast);
}
.yh-button:hover, .yh-button:focus {
  color: var(--yh-button-hover-text-color);
  background-color: var(--yh-button-hover-bg-color);
  border-color: var(--yh-button-hover-border-color);
}
.yh-button:active {
  background-color: var(--yh-button-active-bg-color);
  border-color: var(--yh-button-active-border-color);
  color: var(--yh-button-active-text-color, var(--yh-button-hover-text-color));
}
.yh-button {
  /* 自定义颜色反馈 */
}
.yh-button.is-custom-color:hover, .yh-button.is-custom-color:focus {
  filter: brightness(1.1);
}
.yh-button.is-custom-color:active {
  filter: brightness(0.9);
}
.yh-button.is-custom-color.is-plain:hover {
  background-color: var(--yh-button-border-color) !important;
}

.yh-button {
  /* 垂直布局 */
}
.yh-button.is-vertical {
  flex-direction: column;
  height: auto;
  padding: 12px;
  gap: 4px;
}
.yh-button.is-vertical .yh-button__icon {
  margin: 0;
  font-size: 1.5em;
}

.yh-button {
  /* 状态控制 */
}
.yh-button.is-disabled {
  cursor: not-allowed !important;
  opacity: 0.5;
  pointer-events: none;
}

.yh-button.is-loading {
  cursor: default !important;
  pointer-events: none;
}

.yh-button {
  /* 文字模式与链接模式（基础样式） */
}
.yh-button.is-text {
  /* 纯文本按钮：移除背景与边框，仅保留文字色变化 */
  --yh-button-bg-color: transparent;
  --yh-button-hover-bg-color: transparent;
  --yh-button-active-bg-color: transparent;
  border-color: transparent;
  background-color: transparent;
  padding-left: 0;
  padding-right: 0;
}
.yh-button.is-text:hover, .yh-button.is-text:focus {
  background-color: transparent;
  text-decoration: none;
}
.yh-button.is-text:active {
  background-color: transparent;
}
.yh-button.is-text.is-disabled {
  background-color: transparent;
}

.yh-button.is-link {
  border-color: transparent !important;
  background-color: transparent !important;
  height: auto;
  padding: 2px;
}
.yh-button.is-link:hover {
  text-decoration: underline;
  background-color: transparent !important;
}

.yh-button {
  /* 类型应用 */
}
.yh-button--primary {
  --yh-button-bg-color: var(--yh-color-primary);
  --yh-button-text-color: var(--yh-color-primary-text, var(--yh-color-white, #ffffff));
  --yh-button-border-color: var(--yh-color-primary);
  --yh-button-hover-bg-color: var(--yh-color-primary-light-3);
  --yh-button-hover-text-color: var(--yh-color-primary-text, var(--yh-color-white, #ffffff));
  --yh-button-hover-border-color: var(--yh-color-primary-light-3);
  --yh-button-active-bg-color: var(--yh-color-primary-dark-2);
  --yh-button-active-border-color: var(--yh-color-primary-dark-2);
}
.yh-button--primary.is-plain {
  --yh-button-bg-color: var(--yh-color-primary-light-9);
  --yh-button-text-color: var(--yh-color-primary);
  --yh-button-border-color: var(--yh-color-primary-light-5);
  --yh-button-hover-bg-color: var(--yh-color-primary);
  --yh-button-hover-text-color: var(--yh-color-white, #ffffff);
  --yh-button-hover-border-color: var(--yh-color-primary);
}

.yh-button--primary {
  /* 针对 Text 和 Link 模式的颜色覆盖 */
}
.yh-button--primary.is-text {
  --yh-button-text-color: var(--yh-color-primary);
  --yh-button-hover-text-color: var(--yh-color-primary-light-3);
  --yh-button-active-text-color: var(--yh-color-primary-dark-2);
}

.yh-button--primary.is-link {
  --yh-button-text-color: var(--yh-color-primary);
  --yh-button-hover-text-color: var(--yh-color-primary-light-3);
  --yh-button-active-text-color: var(--yh-color-primary-dark-2);
}

.yh-button--success {
  --yh-button-bg-color: var(--yh-color-success);
  --yh-button-text-color: var(--yh-color-success-text, var(--yh-color-white, #ffffff));
  --yh-button-border-color: var(--yh-color-success);
  --yh-button-hover-bg-color: var(--yh-color-success-light-3);
  --yh-button-hover-text-color: var(--yh-color-success-text, var(--yh-color-white, #ffffff));
  --yh-button-hover-border-color: var(--yh-color-success-light-3);
  --yh-button-active-bg-color: var(--yh-color-success-dark-2);
  --yh-button-active-border-color: var(--yh-color-success-dark-2);
}
.yh-button--success.is-plain {
  --yh-button-bg-color: var(--yh-color-success-light-9);
  --yh-button-text-color: var(--yh-color-success);
  --yh-button-border-color: var(--yh-color-success-light-5);
  --yh-button-hover-bg-color: var(--yh-color-success);
  --yh-button-hover-text-color: var(--yh-color-white, #ffffff);
  --yh-button-hover-border-color: var(--yh-color-success);
}

.yh-button--success {
  /* 针对 Text 和 Link 模式的颜色覆盖 */
}
.yh-button--success.is-text {
  --yh-button-text-color: var(--yh-color-success);
  --yh-button-hover-text-color: var(--yh-color-success-light-3);
  --yh-button-active-text-color: var(--yh-color-success-dark-2);
}

.yh-button--success.is-link {
  --yh-button-text-color: var(--yh-color-success);
  --yh-button-hover-text-color: var(--yh-color-success-light-3);
  --yh-button-active-text-color: var(--yh-color-success-dark-2);
}

.yh-button--warning {
  --yh-button-bg-color: var(--yh-color-warning);
  --yh-button-text-color: var(--yh-color-warning-text, var(--yh-color-white, #ffffff));
  --yh-button-border-color: var(--yh-color-warning);
  --yh-button-hover-bg-color: var(--yh-color-warning-light-3);
  --yh-button-hover-text-color: var(--yh-color-warning-text, var(--yh-color-white, #ffffff));
  --yh-button-hover-border-color: var(--yh-color-warning-light-3);
  --yh-button-active-bg-color: var(--yh-color-warning-dark-2);
  --yh-button-active-border-color: var(--yh-color-warning-dark-2);
}
.yh-button--warning.is-plain {
  --yh-button-bg-color: var(--yh-color-warning-light-9);
  --yh-button-text-color: var(--yh-color-warning);
  --yh-button-border-color: var(--yh-color-warning-light-5);
  --yh-button-hover-bg-color: var(--yh-color-warning);
  --yh-button-hover-text-color: var(--yh-color-white, #ffffff);
  --yh-button-hover-border-color: var(--yh-color-warning);
}

.yh-button--warning {
  /* 针对 Text 和 Link 模式的颜色覆盖 */
}
.yh-button--warning.is-text {
  --yh-button-text-color: var(--yh-color-warning);
  --yh-button-hover-text-color: var(--yh-color-warning-light-3);
  --yh-button-active-text-color: var(--yh-color-warning-dark-2);
}

.yh-button--warning.is-link {
  --yh-button-text-color: var(--yh-color-warning);
  --yh-button-hover-text-color: var(--yh-color-warning-light-3);
  --yh-button-active-text-color: var(--yh-color-warning-dark-2);
}

.yh-button--danger {
  --yh-button-bg-color: var(--yh-color-danger);
  --yh-button-text-color: var(--yh-color-danger-text, var(--yh-color-white, #ffffff));
  --yh-button-border-color: var(--yh-color-danger);
  --yh-button-hover-bg-color: var(--yh-color-danger-light-3);
  --yh-button-hover-text-color: var(--yh-color-danger-text, var(--yh-color-white, #ffffff));
  --yh-button-hover-border-color: var(--yh-color-danger-light-3);
  --yh-button-active-bg-color: var(--yh-color-danger-dark-2);
  --yh-button-active-border-color: var(--yh-color-danger-dark-2);
}
.yh-button--danger.is-plain {
  --yh-button-bg-color: var(--yh-color-danger-light-9);
  --yh-button-text-color: var(--yh-color-danger);
  --yh-button-border-color: var(--yh-color-danger-light-5);
  --yh-button-hover-bg-color: var(--yh-color-danger);
  --yh-button-hover-text-color: var(--yh-color-white, #ffffff);
  --yh-button-hover-border-color: var(--yh-color-danger);
}

.yh-button--danger {
  /* 针对 Text 和 Link 模式的颜色覆盖 */
}
.yh-button--danger.is-text {
  --yh-button-text-color: var(--yh-color-danger);
  --yh-button-hover-text-color: var(--yh-color-danger-light-3);
  --yh-button-active-text-color: var(--yh-color-danger-dark-2);
}

.yh-button--danger.is-link {
  --yh-button-text-color: var(--yh-color-danger);
  --yh-button-hover-text-color: var(--yh-color-danger-light-3);
  --yh-button-active-text-color: var(--yh-color-danger-dark-2);
}

.yh-button--info {
  --yh-button-bg-color: var(--yh-color-info);
  --yh-button-text-color: var(--yh-color-info-text, var(--yh-color-white, #ffffff));
  --yh-button-border-color: var(--yh-color-info);
  --yh-button-hover-bg-color: var(--yh-color-info-light-3);
  --yh-button-hover-text-color: var(--yh-color-info-text, var(--yh-color-white, #ffffff));
  --yh-button-hover-border-color: var(--yh-color-info-light-3);
  --yh-button-active-bg-color: var(--yh-color-info-dark-2);
  --yh-button-active-border-color: var(--yh-color-info-dark-2);
}
.yh-button--info.is-plain {
  --yh-button-bg-color: var(--yh-color-info-light-9);
  --yh-button-text-color: var(--yh-color-info);
  --yh-button-border-color: var(--yh-color-info-light-5);
  --yh-button-hover-bg-color: var(--yh-color-info);
  --yh-button-hover-text-color: var(--yh-color-white, #ffffff);
  --yh-button-hover-border-color: var(--yh-color-info);
}

.yh-button--info {
  /* 针对 Text 和 Link 模式的颜色覆盖 */
}
.yh-button--info.is-text {
  --yh-button-text-color: var(--yh-color-info);
  --yh-button-hover-text-color: var(--yh-color-info-light-3);
  --yh-button-active-text-color: var(--yh-color-info-dark-2);
}

.yh-button--info.is-link {
  --yh-button-text-color: var(--yh-color-info);
  --yh-button-hover-text-color: var(--yh-color-info-light-3);
  --yh-button-active-text-color: var(--yh-color-info-dark-2);
}

.yh-button {
  /* 尺寸应用 */
}
.yh-button--large {
  --yh-button-height: var(--yh-component-size-large);
  --yh-button-padding: 0 19px;
  --yh-button-font-size: var(--yh-component-size-large-font);
}
.yh-button--small {
  --yh-button-height: var(--yh-component-size-small);
  --yh-button-padding: 0 11px;
  --yh-button-font-size: var(--yh-component-size-small-font);
}
.yh-button.is-round {
  border-radius: var(--yh-radius-round);
}

.yh-button.is-circle {
  border-radius: var(--yh-radius-circle) !important;
  width: var(--yh-button-height);
  padding: 0;
  flex-shrink: 0;
}

.yh-button {
  /* 加载图标 */
}
.yh-button__loading-icon {
  display: inline-flex;
}

.yh-button__loading-svg {
  width: 1em;
  height: 1em;
  fill: currentColor;
  animation: yh-rotate 1.5s linear infinite;
}

@keyframes yh-rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
