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

interface Props {
  components: PageDataTemplateComponents
  pageVerTmplSeq: number
}

const props = defineProps<Props>()

const backgroundData = computed(() =>
  getComponentGroup(props.components, 'background')
)
const mainTitleData = computed(() =>
  getComponentGroup(props.components, 'mainTitle')
)
const slideData = computed(() => {
  return getComponentContainer(props.components, 'group_sets', { minLength: 4 })
})

const buttonListData = ref(
  getComponentGroupAry(slideData?.value[0], 'buttonList')
)

const slideItemSize = {
  mo: {
    width: 295,
    height: 384,
    gap: 12,
    scale: 1,
  },
  pc: {
    width: 419,
    height: 564,
    gap: 20,
    scale: 1.0979,
  },
}

const handleSplideMove = (_splide: SplideType, newIndex: number) => {
  buttonListData.value = getComponentGroupAry(
    slideData.value[newIndex],
    'buttonList'
  )
}
const { locale } = useI18n()
const { sendLog, useAnalyticsLogDataDirect } = useAnalytics()
const onArrowClick = (direction, targetIndex) => {
  const arrowGroupAry = getComponentGroupAry(props.components, 'arrow')
  const logTracking = arrowGroupAry?.[direction === 'prev' ? 0 : 1]
  sendLog(locale.value, useAnalyticsLogDataDirect(logTracking, 1))
}
</script>

<template>
  <section class="section-container">
    <WidgetsBackground v-if="backgroundData" :resources-data="backgroundData" />
    <div class="section-content px-0">
      <WidgetsMainTitle
        v-if="mainTitleData"
        :resources-data="mainTitleData"
        class="title-md mx-[20px] sm:mx-[40px]"
      />
      <BlocksSlideCenterFocus
        v-if="slideData"
        :slide-item-size="slideItemSize"
        :slide-item-length="slideData?.length"
        :pagination="false"
        class="mt-[24px] md:mt-[48px]"
        @move="handleSplideMove"
        @arrow-click="onArrowClick"
      >
        <SplideSlide v-for="(item, index) in slideData" :key="index">
          <div class="slide-inner border-line mt-auto">
            <BlocksVisualContent
              :resources-data="getComponentGroup(item, 'imgList')"
              :page-ver-tmpl-seq="props.pageVerTmplSeq"
              object-fit="cover"
              :alt="getComponentGroup(item, 'subTitle')?.display?.text"
            />
          </div>
        </SplideSlide>
      </BlocksSlideCenterFocus>
      <WidgetsButtonList
        v-if="buttonListData"
        :resources-data="buttonListData"
        :page-ver-tmpl-seq="props.pageVerTmplSeq"
        class="mt-[40px] md:mt-[56px]"
      />
    </div>
  </section>
</template>

<style scoped>
.section-container {
  @apply before:hidden md:before:block before:content-[''] before:absolute before:top-0 before:left-0 before:w-[104px] before:h-full before:bg-gradient-to-l from-transparent to-[rgba(0,0,0,0.7)] 
  after:hidden md:after:block after:content-[''] after:absolute after:top-0 after:right-0 after:w-[104px] after:h-full after:bg-gradient-to-r from-transparent to-[rgba(0,0,0,0.7)];
}
</style>
