import Taro, { useDidShow } from "@tarojs/taro";
import React, { useEffect, useState, forwardRef, useImperativeHandle } from "react";
import LoginPopup from '@/components/LoginPopup'
import { useIsLogin } from '@/xiaolanbenlib/hooks/data/useAuth'

interface IProps {
  onEnd: ()=>void
}

const CheckLoginPopup = forwardRef(({ onEnd }: IProps, ref) => {
  const [showLogin, setShowLogin] = useState(false);
  const isLogin = useIsLogin()

  useImperativeHandle(ref, () => ({
    setShowLogin,
  }));

  const onLoginEnd = ()=> {
    onEnd()
  }

  const checkLogin = () => {
    if(!isLogin){
      setShowLogin(true)
    }else{
      setShowLogin(false)
    }
  }
  useDidShow(()=> {
    checkLogin()
    console.log('LoginPopup use did show')
  })
  useEffect(()=> {
    checkLogin()
  }, [isLogin])

  return  <LoginPopup showPopup={showLogin} setShowPopup={setShowLogin} onEnd={onLoginEnd}></LoginPopup>
});

export default CheckLoginPopup;
