import { tryLimit } from "@/utils/tryLimit";

/** 本函数确保无需catch
 *  @returns true: 限流。 false: 不限 */
export async function limitLogin(): Promise<boolean> {
  const { limitResult, serverOk } = await tryLimit({
    action: "login",
    limit: 500,
  });
  logger({
    cnl: ["server"],
    pgn: "登录页面",
    act: "login clicked",
    key: "登录限流API call",
  });
  if (!limitResult) {
    logger({
      cnl: ["server"],
      pgn: "登录页面",
      act: "登录被限流",
      key: "登录限流API call",
      ext: { serverOk },
    });
    $message.warning("网络繁忙，请稍后再试。");
    await new Promise((resolve) => setTimeout(resolve, 3000));
    return true;
  } else {
    logger({
      cnl: ["server"],
      pgn: "登录页面",
      key: "登录限流API call",
      act: "登录未限流",
    });
    return false;
  }
}
