import { PageContainer } from '@ant-design/pro-layout';
import type { RunTimeLayoutConfig } from 'umi';
import { history } from 'umi';
import RightContent from '@/components/RightContent';
import RoutePath from '@/routes/routePath';
import { CACHE_TOKEN } from '@/constants/cacheKey';
import { getRoleList } from './services/system/role';

// ProLayout 支持的api https://procomponents.ant.design/components/layout

export async function getInitialState() {
  if (localStorage.getItem(CACHE_TOKEN)) {
    const res = await getRoleList({});
    // return {
    //   routeList: res,
    // };
    return {
      routeList: [],
    };
  }
}

export const layout: RunTimeLayoutConfig = ({ initialState }) => {
  return {
    rightContentRender: () => <RightContent />,
    disableContentMargin: false,
    onPageChange: () => {
      if (!localStorage.getItem(CACHE_TOKEN)) {
        history.push(RoutePath.LOGIN);
      }
    },

    menuHeaderRender: undefined,
    // 自定义 403 页面
    // unAccessible: <div>unAccessible</div>,
    // 增加一个 loading 的状态
    childrenRender: (children) => {
      const pathname = location.hash.replace('#', '');
      return pathname === RoutePath.LOGIN ? children : <PageContainer>{children}</PageContainer>;
    },
    ...initialState?.settings,
  };
};
