import { createSDKPolyfill } from '@hyext/utils'
import logger from './logger'

const polyfillSDK = createSDKPolyfill<{
  initGameEnv: typeof hyExt.exe.initGameEnv
  downloadBatchRes: typeof hyExt.fs.downloadBatchRes
  on: typeof hyExt.context.on
  off: typeof hyExt.context.off
  launchGame: typeof hyExt.exe.launchGame
  onGameMessage: typeof hyExt.exe.onGameMessage
  getExtInfo: typeof hyExt.env.getExtInfo
  commonQuery: typeof hyExt.backend.commonQuery
  getLiveInfo: typeof hyExt.context.getLiveInfo
  getStreamerInfo: typeof hyExt.context.getStreamerInfo
  shutdownGame: typeof hyExt.exe.shutdownGame
  localControlPanelVisible: typeof hyExt.action.localControlPanelVisible
  getUserInfo: typeof hyExt.context.getUserInfo
}>({
  SDK: global.hyExt,
  paths: [
    'exe.initGameEnv',
    'fs.downloadBatchRes',
    'context.on',
    'context.off',
    'exe.launchGame',
    'exe.onGameMessage',
    'env.getExtInfo',
    'backend.commonQuery',
    'context.getLiveInfo',
    'context.getStreamerInfo',
    'exe.shutdownGame',
    'action.localControlPanelVisible',
    'context.getUserInfo',
  ],
  onCall(callArgs, apiName, path) {
    logger.log(`SDK.${path}开始调用`, callArgs)
  },
  onError(errMsg, apiName, path) {
    logger.log(`SDK.${path}调用失败`, errMsg)
    if (apiName.includes('launchGame')) {
      return new Error(errMsg)
    }
  },
  onSuccess(res, apiName, path) {
    logger.log(`SDK.${path}调用成功`, res)
  },
})

export default polyfillSDK
