import {
  errorConfig,
  requestInterceptors,
  responseInterceptors,
} from '@/utils/request';
import type { RequestConfig } from '@umijs/max';
import { Base64 } from 'js-base64';
import services from './services';
import { getUrlQuery } from './utils/comm';
import { setCookie } from './utils/cookie';
import { checkBeforeLogin } from './utils/loginUtil/SSO';

//request 配置
export const request: RequestConfig = {
  timeout: 600000,
  errorConfig,
  requestInterceptors,
  responseInterceptors,
};

let token = getUrlQuery('access_token');
if (token) {
  setCookie('token', Base64.decode(token));
}

export function render(oldRender: () => void) {
  checkBeforeLogin(() => {
    oldRender();
  });
}

export async function getInitialState(): Promise<any> {
  let areaId: string = '';
  try {
    const userPermDataResp = await services.CommQuery.getUserDataPermission();
    if (userPermDataResp.code === 200) {
      areaId = (userPermDataResp?.data as any)?.areaGrid[0];
    }
  } catch (error) {
    console.error('获取初始数据出错：', error);
  }
  return {
    areaId,
  };
}
