<script setup lang="ts">
import { SplideSlide } from '@splidejs/vue-splide'
import { getComponentGroup, getComponentGroupAry } from '#layers/utils/dataUtil'

interface Props {
  components: Record<string, any>
  pageVerTmplSeq: string
}

const props = defineProps<Props>()

const pageDataStore = usePageDataStore()
const { getResourcesData } = useResourcesData()

const { pageData } = storeToRefs(pageDataStore)

const backgroundData = computed(() =>
  getComponentGroup(props.components, 'background')
)
const mainTitleData = computed(() =>
  getComponentGroup(props.components, 'mainTitle')
)
const descriptionData = computed(() =>
  getComponentGroup(props.components, 'description')
)
const videoPlayData = computed(() =>
  getComponentGroup(props.components, 'videoPlay')
)
const buttonListData = computed(() =>
  getComponentGroupAry(props.components, 'buttonList')
)

// 비동기 데이터 로딩
const { data: resourcesData } = await useLazyAsyncData(
  'gr-visual-02-resources',
  async () => {
    if (!pageData.value?.page_seq || !pageData.value?.page_ver) {
      return null
    }

    return await getResourcesData({
      pageSeq: pageData.value.page_seq,
      pageVer: pageData.value.page_ver,
      pageVerTmplSeq: props.pageVerTmplSeq,
      langCode: 'ko',
    })
  }
)

const slideData = computed(() => {
  const operateComponents = resourcesData.value?.operate_components

  if (!operateComponents) {
    return []
  }

  const firstKey = Object.keys(operateComponents)[0]
  const data = operateComponents[firstKey]?.list_operate_groups || []

  if (data.length < 3) {
    return [...data, ...data]
  }

  return data
})

const slideItemSize = {
  mo: {
    width: 276,
    height: 174,
    gap: 12,
  },
  pc: {
    width: 455,
    height: 287,
    gap: 32,
  },
}
</script>

<template>
  <section class="relative pt-[140px] pb-[80px] md:pt-[200px] md:pb-[120px]">
    <WidgetsBackground
      v-if="backgroundData"
      :resources-data="backgroundData"
      gradient="h-[440px] bg-[linear-gradient(180deg,rgba(16,13,15,0)_0%,#100D0F_40%)] md:h-[720px] md:bg-[linear-gradient(180deg,rgba(16,13,15,0)_0%,#100D0F_50%)]"
    />
    <div class="section-content px-0 gap-5">
      <WidgetsMainTitle
        v-if="mainTitleData"
        :resources-data="mainTitleData"
        class="w-[355px] md:w-[944px]"
      />
      <WidgetsDescription
        v-if="descriptionData"
        :resources-data="descriptionData"
        class="w-[355px] md:w-[944px]"
      />
      <WidgetsVideoPlay
        v-if="videoPlayData"
        :resources-data="videoPlayData"
        :page-ver-tmpl-seq="Number(props.pageVerTmplSeq)"
      />
      <WidgetsButtonList
        v-if="buttonListData.length > 0"
        :resources-data="buttonListData"
        :page-ver-tmpl-seq="Number(props.pageVerTmplSeq)"
        class="mt-[48px] md:mt-[72px]"
      />
      <BlocksSlideCenterHighlight
        v-if="slideData"
        :slide-item-size="slideItemSize"
        :slide-item-length="slideData?.length"
        :pagination="false"
        class="mt-[36px] md:mt-[60px]"
      >
        <SplideSlide v-for="(item, index) in slideData" :key="index">
          <BlocksCardNews
            :title="item.title"
            :description="formatTimestamp(item.reg_dt, 'YYYY.MM.DD')"
            :img-path="getResolvedHost(item.img_path)"
            :url="item.url"
            :link-target="item.link_target"
            class="slide-inner"
          />
        </SplideSlide>
      </BlocksSlideCenterHighlight>
    </div>
  </section>
</template>
