<script setup>
import { ref, computed, onMounted, onBeforeUnmount, useSlots } from "vue";
import { useNamespace, useLocale } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
import { alertProps, alertEmits } from "./alert";
defineOptions({
  name: "YhAlert"
});
const props = defineProps(alertProps);
const emit = defineEmits(alertEmits);
const ns = useNamespace("alert");
const { t } = useLocale();
const { themeStyle } = useComponentTheme(
  "alert",
  computed(() => props.themeOverrides)
);
const slots = useSlots();
const visible = ref(true);
const progress = ref(100);
const remainingTime = ref(props.duration);
const lastUpdate = ref(0);
const isPaused = ref(false);
let timer = null;
let rafId = null;
const updateProgress = () => {
  if (isPaused.value || !visible.value) return;
  const now = Date.now();
  const elapsed = now - lastUpdate.value;
  lastUpdate.value = now;
  remainingTime.value = Math.max(0, remainingTime.value - elapsed);
  progress.value = remainingTime.value / props.duration * 100;
  if (remainingTime.value > 0) {
    rafId = requestAnimationFrame(updateProgress);
  } else {
    visible.value = false;
  }
};
const alertClass = computed(() => [
  ns.b(),
  ns.m(props.type),
  ns.m(props.effect),
  ns.is("center", props.center),
  ns.is("scrollable", props.scrollable),
  ns.is("pause-on-hover", props.pauseOnHover),
  ns.is("with-description", !!props.description || !!slots.default)
]);
const alertStyle = computed(() => ({
  ...themeStyle.value,
  "--yh-alert-scroll-speed": `${props.scrollSpeed}s`
}));
const progressStyle = computed(() => ({
  width: `${progress.value}%`,
  transition: "none"
}));
const handleClose = (evt) => {
  visible.value = false;
  emit("close", evt);
  clearAutoClose();
};
const clearAutoClose = () => {
  if (timer) clearTimeout(timer);
  if (rafId) cancelAnimationFrame(rafId);
};
const handleMouseEnter = () => {
  if (props.pauseOnHover && props.duration > 0) {
    isPaused.value = true;
    if (timer) clearTimeout(timer);
  }
};
const handleMouseLeave = () => {
  if (props.pauseOnHover && props.duration > 0 && visible.value) {
    isPaused.value = false;
    lastUpdate.value = Date.now();
    if (props.showProgress) {
      rafId = requestAnimationFrame(updateProgress);
    }
    timer = setTimeout(() => {
      visible.value = false;
    }, remainingTime.value);
  }
};
onMounted(() => {
  if (props.duration > 0) {
    remainingTime.value = props.duration;
    lastUpdate.value = Date.now();
    if (props.showProgress) {
      rafId = requestAnimationFrame(updateProgress);
    }
    timer = setTimeout(() => {
      visible.value = false;
    }, props.duration);
  }
});
onBeforeUnmount(() => {
  clearAutoClose();
});
</script>

<template>
  <Transition :name="`${ns.namespace.value}-fade`">
    <div
      v-if="visible"
      :class="alertClass"
      :style="alertStyle"
      role="alert"
      @mouseenter="handleMouseEnter"
      @mouseleave="handleMouseLeave"
    >
      <!-- 图标部分 -->
      <div v-if="showIcon" :class="ns.e('icon')">
        <slot name="icon">
          <component :is="icon" v-if="icon" />
          <!-- 默认类型图标 (这里可以预置几个 SVG) -->
          <svg v-else viewBox="0 0 1024 1024" width="20" height="20">
            <path
              v-if="type === 'success'"
              fill="currentColor"
              d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm-55.8 560.3L342.3 510.4a32 32 0 1 0-45.2 45.2l136 136a32 32 0 0 0 45.2 0l311.4-311.4a32 32 0 1 0-45.2-45.2L456.2 624.3z"
            />
            <path
              v-if="type === 'info'"
              fill="currentColor"
              d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm0 192a48 48 0 1 0 0 96 48 48 0 0 0 0-96zm0 160a32 32 0 0 0-32 32v320a32 32 0 0 0 64 0V448a32 32 0 0 0-32-32z"
            />
            <path
              v-if="type === 'warning'"
              fill="currentColor"
              d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm0 192a48 48 0 1 0 0 96 48 48 0 0 0 0-96zm0 160a32 32 0 0 0-32 32v320a32 32 0 0 0 64 0V448a32 32 0 0 0-32-32z"
            />
            <path
              v-if="type === 'error'"
              fill="currentColor"
              d="M512 64a448 448 0 1 1 0 896 448 448 0 0 1 0-896zm170.3 535.1a32 32 0 0 0 0-45.2L557.3 512l125-125a32 32 0 0 0-45.2-45.2L512 466.7l-125-125a32 32 0 0 0-45.2 45.2l125 125-125 125a32 32 0 0 0 45.2 45.2l125-125 125 125a32 32 0 0 0 45.2 0z"
            />
          </svg>
        </slot>
      </div>

      <!-- 内容部分 -->
      <div :class="[ns.e('content')]">
        <span v-if="title || slots.title" :class="ns.e('title')">
          <slot name="title">{{ title }}</slot>
        </span>
        <div :class="ns.e('description-wrapper')">
          <div :class="ns.e('description-content')">
            <p v-if="description || slots.default" :class="ns.e('description')">
              <slot>{{ description }}</slot>
            </p>
            <!-- 极致丝滑：当开启滚动时，自动复制一份内容实现无缝衔接 -->
            <p v-if="scrollable && (description || slots.default)" :class="ns.e('description')">
              <slot>{{ description }}</slot>
            </p>
          </div>
        </div>

        <!-- Action 扩展槽 -->
        <div v-if="slots.action" :class="ns.e('action')">
          <slot name="action" />
        </div>
      </div>

      <!-- 关闭按钮 -->
      <div
        v-if="closable"
        :class="ns.e('close')"
        :aria-label="t('alert.close')"
        @click="handleClose"
      >
        <slot name="close">
          <span v-if="closeText">{{ closeText }}</span>
          <component :is="closeIcon" v-else-if="closeIcon" style="width: 16px; height: 16px" />
          <svg v-else viewBox="0 0 1024 1024" width="16" height="16">
            <path
              fill="currentColor"
              d="M810.7 274.7L749.3 213.3 512 450.7 274.7 213.3 213.3 274.7 450.7 512 213.3 749.3 274.7 810.7 512 573.3 749.3 810.7 810.7 749.3 573.3 512z"
            />
          </svg>
        </slot>
      </div>

      <!-- 倒计时进度条 -->
      <div v-if="showProgress && duration > 0" :class="ns.e('progress-track')">
        <div :class="ns.e('progress-bar')" :style="progressStyle"></div>
      </div>
    </div>
  </Transition>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
.yh-alert {
  width: 100%;
  padding: 12px 16px;
  border-radius: 12px;
  position: relative;
  display: flex;
  align-items: center;
  overflow: hidden;
  box-sizing: border-box;
  margin-bottom: 20px;
  transition: opacity 0.3s, transform 0.3s;
}
.yh-alert.is-center {
  justify-content: center;
  text-align: center;
}
.yh-alert.is-center .yh-alert__icon {
  margin-right: 8px;
}
.yh-alert.is-with-description {
  padding: 16px;
  align-items: flex-start;
}
.yh-alert.is-with-description .yh-alert__icon {
  font-size: 24px;
  margin-right: 12px;
}
.yh-alert.is-with-description .yh-alert__title {
  font-weight: 700;
  margin-bottom: 4px;
  font-size: 16px;
}
.yh-alert {
  /* 状态颜色定义 */
}
.yh-alert--success {
  color: var(--yh-color-success);
  background-color: var(--yh-color-success-light-9);
  border: 1px solid transparent;
}
.yh-alert--success--outline {
  background-color: transparent;
  border-color: var(--yh-color-success);
}

.yh-alert--success--dark {
  background-color: var(--yh-color-success);
  color: #fff;
}
.yh-alert--success--dark .yh-alert__description {
  color: rgba(255, 255, 255, 0.9);
}

.yh-alert--success--glass {
  background: rgba(var(--yh-color-success-light-9), 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(var(--yh-color-success), 0.2);
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.05);
}

.yh-alert--success .yh-alert__progress-bar {
  background-color: var(--yh-color-success);
  opacity: 0.3;
}
.yh-alert--success {
  /* 自动适配侧边渐变，用于提升滚动丝滑感 */
}
.yh-alert--success .yh-alert__description-wrapper::before {
  background: linear-gradient(to right, var(--yh-color-success-light-9), transparent);
}
.yh-alert--success .yh-alert__description-wrapper::after {
  background: linear-gradient(to left, var(--yh-color-success-light-9), transparent);
}
.yh-alert--success.yh-alert--dark .yh-alert__description-wrapper::before {
  background: linear-gradient(to right, var(--yh-color-success), transparent);
}
.yh-alert--success.yh-alert--dark .yh-alert__description-wrapper::after {
  background: linear-gradient(to left, var(--yh-color-success), transparent);
}

.yh-alert--info {
  color: var(--yh-color-info);
  background-color: var(--yh-color-info-light-9);
  border: 1px solid transparent;
}
.yh-alert--info--outline {
  background-color: transparent;
  border-color: var(--yh-color-info);
}

.yh-alert--info--dark {
  background-color: var(--yh-color-info);
  color: #fff;
}
.yh-alert--info--dark .yh-alert__description {
  color: rgba(255, 255, 255, 0.9);
}

.yh-alert--info--glass {
  background: rgba(var(--yh-color-info-light-9), 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(var(--yh-color-info), 0.2);
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.05);
}

.yh-alert--info .yh-alert__progress-bar {
  background-color: var(--yh-color-info);
  opacity: 0.3;
}
.yh-alert--info {
  /* 自动适配侧边渐变，用于提升滚动丝滑感 */
}
.yh-alert--info .yh-alert__description-wrapper::before {
  background: linear-gradient(to right, var(--yh-color-info-light-9), transparent);
}
.yh-alert--info .yh-alert__description-wrapper::after {
  background: linear-gradient(to left, var(--yh-color-info-light-9), transparent);
}
.yh-alert--info.yh-alert--dark .yh-alert__description-wrapper::before {
  background: linear-gradient(to right, var(--yh-color-info), transparent);
}
.yh-alert--info.yh-alert--dark .yh-alert__description-wrapper::after {
  background: linear-gradient(to left, var(--yh-color-info), transparent);
}

.yh-alert--warning {
  color: var(--yh-color-warning);
  background-color: var(--yh-color-warning-light-9);
  border: 1px solid transparent;
}
.yh-alert--warning--outline {
  background-color: transparent;
  border-color: var(--yh-color-warning);
}

.yh-alert--warning--dark {
  background-color: var(--yh-color-warning);
  color: #fff;
}
.yh-alert--warning--dark .yh-alert__description {
  color: rgba(255, 255, 255, 0.9);
}

.yh-alert--warning--glass {
  background: rgba(var(--yh-color-warning-light-9), 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(var(--yh-color-warning), 0.2);
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.05);
}

.yh-alert--warning .yh-alert__progress-bar {
  background-color: var(--yh-color-warning);
  opacity: 0.3;
}
.yh-alert--warning {
  /* 自动适配侧边渐变，用于提升滚动丝滑感 */
}
.yh-alert--warning .yh-alert__description-wrapper::before {
  background: linear-gradient(to right, var(--yh-color-warning-light-9), transparent);
}
.yh-alert--warning .yh-alert__description-wrapper::after {
  background: linear-gradient(to left, var(--yh-color-warning-light-9), transparent);
}
.yh-alert--warning.yh-alert--dark .yh-alert__description-wrapper::before {
  background: linear-gradient(to right, var(--yh-color-warning), transparent);
}
.yh-alert--warning.yh-alert--dark .yh-alert__description-wrapper::after {
  background: linear-gradient(to left, var(--yh-color-warning), transparent);
}

.yh-alert--error {
  color: var(--yh-color-danger);
  background-color: var(--yh-color-danger-light-9);
  border: 1px solid transparent;
}
.yh-alert--error--outline {
  background-color: transparent;
  border-color: var(--yh-color-danger);
}

.yh-alert--error--dark {
  background-color: var(--yh-color-danger);
  color: #fff;
}
.yh-alert--error--dark .yh-alert__description {
  color: rgba(255, 255, 255, 0.9);
}

.yh-alert--error--glass {
  background: rgba(var(--yh-color-danger-light-9), 0.7);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(var(--yh-color-danger), 0.2);
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.05);
}

.yh-alert--error .yh-alert__progress-bar {
  background-color: var(--yh-color-danger);
  opacity: 0.3;
}
.yh-alert--error {
  /* 自动适配侧边渐变，用于提升滚动丝滑感 */
}
.yh-alert--error .yh-alert__description-wrapper::before {
  background: linear-gradient(to right, var(--yh-color-danger-light-9), transparent);
}
.yh-alert--error .yh-alert__description-wrapper::after {
  background: linear-gradient(to left, var(--yh-color-danger-light-9), transparent);
}
.yh-alert--error.yh-alert--dark .yh-alert__description-wrapper::before {
  background: linear-gradient(to right, var(--yh-color-danger), transparent);
}
.yh-alert--error.yh-alert--dark .yh-alert__description-wrapper::after {
  background: linear-gradient(to left, var(--yh-color-danger), transparent);
}

.yh-alert__icon {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  flex-shrink: 0;
}

.yh-alert__content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.yh-alert__description-wrapper {
  width: 100%;
  overflow: hidden;
  position: relative;
}
.yh-alert__description-wrapper::before, .yh-alert__description-wrapper::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 20px;
  z-index: 2;
  pointer-events: none;
}
.yh-alert__description-wrapper::before {
  left: 0;
}
.yh-alert__description-wrapper::after {
  right: 0;
}

.yh-alert.is-scrollable .yh-alert__description-content {
  display: inline-flex;
  animation: yh-alert-marquee-endless var(--yh-alert-scroll-speed, 15s) linear infinite;
  will-change: transform;
  padding-left: 20px; /* 给起始位置一点呼吸空间 */
}
.yh-alert.is-scrollable .yh-alert__description {
  white-space: nowrap;
  padding-right: 50px; /* 两个拷贝之间的间距 */
  display: inline-block;
  backface-visibility: hidden;
  transform: translateZ(0);
}
.yh-alert.is-scrollable.is-pause-on-hover:hover .yh-alert__description-content {
  animation-play-state: paused;
}
.yh-alert__title {
  font-size: 14px;
  line-height: 1.5;
  font-weight: 600;
}

.yh-alert__description {
  font-size: 13px;
  margin: 0;
  line-height: 1.5;
  color: inherit;
  opacity: 0.85;
}

.yh-alert__action {
  margin-top: 8px;
  display: flex;
  gap: 12px;
}

.yh-alert__close {
  cursor: pointer;
  margin-left: 12px;
  flex-shrink: 0;
  transition: opacity 0.2s;
  opacity: 0.6;
  font-size: 13px;
  display: flex;
  align-items: center;
  line-height: normal;
}
.yh-alert__close:hover {
  opacity: 1;
}

.yh-alert__progress-track {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: rgba(0, 0, 0, 0.03);
}

.yh-alert__progress-bar {
  height: 100%;
  width: 100%;
}

@keyframes yh-alert-marquee-endless {
  0% {
    transform: translate3d(0, 0, 0);
  }
  100% {
    transform: translate3d(-50%, 0, 0);
  }
}
.yh-alert-fade-enter-active,
.yh-alert-fade-leave-active {
  transition: all 0.3s cubic-bezier(0.55, 0, 0.1, 1);
}

.yh-alert-fade-enter-from,
.yh-alert-fade-leave-to {
  opacity: 0;
  transform: translateY(-20px) scaleY(0.5);
}
</style>
