import { throttle } from "lodash-es";
import { message } from "ant-design-vue";
import { logout } from "@/api/loginPage";
import router, { routeLogout } from "@/router";

export const notifyInvalidTokenThrottled = throttle(
  () => {
    if (router.currentRoute.value.name === "Login") {
      return;
    }
    void message.error({
      content: "登录失效，请重新登录！",
      duration: 10,
    });
    setTimeout(() => {
      logout().finally(
        () =>
          void routeLogout({
            cause: "登录失效",
            redirectTo: router.currentRoute.value.fullPath,
          })
      );
    }, 1000);
    console.log("登录失效");
  },
  1000,
  { trailing: false }
);
