/**
 * 与app进行交互的hook
 */

import { useUserStore } from "@/store"

export const useAppBridge = () => {

  const _reload = (path: string) => window.reloadAppPage.postMessage(JSON.stringify({ route: path }))

  const _push = (path: string) => window.navAppPage.postMessage(JSON.stringify({ route: path }))

  const _back = (path: string = '') => window.popPage.postMessage(path)
  
  const _getToken = () => {
    window.returnToken = token => useUserStore().token = token
    try {
      window.getToken.postMessage(JSON.stringify({}))
    } catch (error) {
      
    }
    
  }

  return {
    reload: _reload,
    push: _push,
    back: _back,
    getToken: _getToken
  }

}

