
/**
 * @description 点击调用app方法
 */

import { useTokenStore } from "@/store";

const push = (path: string) => {
  switch (path) {
    case 'navigateExercise':
      window.navigateExercise.postMessage(JSON.stringify({}))
      break;
    case 'navigateRecord':
      window.navigateRecord.postMessage(JSON.stringify({}))
      break;
    case 'reloadGame':
      window.reloadGame.postMessage(JSON.stringify({}))
      break;
  }
}

const back = (path = '') => {
  window.popPage.postMessage(path)
}


const getToken = () => {
  try {
    window.getToken.postMessage(JSON.stringify({}))
  } catch (error) {
    
  }

  return new Promise( (resolve) => {
    window.returnToken = token => {
      useTokenStore().set(token) 
      resolve(token)
    }
  })
}


const shareWx = (url: string) => {
  const prams = JSON.stringify({callname: url})
  window.sharePoster.postMessage(prams)
}

const savePoster = (url: string) => {
  const prams = JSON.stringify({callname: url})
  window.savePoster.postMessage(prams)
}

export const useAppRouter = () => {
  
  const router = {
    push: push,
    back: back,
    getToken
  }

  return router
}

export const useAppFunc = () => {

  return {
    shareWx,
    savePoster
  }

}