import { PropsWithChildren } from 'react'
import Taro, { useLaunch } from '@tarojs/taro'
import { Provider } from 'react-redux'
import configStore from './store'

import './app.css'
import { useServerSdk } from './hooks'

const store = configStore()

function App({ children }: PropsWithChildren<any>) {
  const serverSdk = useServerSdk()
  useLaunch(() => {
    Taro.login({
      success: async (res) => {
        try {
          const login = await serverSdk.login({ ...res })
          // 获取accessToken然后存储到本地
          if (login?.tokens?.accessToken) {
            serverSdk.setAccessToken(login.tokens.accessToken, login.user.id)
          }
        } catch (error) {
          console.error('Login failed:', error)
        }
      },
      fail: (error) => {
        console.error('Taro.login failed:', error)
      }
    })
    console.log('App launched.')
  })

  // children 是将要会渲染的页面
  return (
    <Provider store={store}>
      {children}
    </Provider>
  )
}



export default App
