import { LoginParams } from '@/entities/user/model/types';
import { StorageKeys } from '@/shared/constants/local-storage';
import { setLocalStorage } from '@/shared/lib/web-view-bridge';
import { useLoginMutation } from '@/entities/user/api/use-login-mutation';
import { useStore } from '~/shared/model/store';

export const useUserInfo = () => {
  const { mutateAsync: login } = useLoginMutation();
	const callLogin = async (params: LoginParams) => {
		try{
			useStore.getState().UserStore.resetUserInfo();
			
			const result = await login(params);
			if(result.requires2FA){
				// 2차인증 필요
				
			}
			else{
				const { 
					tokenType,
					accessToken,
					refreshToken,
					accessTokenExpiresIn,
					refreshTokenExpiresIn,
					menuGrants,
					// usrid,
					// clientAddressIP,
					// requires2FA
				} = result;
				setLocalStorage(StorageKeys.TokenType, tokenType);
				setLocalStorage(StorageKeys.AccessToken, accessToken);
				setLocalStorage(StorageKeys.RefreshToken, refreshToken);
				setLocalStorage(StorageKeys.AccessTokenExpiresIn, accessTokenExpiresIn);
				setLocalStorage(StorageKeys.RefreshTokenExpiresIn, refreshTokenExpiresIn);
				setLocalStorage(StorageKeys.MenuGrants, menuGrants);
				// setLocalStorage(StorageKeys.Usrid, usrid);
				// setLocalStorage(StorageKeys.ClientAddressIP, clientAddressIP);
				// setLocalStorage(StorageKeys.Requires2FA, requires2FA);
				console.log(result)
				useStore.getState().UserStore.setUserInfo(result);
			}
		}
		catch(e: any){
	
		}
	};
	return {
		callLogin,
	} 		
}