<script setup lang="ts">
import { onMounted, onUnmounted, provide, ref } from 'vue'
import { KkTag } from 'kankan-components'

const tags = ref<any>([])

onMounted(() => {
  const __win = window as any
  if (!__win.__sdk) {
    const __sdk = (__win.__sdk = new __win.KanKan({
      num: 'KJ-t-wOXfx2SDFy',
      server: '#DEMOSEVER#',
    }))
    provide('__sdk', __sdk)

    __sdk.TagManager.on('loaded', (data: any) =>
      __sdk.TagManager.load((tags.value = data) && tags.value)
    )
    __sdk.mount('#scene').render()
  }
})
onUnmounted(() => {
  const __win = window as any
  if (__win.__sdk) {
    __win.__sdk = null
  }
})
const handleTagview = ({ id }) => {
  console.log('id', id)
}
</script>

<template>
  <div id="scene" class="scene">
    <Teleport v-if="tags.length > 0" to=".kankan-plugins">
      <div xui_tags_view>
        <kk-tag
          v-for="(item, index) in tags"
          :key="index"
          :tag="item"
          @click="handleTagview"
        />
      </div>
    </Teleport>
  </div>
</template>
<style>
html,
body,
#app {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}
.scene {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
}
</style>
