import Mock from 'mockjs';
import setupMock, {
  successResponseWrap,
  failResponseWrap,
} from '@/utils/setup-mock';

import { MockParams } from '@/types/mock';
import { menus } from './datas/user';

setupMock({
  setup() {
    // Mock.XHR.prototype.withCredentials = true;

    // 登录
    Mock.mock(new RegExp('/api/user/login'), (params: MockParams) => {
      const { account, password } = JSON.parse(params.body);
      if (!account) {
        return failResponseWrap(null, '用户名不能为空', 50000);
      }
      if (!password) {
        return failResponseWrap(null, '密码不能为空', 50000);
      }

      return successResponseWrap({
        id: 1,
        account,
        name: '张三',
        role: 'ADMIN',
        sessionId: '123456',
        token: 'abcdef',
        orgId: 1,
        categoryId: 1,
        applyTaskId: 1,
      });
    });

    // 登出
    Mock.mock(new RegExp('/api/user/logout'), () => {
      return successResponseWrap(null);
    });

    // 用户的服务端菜单
    Mock.mock(new RegExp('/api/admin/sys/user/get_menu'), () => {
      return successResponseWrap({
        userId: 1,
        privileges: menus,
      });
    });
  },
});
