import { useCallback } from 'react';
import { showMessage } from '@common/ToastHelper.ts';
import { useAuth } from '@common/contexts/useAuth.ts';
import { useCommon } from '@common/contexts/useCommon.ts';
import { RequiredUserInfo } from '@common/contexts/AuthContext.tsx';

/**
 * @description: 处理登录的 hook，可以合并处理登录时的一些操作
 */
const useLogin = () => {
  const {
    actions: { login },
  } = useAuth();

  const {
    actions: { save },
  } = useCommon();

  return useCallback(
    (token: string, userInfo: RequiredUserInfo) => {
      login(token, userInfo).catch(e => {
        showMessage(e.message);
      });
      // 如果手机号不是上次登录的手机号，则清除本地证书
      // if (userInfo.phone !== info.lastLoginPhoneNumber) {
      //   clearCert();
      save('lastLoginPhoneNumber', userInfo.phone).catch(e => {
        showMessage(e.message);
      });
      // }
    },
    [login, save],
  );
};

export { useLogin };
