<script setup>
import { computed, provide, ref, onMounted, onUnmounted } from "vue";
import { useNamespace } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
import { stepsProps, stepsEmits, STEPS_INJECTION_KEY } from "./steps";
defineOptions({
  name: "YhSteps"
});
const props = defineProps(stepsProps);
const emit = defineEmits(stepsEmits);
const ns = useNamespace("steps");
const { themeStyle } = useComponentTheme(
  "steps",
  computed(() => props.themeOverrides)
);
const steps = ref([]);
const isResponsiveVertical = ref(false);
const checkResponsive = () => {
  if (typeof window === "undefined") return;
  if (props.responsive) {
    isResponsiveVertical.value = window.innerWidth < props.responsiveBreakpoint;
  } else {
    isResponsiveVertical.value = false;
  }
};
onMounted(() => {
  if (props.responsive && typeof window !== "undefined") {
    checkResponsive();
    window.addEventListener("resize", checkResponsive);
  }
});
onUnmounted(() => {
  if (props.responsive && typeof window !== "undefined") {
    window.removeEventListener("resize", checkResponsive);
  }
});
const registerStep = (step) => {
  const index = steps.value.findIndex((s) => s.uid === step.uid);
  if (index === -1) {
    steps.value.push(step);
  } else {
    steps.value[index] = step;
  }
  steps.value.sort((a, b) => a.uid - b.uid);
};
const unregisterStep = (uid) => {
  const index = steps.value.findIndex((s) => s.uid === uid);
  if (index > -1) {
    steps.value.splice(index, 1);
  }
};
const handleStepClick = (index, disabled) => {
  if (!props.clickable || disabled) return;
  emit("change", index);
};
provide(STEPS_INJECTION_KEY, {
  props,
  steps,
  isResponsiveVertical,
  registerStep,
  unregisterStep,
  handleStepClick
});
const actualDirection = computed(() => {
  if (isResponsiveVertical.value) return "vertical";
  return props.direction;
});
const stepsClass = computed(() => [
  ns.b(),
  ns.m(actualDirection.value),
  ns.m(`size-${props.size}`),
  ns.m(`label-${props.labelPlacement}`),
  ns.is("simple", props.simple),
  ns.is("center", props.alignCenter),
  ns.is("dot", props.progressDot === true || props.progressDot === "dot"),
  ns.is("navigation", props.progressDot === "navigation"),
  ns.is("clickable", props.clickable),
  ns.is("progress", props.showProgress),
  ns.is("timeline", props.showTimeline),
  ns.is("responsive", props.responsive)
]);
</script>

<template>
  <div :class="stepsClass" :style="themeStyle">
    <slot></slot>
  </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>
