<script setup>
import { ref, watch, onMounted, computed, onBeforeUnmount } from "vue";
import { useNamespace } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
import { skeletonProps } from "./skeleton";
import YhSkeletonItem from "./skeleton-item.vue";
defineOptions({
  name: "YhSkeleton"
});
const props = defineProps(skeletonProps);
const ns = useNamespace("skeleton");
const { themeStyle } = useComponentTheme(
  "skeleton",
  computed(() => props.themeOverrides)
);
const uiLoading = ref(props.loading && props.throttle <= 0);
let timeout = null;
watch(
  () => props.loading,
  (val) => {
    if (timeout) clearTimeout(timeout);
    if (val) {
      if (props.throttle <= 0) {
        uiLoading.value = true;
      } else {
        timeout = setTimeout(() => {
          uiLoading.value = true;
        }, props.throttle);
      }
    } else {
      uiLoading.value = false;
    }
  },
  { immediate: true }
);
const containerClass = computed(() => [ns.b(), ns.is("animated", props.animated)]);
const isIntersecting = ref(false);
const skeletonRef = ref(null);
let observer = null;
onMounted(() => {
  if (props.lazy && skeletonRef.value) {
    observer = new IntersectionObserver(
      (entries) => {
        isIntersecting.value = entries[0].isIntersecting;
      },
      { threshold: 0.1 }
    );
    observer.observe(skeletonRef.value);
  } else {
    isIntersecting.value = true;
  }
});
onBeforeUnmount(() => {
  if (observer) {
    observer.disconnect();
    observer = null;
  }
  if (timeout) {
    clearTimeout(timeout);
  }
});
</script>

<template>
  <div v-if="uiLoading" ref="skeletonRef" :class="containerClass" :style="themeStyle">
    <slot name="template">
      <!-- 默认布局：Avatar + Title + Rows -->
      <div v-if="avatar" :class="ns.e('header')">
        <yh-skeleton-item
          variant="circle"
          :width="48"
          :height="48"
          :animated="isIntersecting && animated"
        />
      </div>
      <div :class="ns.e('content')">
        <yh-skeleton-item
          v-if="title"
          variant="h3"
          style="width: 40%; margin-bottom: 16px"
          :animated="isIntersecting && animated"
        />
        <yh-skeleton-item
          v-for="i in rows"
          :key="i"
          variant="text"
          :style="{
  width: i === rows && rows > 1 ? '60%' : '100%',
  marginBottom: i === rows ? '0' : '12px'
}"
          :animated="isIntersecting && animated"
        />
      </div>
    </slot>
  </div>
  <slot v-else />
</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>
