import DefaultAgent from "@/components/AgentPage";
import InitView from "./components/InitView";
import { useDidShow } from "@tarojs/taro";
import { useAgentStore, useAgentStoreActions } from "@/store/agentStore";
import { useIsLogin } from "@/xiaolanbenlib/hooks/data/useAuth";
import { useEffect } from "react";

export default function Index() {
  const isLogin = useIsLogin();

  const defaultAgent = useAgentStore((state) => state.defaultAgent);
  const { fetchAgents, resetData } = useAgentStoreActions();

  useDidShow(() => {
    // 如果没有登录过，则显示默认欢迎页
    // 如果有登录状态，主动获取智能体列表
    if (isLogin) {
      fetchAgents();
    }
  });

  // 如果登出，则重置当前 agent store
  useEffect(()=> {
    if(!isLogin){
      resetData()
    }
  }, [isLogin])
  if (!defaultAgent) {
    return <InitView></InitView>;
  }

  if (defaultAgent?.agentId) {
    return <DefaultAgent agentId={defaultAgent.agentId}></DefaultAgent>;
  }
}
