import { QueryClient } from '@kevisual/query';
import { QueryLoginBrowser } from '@kevisual/query-login';
import { toastLogin } from '@kevisual/resources/pages/message/ToastLogin.tsx';
import { toast } from 'react-toastify';
export const query = new QueryClient({
  io: true,
});
/**
 * 登录查询
 */
export const queryLogin = new QueryLoginBrowser({
  query: query as any,
});
// @ts-ignore
if (window.context) {
  // @ts-ignore
  window.context.queryLogin = queryLogin;
} else {
  // @ts-ignore
  window.context = {
    queryLogin,
  };
}
query.afterResponse = async (res, ctx) => {
  const newRes = await queryLogin.run401Action(res, ctx, {
    afterAlso401: () => {
      toastLogin();
    },
    afterCheck: (res) => {
      if (res.code === 200) {
        // console.log('afterCheck');
        toast.success('刷新登陆信息');
        setTimeout(() => {
          window.location.reload();
        }, 1000);
      }
    },
  });

  return newRes as any;
};
export const request = query.post;

export const ws = query.qws?.ws;

query.qws.listenConnect(() => {
  console.log('Connected to WebSocket server');
});
