import {  useState,useMemo } from 'react';
import { createContainer } from "unstated-next"
import {useDelay} from '@/hooks';
import {logout as apiLogout} from '@/services';
const useApp=() =>{
  const [loading,setLoading] = useState(true);
  const [nav,setNavConfig] = useState({visible:true,syncDocumentTitle:false});
  const [user,setUser] = useState(null);

  const delay = useDelay();

  const isLogin=async ()=>{
    await delay.wait(2000);
    return user?true:false;
  }
  const login=async (code)=>{
    await delay.wait(2000);
    setUser({id:'1001',auth:['normal','admin']});
    return true;
  }
  const logout=async ()=>{
    apiLogout();
    setUser(null);
  }


  const setNav=(config)=>{
    setNavConfig({...nav,...config});
  }
  const action=useMemo(()=>{
    return {
      loading,
      setLoading,
      isLogin,
      login,
      nav,
      setNav,
      logout,
      setUser,
      user
    }
  },[loading,nav,setNav,user,setUser,isLogin]);

  return action
}

const App = createContainer(useApp);

export default App;