<script setup lang="ts">
import type { GameDataValue } from '#layers/types/api/gameData'
let mountedInstance: any = null
const runtimeConfig = useRuntimeConfig()
const baseDomain = `${runtimeConfig.public.baseDomain}`
onMounted(() => {
  const gameDataStore = useGameDataStore()
  const gameData = gameDataStore.gameData as GameDataValue
  const langCodes = gameData?.lang_codes
  const defaultLangCode = gameData?.default_lang_code
  const stoveGnbData = gameData?.stove_gnb_json
  const designTheme = gameData?.design_theme

  const currentDomain =
    window.location.protocol + '//' + window.location.hostname
  if (typeof window !== 'undefined' && (window as any).StoveGnb) {
    const stoveGnbOptions = {
      logArea: currentDomain,
      useLanguageCodeFromPath: false,
      serviceTitle: {
        pc: '',
        mobile: '',
      },
      widget: {
        notification: stoveGnbData?.notify_icon_visible || true,
        stoveDownload: stoveGnbData?.stove_install_button_visible || true,
        languageSelect: false,
        themeSelect: false,
        stoveMenu: {
          active: false,
          mobile: true,
        },
      },
      global: {
        languageCoverages: langCodes,
        defaultSelectedLanguage: defaultLangCode || 'en',
      },
      loginMethod: {
        redirectCurrentPage: true,
      },
      mode: {
        theme: {
          support: designTheme === 1 ? ['light'] : ['dark'],
          default: designTheme === 1 ? 'light' : 'dark',
        },
        mini: true,
        layout: 'wide',
        fixed: false,
      },
    }
    mountedInstance = (window as any).StoveGnb.mount(
      '#stove-wrap',
      stoveGnbOptions
    )
  }
  if (mountedInstance) {
    //Stove GNB에서도 쿠키를 굽고 있어 소문자로 통일
    //LOCALE 쿠키를 가져온 후 소문자로 변경해서 다시 LOCALE 쿠키를 설정
    nextTick(() => {
      const localeCookie = useCookie('LOCALE', {
        domain: baseDomain,
        path: '/',
        maxAge: 60 * 60 * 24 * 365, // 1년 (초 단위)
      })
      localeCookie.value = localeCookie.value.toLowerCase()
    })
  }
})

onBeforeUnmount(() => {
  if (mountedInstance && typeof mountedInstance.destroy === 'function') {
    mountedInstance.destroy()
  }
  mountedInstance = null
})
</script>
<template>
  <div id="stove-wrap" class="relative h-[48px] z-[5]" />
</template>
