<script setup>
import { computed } from "vue";
import { useNamespace } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
import { skeletonItemProps } from "./skeleton";
defineOptions({
  name: "YhSkeletonItem"
});
const props = defineProps(skeletonItemProps);
const ns = useNamespace("skeleton-item");
const { themeStyle } = useComponentTheme(
  "skeleton-item",
  computed(() => props.themeOverrides)
);
const style = computed(() => {
  const s = {
    ...themeStyle.value
  };
  if (props.width) {
    s.width = typeof props.width === "number" ? `${props.width}px` : props.width;
  }
  if (props.height) {
    s.height = typeof props.height === "number" ? `${props.height}px` : props.height;
  }
  return s;
});
const classes = computed(() => [
  ns.b(),
  ns.m(props.variant),
  ns.is("animated", props.animated),
  ns.is("round", props.round),
  ns.is("sharp", props.sharp)
]);
</script>

<template>
  <div v-if="props.repeat > 1" style="display: contents">
    <div v-for="i in props.repeat" :key="i" :class="classes" :style="style">
      <slot v-if="variant === 'image'" name="image">
        <svg viewBox="0 0 1024 1024" width="24" height="24">
          <path
            fill="currentColor"
            d="M160 160v640h704V160H160zm704-64c35.3 0 64 28.7 64 64v640c0 35.3-28.7 64-64 64H160c-35.3 0-64-28.7-64-64V160c0-35.3 28.7-64 64-64h704zM320 320a64 64 0 1 0 0 128 64 64 0 0 0 0-128zM320 576a32 32 0 0 0-32 32v128h64v-64l128-128 160 160 128-128 96 96v-128h64v192c0 35.3-28.7 64-64 64H160c-35.3 0-64-28.7-64-64v-192h64v128l128-128 128 128 192-192 128 128v-64h-64z"
          />
        </svg>
      </slot>
    </div>
  </div>
  <div v-else :class="classes" :style="style">
    <slot v-if="variant === 'image'" name="image">
      <svg viewBox="0 0 1024 1024" width="24" height="24">
        <path
          fill="currentColor"
          d="M160 160v640h704V160H160zm704-64c35.3 0 64 28.7 64 64v640c0 35.3-28.7 64-64 64H160c-35.3 0-64-28.7-64-64V160c0-35.3 28.7-64 64-64h704zM320 320a64 64 0 1 0 0 128 64 64 0 0 0 0-128zM320 576a32 32 0 0 0-32 32v128h64v-64l128-128 160 160 128-128 96 96v-128h64v192c0 35.3-28.7 64-64 64H160c-35.3 0-64-28.7-64-64v-192h64v128l128-128 128 128 192-192 128 128v-64h-64z"
        />
      </svg>
    </slot>
  </div>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
.yh-skeleton {
  width: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
}
.yh-skeleton__header {
  margin-bottom: 20px;
}

.yh-skeleton__content {
  flex: 1;
}

.yh-skeleton-item {
  background: var(--yh-fill-color-dark);
  display: inline-block;
  vertical-align: middle;
  border-radius: var(--yh-radius-base);
  box-sizing: border-box;
  max-width: 100%;
  overflow: hidden !important;
  position: relative;
}
.yh-skeleton-item--circle {
  border-radius: 50% !important;
  overflow: hidden !important;
}

.yh-skeleton-item--rect {
  border-radius: 4px;
}

.yh-skeleton-item--h1 {
  height: 32px;
  margin-bottom: 24px;
}

.yh-skeleton-item--h3 {
  height: 24px;
  margin-bottom: 16px;
}

.yh-skeleton-item--text {
  height: 16px;
  border-radius: 8px;
  overflow: hidden !important;
}

.yh-skeleton-item--caption {
  height: 12px;
}

.yh-skeleton-item--button {
  height: 32px;
  width: 64px;
  border-radius: var(--yh-radius-base);
}

.yh-skeleton-item--image {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100px;
  height: 100px;
  color: var(--yh-text-color-placeholder);
  overflow: hidden !important;
}
.yh-skeleton-item--image svg {
  opacity: 0.5;
}

.yh-skeleton-item.is-round {
  border-radius: 999px;
  overflow: hidden !important;
}
.yh-skeleton-item.is-sharp {
  border-radius: 0;
}
.yh-skeleton-item {
  /* 核心闪烁动画：极致平滑的线性位移 */
}
.yh-skeleton-item.is-animated {
  background: linear-gradient(90deg, var(--yh-fill-color-dark) 25%, var(--yh-fill-color-light) 37%, var(--yh-fill-color-dark) 63%);
  background-size: 400% 100%;
  animation: yh-skeleton-loading 1.4s ease infinite;
}

@keyframes yh-skeleton-loading {
  0% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0 50%;
  }
}
/* YH-UI 自创高级：波浪模式 (可选) */
@keyframes yh-skeleton-wave {
  0% {
    opacity: 1;
  }
  50% {
    opacity: 0.4;
  }
  100% {
    opacity: 1;
  }
}
</style>
