import { createVNode, render } from 'vue'
import RewardDia from '@c/RewardDia/index.vue'
import { useSound } from './useSound'

interface Tips_Config {
  rewardList?: anyObj[]
  onOk?: (...args: any[]) => void
}

const OSS_URL = import.meta.env.VITE_OSS_URL

export const useReward = (config: Tips_Config) => {
  const container = document.createElement('div')
  const dia = createVNode(RewardDia, {
    modelValue: true,
    rewardList: config.rewardList,
    ...config,
    onOk: async () => {
      config.onOk && (await config.onOk())
      close()
    },
    onCancel: () => {
      close()
    },
  })

  const close = () => {
    dia.component!.props.modelValue = false
    setTimeout(() => {
      render(null, container)
    }, 300)
  }

  render(dia, container)
  document.querySelector('body')?.appendChild(container.firstElementChild!)
  // 音效
  useSound(`${OSS_URL}/sound/receive_reward.mp3`)
}
