<template>
  <div
    class="ui-gate-layer"
    :style="{
      height: height ? normalizeUnitToStyle(height) : 'auto',
      '--len': contentInstances.length,
      '--current': slideIndex,
    }"
  >
    <div class="ui-gate-slides">
      <slot />
    </div>
  </div>
</template>

<script lang="ts" setup>
// watchEffect
import { computed, defineProps, provide, ref, watch } from 'vue'
import { normalizeUnitToStyle } from '@kankan-components/utils'
import { Relation } from './constant'
import { gateProps } from './gate'

defineOptions({
  name: 'UIGate',
})

const contentInstances = ref([])
const props = defineProps(gateProps)

const slideIndex = computed(() =>
  props.index > contentInstances.value.length - 1
    ? contentInstances.value.length - 1
    : props.index < 0
    ? 0
    : props.index
)

watch([contentInstances, slideIndex], () => {
  for (let i = 0; i < contentInstances.value.length; i++) {
    const instance = contentInstances.value[i]
    instance.value = i === slideIndex.value
  }
})

provide(Relation, contentInstances)
</script>
