<script setup lang="ts">
import { SplideSlide } from '@splidejs/vue-splide'
import {
  getComponentContainer,
  getComponentGroupAry,
  getComponentGroup,
  hasComponentGroup,
} from '#layers/utils/dataUtil'
import type { PageDataTemplateComponents } from '#layers/types/api/pageData'

interface Props {
  components: PageDataTemplateComponents
  pageVerTmplSeq: number
}

const props = defineProps<Props>()

const { locale } = useI18n()
const { sendLog, useAnalyticsLogDataDirect } = useAnalytics()

const currentSlideIndex = ref<number | null>(null)

const slideData = computed(() => {
  return getComponentContainer(props.components, 'group_sets')
})
const paginationData = computed(() => {
  return getComponentGroupAry(props.components, 'pagination')
})

const onArrowClick = direction => {
  const arrowGroupAry = getComponentGroupAry(props.components, 'arrow')
  const logTracking = arrowGroupAry?.[direction === 'prev' ? 0 : 1]
  sendLog(locale.value, useAnalyticsLogDataDirect(logTracking, 1))
}
</script>

<template>
  <section class="section-standard">
    <BlocksSlideFade
      v-if="slideData"
      v-model:index="currentSlideIndex"
      :arrows="slideData.length > 1"
      :pagination="slideData.length > 1"
      class="h-full"
      :pagination-data="paginationData"
      @arrow-click="onArrowClick"
    >
      <SplideSlide v-for="(item, index) in slideData" :key="index">
        <WidgetsBackground
          v-if="hasComponentGroup(item, 'background')"
          :resources-data="getComponentGroup(item, 'background')"
          :video-play="currentSlideIndex === index"
        />
        <div class="content-standard gap-3 md:gap-5">
          <WidgetsSubTitle
            v-if="hasComponentGroup(item, 'subTitle')"
            :resources-data="getComponentGroup(item, 'subTitle')"
            class="title-md max-w-[944px]"
          />
          <WidgetsMainTitle
            v-if="hasComponentGroup(item, 'mainTitle')"
            :resources-data="getComponentGroup(item, 'mainTitle')"
            class="title-xlg max-w-[944px]"
          />
          <WidgetsDescription
            v-if="hasComponentGroup(item, 'description')"
            :resources-data="getComponentGroup(item, 'description')"
            class="max-w-[944px]"
          />
          <WidgetsButtonList
            v-if="hasComponentGroup(item, 'buttonList')"
            :resources-data="getComponentGroupAry(item, 'buttonList')"
            :page-ver-tmpl-seq="props.pageVerTmplSeq"
            class="mt-[28px] md:mt-[52px]"
          />
        </div>
      </SplideSlide>
    </BlocksSlideFade>
  </section>
</template>
