import Taro, { onAppShow } from '@tarojs/taro'
import type { FC } from 'react'
import { Provider } from 'react-redux'
import configStore from './store'
import { shareShop } from './common/util'
import ContextBlueTooth from '@/use/contextBlueTooth'
import './app.scss'

interface ParamsType {
  children?: React.ReactNode
}

const store = configStore()
const App: FC<ParamsType> = (params) => {
  const { children } = params
  if (process.env.TARO_ENV === 'weapp') {
    Taro.showShareMenu({
      withShareTicket: true,
    })

    // 分享
    shareShop()

    // 检查版本更新
    onAppShow(() => {
      const updateManager = Taro.getUpdateManager()
      updateManager.onCheckForUpdate((res) => {
        // 请求完新版本信息的回调
        console.log('版本信息:', res.hasUpdate)
      })
      updateManager.onUpdateReady(() => {
        Taro.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success(res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          },
        })
      })
      updateManager.onUpdateFailed(() => {
        console.log('新版本更新失败')
      })
    })
  }

  return (
    <ContextBlueTooth>
      <Provider store={store}>{children}</Provider>
    </ContextBlueTooth>
  )
}

export default App
