<script setup>
import { computed, inject, onMounted, onUnmounted, getCurrentInstance, watch, ref } from "vue";
import { useNamespace } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
import { stepProps } from "./step";
import { STEPS_INJECTION_KEY } from "./steps";
defineOptions({
  name: "YhStep"
});
const props = defineProps(stepProps);
const ns = useNamespace("step");
const { themeStyle } = useComponentTheme(
  "step",
  computed(() => props.themeOverrides)
);
const instance = getCurrentInstance();
const uid = instance?.uid ?? 0;
const stepsContext = inject(STEPS_INJECTION_KEY);
const hasRendered = ref(!props.lazy);
const stepIndex = computed(() => {
  return stepsContext?.steps.value.findIndex((s) => s.uid === uid) ?? 0;
});
const currentStatus = computed(() => {
  if (props.status) return props.status;
  const active = stepsContext?.props.active ?? 0;
  const finishStatus = stepsContext?.props.finishStatus ?? "finish";
  const processStatus = stepsContext?.props.processStatus ?? "process";
  if (stepIndex.value < active) {
    return finishStatus;
  } else if (stepIndex.value === active) {
    return processStatus;
  }
  return "wait";
});
const isActive = computed(() => stepIndex.value === (stepsContext?.props.active ?? 0));
watch(isActive, (active) => {
  if (active && props.lazy) {
    hasRendered.value = true;
  }
});
const isLast = computed(() => {
  const total = stepsContext?.steps.value.length ?? 0;
  return stepIndex.value === total - 1;
});
const progressPercent = computed(() => {
  if (!stepsContext?.props.showProgress) return 0;
  const active = stepsContext?.props.active ?? 0;
  if (stepIndex.value < active) {
    return 100;
  } else if (stepIndex.value === active) {
    return props.progress;
  }
  return 0;
});
const _actualDirection = computed(() => {
  if (stepsContext?.isResponsiveVertical.value) return "vertical";
  return stepsContext?.props.direction ?? "horizontal";
});
const stepStyle = computed(() => {
  const styles = {
    ...themeStyle.value
  };
  const space = stepsContext?.props.space;
  if (!space) return styles;
  const spaceValue = typeof space === "number" ? `${space}px` : space;
  styles.flexBasis = spaceValue;
  if (!isLast.value) {
    styles.maxWidth = spaceValue;
  }
  return styles;
});
const handleClick = () => {
  stepsContext?.handleStepClick(stepIndex.value, props.disabled);
};
onMounted(() => {
  stepsContext?.registerStep({
    uid,
    title: props.title,
    description: props.description,
    icon: props.icon,
    status: currentStatus.value,
    disabled: props.disabled,
    time: props.time,
    progress: props.progress
  });
});
watch(
  () => ({
    title: props.title,
    description: props.description,
    icon: props.icon,
    status: currentStatus.value,
    disabled: props.disabled,
    time: props.time,
    progress: props.progress
  }),
  (config) => {
    stepsContext?.registerStep({
      uid,
      ...config
    });
  }
);
onUnmounted(() => {
  stepsContext?.unregisterStep(uid);
});
const statusIconSvg = computed(() => {
  if (currentStatus.value === "finish" || currentStatus.value === "success") {
    return '<path fill="currentColor" d="M406.6 600.6L281.5 475.5a32 32 0 0 0-45.3 45.2l147.5 147.5a32 32 0 0 0 45.2 0l294.6-294.6a32 32 0 0 0-45.2-45.2L406.6 600.6z"/>';
  }
  if (currentStatus.value === "error") {
    return '<path fill="currentColor" d="M560.4 512l133.9-133.9a32 32 0 1 0-45.2-45.3L515.2 466.7 381.3 332.8a32 32 0 1 0-45.3 45.3L469.9 512 336 645.9a32 32 0 1 0 45.3 45.2l133.9-133.9 133.9 133.9a32 32 0 1 0 45.2-45.2L560.4 512z"/>';
  }
  return "";
});
const stepClass = computed(() => [
  ns.b(),
  ns.is("simple", stepsContext?.props.simple),
  ns.is(currentStatus.value, true),
  ns.is("last", isLast.value),
  ns.is("center", stepsContext?.props.alignCenter),
  ns.is("disabled", props.disabled),
  ns.is("clickable", stepsContext?.props.clickable && !props.disabled),
  ns.is("timeline", stepsContext?.props.showTimeline),
  ns.is("label-vertical", stepsContext?.props.labelPlacement === "vertical")
]);
</script>

<template>
  <div :class="stepClass" :style="stepStyle" @click="handleClick">
    <!-- 时间信息（时间线模式） -->
    <div v-if="stepsContext?.props.showTimeline && time" :class="ns.e('time')">
      <slot name="time">{{ time }}</slot>
    </div>

    <!-- 连接线 -->
    <div v-if="!isLast" :class="ns.e('line')">
      <i
        :class="ns.e('line-inner')"
        :style="stepsContext?.props.showProgress ? {
  width: `${progressPercent}%`
} : {}"
      ></i>
    </div>

    <!-- 图标区 -->
    <div :class="[ns.e('head'), ns.is(currentStatus, true)]">
      <slot name="icon">
        <!-- 自定义图标 -->
        <span v-if="icon" :class="[ns.e('icon'), icon]"></span>
        <!-- 内置状态图标 -->
        <svg
          v-else-if="statusIconSvg"
          :class="ns.e('icon-inner')"
          viewBox="0 0 1024 1024"
          width="20"
          height="20"
        >
          <!-- eslint-disable-next-line vue/no-v-html -->
          <g v-html="statusIconSvg"></g>
        </svg>
        <!-- 数字 -->
        <span v-else :class="ns.e('number')">{{ stepIndex + 1 }}</span>
      </slot>
    </div>

    <!-- 主体内容 -->
    <div :class="ns.e('main')">
      <div :class="[ns.e('title'), ns.is(currentStatus, true)]">
        <slot name="title">{{ title }}</slot>
      </div>
      <div v-if="description || $slots.description" :class="ns.e('description')">
        <slot name="description">{{ description }}</slot>
      </div>
      <!-- 自定义内容（延迟加载支持） -->
      <div v-if="$slots.default && (hasRendered || !lazy)" :class="ns.e('content')">
        <slot></slot>
      </div>
    </div>
  </div>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
.yh-steps {
  --yh-steps-icon-size: 28px;
  --yh-steps-line-color: var(--yh-border-color-light);
  --yh-steps-finish-color: var(--yh-color-primary);
  --yh-steps-process-color: var(--yh-color-primary);
  --yh-steps-wait-color: var(--yh-text-color-placeholder);
  --yh-steps-error-color: var(--yh-color-danger);
  --yh-steps-success-color: var(--yh-color-success);
  display: flex;
}
.yh-steps--horizontal {
  flex-direction: row;
}

.yh-steps--vertical {
  flex-direction: column;
}
.yh-steps--vertical .yh-step {
  flex-direction: row;
  padding-bottom: 20px;
}
.yh-steps--vertical .yh-step .yh-step__line {
  position: absolute;
  top: calc(var(--yh-steps-icon-size) + 8px);
  left: calc(var(--yh-steps-icon-size) / 2);
  width: 1px;
  height: calc(100% - var(--yh-steps-icon-size) - 8px);
  background: var(--yh-steps-line-color);
}
.yh-steps--vertical .yh-step .yh-step__line-inner {
  display: block;
  width: 100%;
  height: 0;
  background: var(--yh-steps-finish-color);
  transition: height var(--yh-duration-base);
}
.yh-steps--vertical .yh-step.is-finish .yh-step__line-inner, .yh-steps--vertical .yh-step.is-success .yh-step__line-inner {
  height: 100%;
}
.yh-steps--vertical .yh-step .yh-step__main {
  padding-left: 16px;
}

.yh-steps--size-small {
  --yh-steps-icon-size: 22px;
}
.yh-steps--size-small .yh-step__head {
  border-width: 1.5px;
}
.yh-steps--size-small .yh-step__number {
  font-size: 12px;
}
.yh-steps--size-small .yh-step__title {
  font-size: var(--yh-font-size-sm);
}
.yh-steps--size-small .yh-step__description {
  font-size: 12px;
}

.yh-steps--label-vertical .yh-step {
  flex-direction: column;
  align-items: flex-start;
}
.yh-steps--label-vertical .yh-step .yh-step__head {
  margin-bottom: 8px;
}
.yh-steps--label-vertical .yh-step .yh-step__main {
  padding-top: 0;
}

.yh-steps.is-center .yh-step {
  flex: 1 !important;
  flex-direction: column;
  align-items: center;
}
.yh-steps.is-center .yh-step .yh-step__head {
  margin-bottom: 8px;
}
.yh-steps.is-center .yh-step .yh-step__main {
  text-align: center;
  padding-top: 0;
}
.yh-steps.is-center .yh-step .yh-step__line {
  left: calc(50% + var(--yh-steps-icon-size) / 2 + 5px);
  right: calc(-50% + var(--yh-steps-icon-size) / 2 + 5px);
  width: auto;
}
.yh-steps.is-simple {
  background-color: var(--yh-fill-color-light);
  border-radius: var(--yh-radius-md);
  padding: 12px 20px;
}
.yh-steps.is-simple .yh-step {
  flex-direction: row;
  align-items: center;
}
.yh-steps.is-simple .yh-step .yh-step__head {
  display: none;
}
.yh-steps.is-simple .yh-step .yh-step__line {
  position: relative;
  top: auto;
  left: auto;
  width: 24px;
  height: 1px;
  margin: 0 12px;
  background: var(--yh-steps-line-color);
}
.yh-steps.is-simple .yh-step .yh-step__main {
  display: flex;
  align-items: center;
  gap: 8px;
}
.yh-steps.is-simple .yh-step .yh-step__title {
  font-size: var(--yh-font-size-base);
}
.yh-steps.is-simple .yh-step .yh-step__title::before {
  content: counter(step);
  counter-increment: step;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  margin-right: 8px;
  border-radius: 50%;
  font-size: 12px;
  background: var(--yh-steps-wait-color);
  color: #fff;
}
.yh-steps.is-simple .yh-step.is-process .yh-step__title::before {
  background: var(--yh-steps-process-color);
}
.yh-steps.is-simple .yh-step.is-finish .yh-step__title::before, .yh-steps.is-simple .yh-step.is-success .yh-step__title::before {
  background: var(--yh-steps-finish-color);
}
.yh-steps.is-simple .yh-step.is-error .yh-step__title::before {
  background: var(--yh-steps-error-color);
}
.yh-steps.is-simple {
  counter-reset: step;
}
.yh-steps.is-dot {
  --yh-steps-icon-size: 10px;
}
.yh-steps.is-dot .yh-step .yh-step__head {
  width: var(--yh-steps-icon-size);
  height: var(--yh-steps-icon-size);
  min-width: var(--yh-steps-icon-size);
  border-radius: 50%;
  background: var(--yh-steps-wait-color);
  border: none;
}
.yh-steps.is-dot .yh-step .yh-step__head.is-process {
  background: var(--yh-steps-process-color);
  box-shadow: 0 0 0 4px rgba(var(--yh-color-primary-rgb), 0.2);
}
.yh-steps.is-dot .yh-step .yh-step__head.is-finish, .yh-steps.is-dot .yh-step .yh-step__head.is-success {
  background: var(--yh-steps-finish-color);
}
.yh-steps.is-dot .yh-step .yh-step__head.is-error {
  background: var(--yh-steps-error-color);
}
.yh-steps.is-dot .yh-step .yh-step__number {
  display: none;
}
.yh-steps.is-dot .yh-step .yh-step__icon {
  display: none;
}
.yh-steps.is-dot .yh-step .yh-step__icon-inner {
  display: none;
}
.yh-steps.is-dot .yh-step .yh-step__line {
  top: calc(var(--yh-steps-icon-size) / 2);
  left: calc(var(--yh-steps-icon-size) + 8px);
}
.yh-steps.is-navigation {
  background: var(--yh-fill-color-light);
  border-radius: var(--yh-radius-md);
  padding: 0;
}
.yh-steps.is-navigation .yh-step {
  flex: 1;
  padding: 16px 24px;
  cursor: pointer;
  position: relative;
  transition: background var(--yh-duration-fast);
}
.yh-steps.is-navigation .yh-step::after {
  content: "";
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%) rotate(45deg);
  width: 10px;
  height: 10px;
  border-top: 2px solid var(--yh-steps-line-color);
  border-right: 2px solid var(--yh-steps-line-color);
}
.yh-steps.is-navigation .yh-step.is-last::after {
  display: none;
}
.yh-steps.is-navigation .yh-step:hover {
  background: var(--yh-fill-color);
}
.yh-steps.is-navigation .yh-step.is-process {
  background: var(--yh-color-primary-light-9);
}
.yh-steps.is-navigation .yh-step .yh-step__head {
  display: none;
}
.yh-steps.is-navigation .yh-step .yh-step__line {
  display: none;
}
.yh-steps.is-navigation .yh-step .yh-step__main {
  padding-left: 0;
}
.yh-steps.is-navigation .yh-step .yh-step__title {
  font-weight: 500;
}
.yh-steps.is-clickable .yh-step:not(.is-disabled) {
  cursor: pointer;
}
.yh-steps.is-clickable .yh-step:not(.is-disabled):hover .yh-step__title {
  color: var(--yh-steps-process-color);
}
.yh-steps.is-progress .yh-step__line {
  background: var(--yh-steps-line-color);
  overflow: hidden;
  border-radius: 1px;
}
.yh-steps.is-progress .yh-step__line-inner {
  height: 100%;
  background: linear-gradient(90deg, var(--yh-steps-finish-color), var(--yh-color-primary-light-3));
  transition: width 0.3s ease;
}
.yh-steps.is-timeline.yh-steps--vertical .yh-step {
  padding-left: 100px;
  position: relative;
}
.yh-steps.is-timeline.yh-steps--vertical .yh-step .yh-step__time {
  position: absolute;
  left: 0;
  top: 0;
  width: 80px;
  text-align: right;
  font-size: var(--yh-font-size-sm);
  color: var(--yh-text-color-secondary);
}
.yh-steps.is-timeline.yh-steps--vertical .yh-step .yh-step__line {
  left: calc(100px + var(--yh-steps-icon-size) / 2);
}
.yh-steps.is-timeline.yh-steps--vertical .yh-step .yh-step__head {
  position: relative;
}
.yh-steps.is-timeline.yh-steps--horizontal .yh-step__time {
  font-size: 12px;
  color: var(--yh-text-color-secondary);
  margin-top: 4px;
}

.yh-step {
  position: relative;
  flex: 1;
  display: flex;
  flex-shrink: 0;
  flex-direction: column;
  padding-right: 16px;
}
.yh-step:last-child {
  flex: none;
  padding-right: 0;
}
.yh-step.is-disabled {
  cursor: not-allowed !important;
  opacity: 0.6;
}
.yh-step.is-disabled .yh-step__head,
.yh-step.is-disabled .yh-step__title,
.yh-step.is-disabled .yh-step__description {
  color: var(--yh-text-color-disabled) !important;
  border-color: var(--yh-border-color-lighter) !important;
}
.yh-step.is-clickable {
  cursor: pointer;
}
.yh-step.is-clickable:hover .yh-step__head {
  border-color: var(--yh-steps-process-color);
}
.yh-step.is-timeline {
  padding-left: 0;
}
.yh-step__head {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--yh-steps-icon-size);
  height: var(--yh-steps-icon-size);
  min-width: var(--yh-steps-icon-size);
  border-radius: 50%;
  border: 2px solid var(--yh-steps-wait-color);
  color: var(--yh-steps-wait-color);
  font-size: 14px;
  font-weight: 500;
  transition: all var(--yh-duration-fast);
  background: var(--yh-bg-color);
  z-index: 1;
}
.yh-step__head.is-process {
  border-color: var(--yh-steps-process-color);
  color: var(--yh-steps-process-color);
  background: var(--yh-color-primary-light-9);
}
.yh-step__head.is-finish {
  border-color: var(--yh-steps-finish-color);
  color: var(--yh-steps-finish-color);
}
.yh-step__head.is-success {
  border-color: var(--yh-steps-finish-color);
  color: var(--yh-steps-finish-color);
}
.yh-step__head.is-error {
  border-color: var(--yh-steps-error-color);
  color: var(--yh-steps-error-color);
}

.yh-step__icon {
  font-size: 16px;
  line-height: 1;
}

.yh-step__icon-inner {
  fill: currentColor;
}

.yh-step__number {
  font-size: 14px;
  font-weight: 500;
  line-height: 1;
}

.yh-step__line {
  position: absolute;
  top: calc(var(--yh-steps-icon-size) / 2);
  left: calc(var(--yh-steps-icon-size) + 10px);
  right: 10px;
  height: 1px;
  background: var(--yh-steps-line-color);
}

.yh-step__line-inner {
  display: block;
  width: 0;
  height: 100%;
  background: var(--yh-steps-finish-color);
  transition: width var(--yh-duration-base);
}

.yh-step.is-finish .yh-step__line-inner, .yh-step.is-success .yh-step__line-inner {
  width: 100%;
}
.yh-step__main {
  padding-top: 10px;
  white-space: normal;
}

.yh-step__title {
  font-size: var(--yh-font-size-base);
  font-weight: 500;
  color: var(--yh-text-color-primary);
  line-height: 1.5;
  transition: color var(--yh-duration-fast);
}
.yh-step__title.is-wait {
  color: var(--yh-text-color-secondary);
}
.yh-step__title.is-process {
  color: var(--yh-steps-process-color);
}
.yh-step__title.is-finish {
  color: var(--yh-text-color-primary);
}
.yh-step__title.is-success {
  color: var(--yh-text-color-primary);
}
.yh-step__title.is-error {
  color: var(--yh-steps-error-color);
}

.yh-step__description {
  font-size: var(--yh-font-size-sm);
  color: var(--yh-text-color-secondary);
  line-height: 1.5;
  margin-top: 4px;
}

.yh-step__time {
  font-size: var(--yh-font-size-sm);
  color: var(--yh-text-color-secondary);
}

.yh-step__content {
  margin-top: 12px;
  padding: 16px;
  background: var(--yh-fill-color-light);
  border-radius: var(--yh-radius-sm);
}
</style>
