import { useLaunch } from '@tarojs/taro';

import { PropsWithChildren } from 'react';
import { Provider } from 'react-redux';

import { REFRESH_UNREAD_COUNT_TIME } from '@/constants/message';
import http from '@/http';
import store from '@/store';
import { requestUnreadMessageCount } from '@/utils/message';
import { getInviteCode, getInviteCodeFromQuery } from '@/utils/partner';
import qiniuUpload from '@/utils/qiniu-upload';
import { requestUserInfo, updateLastLoginTime } from '@/utils/user';

import './app.less';

function App({ children }: PropsWithChildren<BL.Anything>) {
  useLaunch(async ({ query }) => {
    console.log('App launched.');
    await http.init(getInviteCodeFromQuery(query));
    requestUserInfo().then(userInfo => {
      if (userInfo.isPartner) {
        getInviteCode();
      }
    });
    updateLastLoginTime();
    qiniuUpload.init();
    requestUnreadMessageCount();
    setInterval(() => requestUnreadMessageCount(), REFRESH_UNREAD_COUNT_TIME);
  });

  return <Provider store={store}>{children}</Provider>;
}

export default App;
