import React from 'react';
import { getRoutes } from './getRoutes';
interface Permission {
  url: string;
  childMenus?: Permission[] | null;
  resourceId: string;
  name: string;
  resourceType: string;
  [key: string]: any;
}

interface YRouterItem {
  path: string;
  param?: string;
  url?: string;
  origion_url: string;
  name: string;
  meta: {
    title: string;
    icon?: string;
  };
  component?: React.FunctionComponent;
  hidden?: boolean;
  children?: YRouterItem[];
}

interface YRouterConfig {
  [id: string]: {
    component?: React.FunctionComponent;
    icon?: string;
    hidden?: boolean;
    url?: string;
    param?: string;
  };
}

const permissionList1 = [
  {
    childMenus: [
      {
        isHasResourcce: false,
        isPublish: 'PUBLISH',
        name: '导出记录（订单等）',
        pages: 0,
        projectId: 'PRO_SETTLE',
        records: [],
        remark: '',
        resourceId: 'RM0505694576E7710440150123921001',
        resourceParentId: 'RM0505694576E7810438978323482841',
        resourceType: 'MENU',
        status: '1',
        url: 'exportRecord/order',
        childMenus: [
          {
            isHasResourcce: false,
            isPublish: 'PRIVATE',
            name: '主播管理编辑',
            pages: 0,
            projectId: 'PRODISTRIBUTE',
            records: [],
            remark: '',
            resourceId: 'RM05056941EE16952569231938874211',
            resourceParentId: 'MENU4C7EF0AB17979431793224621074',
            resourceType: 'BUTTON',
            status: '1',
            url: 'edit_anchor',
          },
          {
            isHasResourcce: false,
            isPublish: 'PUBLISH',
            name: '审核',
            pages: 0,
            projectId: 'PRODISTRIBUTE',
            records: [],
            remark: '',
            resourceId: 'RM05056941EE16478478885221167367',
            resourceParentId: 'MENU4C7EF0AB17979431793224621074',
            resourceType: 'BUTTON',
            status: '1',
            url: 'audit_anchor',
          },
          {
            isHasResourcce: false,
            isPublish: 'PUBLISH',
            name: '分配主播权限',
            pages: 0,
            projectId: 'PRODISTRIBUTE',
            records: [],
            remark: '',
            resourceId: 'RM5056941EE171122549011526955101',
            resourceParentId: 'MENU4C7EF0AB17979431793224621074',
            resourceType: 'BUTTON',
            status: '1',
            url: 'assign_anchor_tobd',
          },
        ],
      },
      {
        isHasResourcce: false,
        isPublish: 'PUBLISH',
        name: '导出记录（结算等）',
        pages: 0,
        projectId: 'PRO_SETTLE',
        records: [],
        remark: '',
        resourceId: 'RM0505694576E7310440576901566882',
        resourceParentId: 'RM0505694576E7810438978323482841',
        resourceType: 'MENU',
        status: '1',
        url: 'exportRecord/settlement',
      },
      {
        isHasResourcce: false,
        isPublish: 'PRIVATE',
        name: '账号管理编辑',
        pages: 0,
        projectId: 'PRODISTRIBUTE',
        records: [],
        remark: '',
        resourceId: 'RM05056941EE16952568053524630680',
        resourceParentId: 'MENUC7EF0AB221316582934380956940',
        resourceType: 'BUTTON',
        status: '1',
        url: 'edit_account',
      },
    ],
    isHasResourcce: false,
    isPublish: 'PUBLISH',
    name: '导出记录',
    pages: 0,
    projectId: 'PRO_SETTLE',
    records: [],
    remark: '',
    resourceId: 'RM0505694576E7810438978323482841',
    resourceParentId: '',
    resourceType: 'MENU',
    status: '1',
    url: 'exportRecord',
  },
  {
    childMenus: [
      {
        isHasResourcce: false,
        isPublish: 'PUBLISH',
        name: '供应商结算',
        pages: 0,
        projectId: 'PRO_SETTLE',
        records: [],
        remark: '',
        resourceId: 'RM0505694576E7410441210872416230',
        resourceParentId: 'RM0505694576E7310439624296424211',
        resourceType: 'MENU',
        status: '1',
        url: 'financialManage/supplySettlement',
      },
    ],
    isHasResourcce: false,
    isPublish: 'PUBLISH',
    name: '财务管理',
    pages: 0,
    projectId: 'PRO_SETTLE',
    records: [],
    remark: '',
    resourceId: 'RM0505694576E7310439624296424211',
    resourceParentId: '',
    resourceType: 'MENU',
    status: '1',
    url: 'financialManage',
  },
];

const permissionList2 = [
  {
    childMenus: [
      {
        isHasResourcce: false,
        isPublish: 'PUBLISH',
        name: '导出记录（订单等）',
        pages: 0,
        projectId: 'PRO_SETTLE',
        records: [],
        remark: '',
        resourceId: 'RM0505694576E7710440150123921001',
        resourceParentId: 'RM0505694576E7810438978323482841',
        resourceType: 'MENU',
        status: '1',
        url: 'exportRecord/order',
        childMenus: [
          {
            isHasResourcce: false,
            isPublish: 'PRIVATE',
            name: '主播管理编辑',
            pages: 0,
            projectId: 'PRODISTRIBUTE',
            records: [],
            remark: '',
            resourceId: 'RM05056941EE16952569231938874211',
            resourceParentId: 'MENU4C7EF0AB17979431793224621074',
            resourceType: 'BUTTON',
            status: '1',
            url: 'edit_anchor',
          },
          {
            isHasResourcce: false,
            isPublish: 'PUBLISH',
            name: '审核',
            pages: 0,
            projectId: 'PRODISTRIBUTE',
            records: [],
            remark: '',
            resourceId: 'RM05056941EE16478478885221167367',
            resourceParentId: 'MENU4C7EF0AB17979431793224621074',
            resourceType: 'BUTTON',
            status: '1',
            url: 'audit_anchor',
          },
          {
            isHasResourcce: false,
            isPublish: 'PUBLISH',
            name: '分配主播权限',
            pages: 0,
            projectId: 'PRODISTRIBUTE',
            records: [],
            remark: '',
            resourceId: 'RM5056941EE171122549011526955101',
            resourceParentId: 'MENU4C7EF0AB17979431793224621074',
            resourceType: 'BUTTON',
            status: '1',
            url: 'assign_anchor_tobd',
          },
        ],
      },
      {
        isHasResourcce: false,
        isPublish: 'PUBLISH',
        name: '导出记录（结算等）',
        pages: 0,
        projectId: 'PRO_SETTLE',
        records: [],
        remark: '',
        resourceId: 'RM0505694576E7310440576901566882',
        resourceParentId: 'RM0505694576E7810438978323482841',
        resourceType: 'MENU',
        status: '1',
        url: 'exportRecord/settlement',
      },
      {
        isHasResourcce: false,
        isPublish: 'PRIVATE',
        name: '账号管理编辑',
        pages: 0,
        projectId: 'PRODISTRIBUTE',
        records: [],
        remark: '',
        resourceId: 'RM05056941EE16952568053524630680',
        resourceParentId: 'MENUC7EF0AB221316582934380956940',
        resourceType: 'BUTTON',
        status: '1',
        url: 'edit_account',
      },
    ],
    isHasResourcce: false,
    isPublish: 'PUBLISH',
    name: '导出记录',
    pages: 0,
    projectId: 'PRO_SETTLE',
    records: [],
    remark: '',
    resourceId: 'RM0505694576E7810438978323482841',
    resourceParentId: '',
    resourceType: 'MENU',
    status: '1',
    url: 'exportRecord',
  },
  {
    childMenus: [],
    isHasResourcce: false,
    isPublish: 'PUBLISH',
    name: '财务管理',
    pages: 0,
    projectId: 'PRO_SETTLE',
    records: [],
    remark: '',
    resourceId: 'RM0505694576E7310439624296424211',
    resourceParentId: '',
    resourceType: 'MENU',
    status: '1',
    url: 'financialManage',
  },
];

const permissionList3 = [
  {
    childMenus: [
      {
        childMenus: [
          {
            isHasResourcce: false,
            isPublish: 'PUBLISH',
            name: '创建品牌',
            pages: 0,
            projectId: 'PROGOODS',
            records: [],
            remark: '',
            resourceId: 'RM050569467C87079396118295819700',
            resourceParentId: 'MENU1D22321947940064616746177851',
            resourceType: 'MENU',
            status: '1',
            url: '/goodsManage/addBrand',
          },
          {
            isHasResourcce: false,
            isPublish: 'PRIVATE',
            name: '主播管理编辑',
            pages: 0,
            projectId: 'PRODISTRIBUTE',
            records: [],
            remark: '',
            resourceId: 'RM05056941EE16952569231938874211',
            resourceParentId: 'MENU4C7EF0AB17979431793224621074',
            resourceType: 'BUTTON',
            status: '1',
            url: 'edit_anchor',
          },
        ],
        icon: '',
        isHasResourcce: false,
        isPublish: 'PRIVATE',
        name: '品牌管理',
        pages: 0,
        projectId: 'PROGOODS',
        records: [],
        remark: '',
        resourceId: 'MENU1D22321947940064616746177851',
        resourceParentId: 'MENU04CAA28466624885976902549854',
        resourceType: 'MENU',
        sort: 80,
        status: '1',
        url: '/goodsManage/brandManagement',
      },
      {
        isHasResourcce: false,
        isPublish: 'PRIVATE',
        name: 'SPU编辑',
        pages: 0,
        projectId: 'PROGOODS',
        records: [],
        remark: '',
        resourceId: 'RM050569467C86364022170303074050',
        resourceParentId: 'MENU04CAA28466624885976902549854',
        resourceType: 'MENU',
        sort: 81,
        status: '1',
        url: '/goodsManage/editSpu',
      },
    ],
    icon: '',
    isHasResourcce: false,
    isPublish: 'PRIVATE',
    name: '基础资料库',
    pages: 0,
    projectId: 'PROGOODS',
    records: [],
    remark: '',
    resourceId: 'MENU04CAA28466624885976902549854',
    resourceParentId: '',
    resourceType: 'MENU',
    sort: 3,
    status: '1',
    url: '/goodsManage',
  },
];

const routerConfig1 = {
  exportRecord: {},
  'exportRecord/order': {
    component: () => (
      <div>
        <div>exportRecord/order</div>
      </div>
    ),
  },
  'exportRecord/settlement': {
    param: '/:id',
    hidden: true,
    component: () => (
      <div>
        <div>exportRecord/settlement</div>
      </div>
    ),
  },
};

const routerConfig2 = {
  exportRecord: {},
  'exportRecord/order': {
    component: () => (
      <div>
        <div>exportRecord/order</div>
      </div>
    ),
  },
  'exportRecord/settlement': {
    param: '/:id',
    component: () => (
      <div>
        <div>exportRecord/settlement</div>
      </div>
    ),
  },
};

const routerConfig3 = {
  exportRecord: {},
  'exportRecord/order': {
    component: () => (
      <div>
        <div>exportRecord/order</div>
      </div>
    ),
  },
  'exportRecord/settlement': {
    param: '/:id',
    component: () => (
      <div>
        <div>exportRecord/settlement</div>
      </div>
    ),
  },
  financialManage: {},
};

const routerConfig4 = {
  '/goodsManage': {},
  '/goodsManage/editSpu': {
    component: () => (
      <div>
        <div>exportRecord/order</div>
      </div>
    ),
  },
  '/goodsManage/brandManagement': {
    component: () => (
      <div>
        <div>exportRecord/settlement</div>
      </div>
    ),
  },
  '/goodsManage/addBrand': {
    hidden: true,
    component: () => (
      <div>
        <div>exportRecord/settlement</div>
      </div>
    ),
  },
};

const result1 = {
  menus: [
    {
      path: '/exportRecord',
      param: '',
      url: undefined,
      name: 'RM0505694576E7810438978323482841',
      meta: {
        title: '导出记录',
        icon: undefined,
      },
      component: undefined,
      hidden: undefined,
      origion_url: '/exportRecord',
      children: [
        {
          path: '/exportRecord/order',
          param: '',
          url: undefined,
          name: 'RM0505694576E7710440150123921001',
          meta: {
            title: '导出记录（订单等）',
            icon: undefined,
          },
          component: expect.any(Function),
          hidden: undefined,
          origion_url: '/exportRecord/order',
        },
        {
          path: '/exportRecord/settlement',
          param: '/:id',
          url: undefined,
          name: 'RM0505694576E7310440576901566882',
          meta: {
            title: '导出记录（结算等）',
            icon: undefined,
          },
          component: expect.any(Function),
          hidden: true,
          origion_url: '/exportRecord/settlement',
        },
      ],
    },
  ],
  defaultSelectedMenus: ['RM0505694576E7710440150123921001'],
  defaultOpenMenus: ['RM0505694576E7810438978323482841'],
  defaultMenu: {
    path: '/exportRecord/order',
    param: '',
    url: undefined,
    name: 'RM0505694576E7710440150123921001',
    meta: {
      title: '导出记录（订单等）',
      icon: undefined,
    },
    component: expect.any(Function),
    hidden: undefined,
    origion_url: '/exportRecord/order',
  },
  permissions: [
    'edit_anchor',
    'audit_anchor',
    'assign_anchor_tobd',
    'edit_account',
  ],
};

const result2 = {
  menus: [
    {
      path: '/exportRecord',
      param: '',
      url: undefined,
      name: 'RM0505694576E7810438978323482841',
      meta: {
        title: '导出记录',
        icon: undefined,
      },
      component: undefined,
      hidden: undefined,
      origion_url: '/exportRecord',
      children: [
        {
          path: '/exportRecord/order',
          param: '',
          url: undefined,
          name: 'RM0505694576E7710440150123921001',
          meta: {
            title: '导出记录（订单等）',
            icon: undefined,
          },
          component: expect.any(Function),
          hidden: undefined,
          origion_url: '/exportRecord/order',
        },
        {
          path: '/exportRecord/settlement',
          param: '/:id',
          url: undefined,
          name: 'RM0505694576E7310440576901566882',
          meta: {
            title: '导出记录（结算等）',
            icon: undefined,
          },
          component: expect.any(Function),
          hidden: undefined,
          origion_url: '/exportRecord/settlement',
        },
      ],
    },
  ],
  defaultSelectedMenus: ['RM0505694576E7310440576901566882'],
  defaultOpenMenus: ['RM0505694576E7810438978323482841'],
  defaultMenu: {
    path: '/exportRecord/settlement',
    param: '/:id',
    url: undefined,
    name: 'RM0505694576E7310440576901566882',
    meta: {
      title: '导出记录（结算等）',
      icon: undefined,
    },
    component: expect.any(Function),
    hidden: undefined,
    origion_url: '/exportRecord/settlement',
  },
  permissions: [
    'edit_anchor',
    'audit_anchor',
    'assign_anchor_tobd',
    'edit_account',
  ],
};

const result3 = {
  menus: [],
  defaultSelectedMenus: [],
  defaultOpenMenus: [],
  defaultMenu: {},
  permissions: [],
};

const result4 = {
  menus: [
    {
      path: '/exportRecord',
      param: '',
      name: 'RM0505694576E7810438978323482841',
      meta: { title: '导出记录' },
      origion_url: '/exportRecord',
      component: undefined,
      hidden: undefined,
      url: undefined,
      children: [
        {
          path: '/exportRecord/order',
          param: '',
          name: 'RM0505694576E7710440150123921001',
          meta: { title: '导出记录（订单等）' },
          origion_url: '/exportRecord/order',
          component: expect.any(Function),
          hidden: undefined,
          url: undefined,
        },
        {
          path: '/exportRecord/settlement',
          param: '/:id',
          name: 'RM0505694576E7310440576901566882',
          meta: { title: '导出记录（结算等）' },
          origion_url: '/exportRecord/settlement',
          component: expect.any(Function),
          hidden: undefined,
          url: undefined,
        },
      ],
    },
    {
      path: '/financialManage',
      param: '',
      name: 'RM0505694576E7310439624296424211',
      meta: { title: '财务管理' },
      origion_url: '/financialManage',
      children: [],
      component: undefined,
      hidden: undefined,
      url: undefined,
    },
  ],
  defaultSelectedMenus: ['RM0505694576E7310440576901566882'],
  defaultOpenMenus: ['RM0505694576E7810438978323482841'],
  defaultMenu: {
    path: '/exportRecord/settlement',
    param: '/:id',
    name: 'RM0505694576E7310440576901566882',
    meta: { title: '导出记录（结算等）' },
    origion_url: '/exportRecord/settlement',
    component: expect.any(Function),
    url: undefined,
    hidden: undefined,
  },
  permissions: [
    'edit_anchor',
    'audit_anchor',
    'assign_anchor_tobd',
    'edit_account',
  ],
};

const result5 = {
  menus: [
    {
      path: '/exportRecord',
      param: '',
      name: 'RM0505694576E7810438978323482841',
      meta: { title: '导出记录' },
      origion_url: '/exportRecord',
      component: undefined,
      hidden: undefined,
      url: undefined,
      children: [
        {
          path: '/exportRecord/order',
          param: '',
          name: 'RM0505694576E7710440150123921001',
          meta: { title: '导出记录（订单等）', icon: undefined },
          origion_url: '/exportRecord/order',
          component: expect.any(Function),
          hidden: undefined,
          url: undefined,
        },
        {
          path: '/exportRecord/settlement',
          param: '/:id',
          name: 'RM0505694576E7310440576901566882',
          meta: { title: '导出记录（结算等）', icon: undefined },
          origion_url: '/exportRecord/settlement',
          component: expect.any(Function),
          hidden: undefined,
          url: undefined,
        },
      ],
    },
    {
      path: '/financialManage',
      param: '',
      name: 'RM0505694576E7310439624296424211',
      meta: { title: '财务管理', icon: undefined },
      origion_url: '/financialManage',
      children: [],
      component: undefined,
      hidden: undefined,
      url: undefined,
    },
  ],
  defaultSelectedMenus: [],
  defaultOpenMenus: ['RM0505694576E7810438978323482841'],
  defaultMenu: undefined,
  permissions: [
    'edit_anchor',
    'audit_anchor',
    'assign_anchor_tobd',
    'edit_account',
  ],
};

const result6 = {
  menus: [
    {
      path: '/goodsManage',
      param: '',
      name: 'MENU04CAA28466624885976902549854',
      meta: { title: '基础资料库', icon: undefined },
      origion_url: '/goodsManage',
      component: undefined,
      hidden: undefined,
      url: undefined,
      children: [
        {
          path: '/goodsManage/brandManagement',
          param: '',
          name: 'MENU1D22321947940064616746177851',
          meta: { title: '品牌管理', icon: undefined },
          origion_url: '/goodsManage/brandManagement',
          component: expect.any(Function),
          hidden: undefined,
          url: undefined,
        },
        {
          path: '/goodsManage/addBrand',
          param: '',
          name: 'RM050569467C87079396118295819700',
          meta: { title: '创建品牌', icon: undefined },
          origion_url: '/goodsManage/addBrand',
          component: expect.any(Function),
          hidden: true,
          url: undefined,
        },
        {
          path: '/goodsManage/editSpu',
          param: '',
          name: 'RM050569467C86364022170303074050',
          meta: { title: 'SPU编辑', icon: undefined },
          origion_url: '/goodsManage/editSpu',
          component: expect.any(Function),
          hidden: undefined,
          url: undefined,
        },
      ],
    },
  ],
  defaultSelectedMenus: ['MENU1D22321947940064616746177851'],
  defaultOpenMenus: ['MENU04CAA28466624885976902549854'],
  defaultMenu: {
    path: '/goodsManage/brandManagement',
    param: '',
    name: 'MENU1D22321947940064616746177851',
    meta: { title: '品牌管理', icon: undefined },
    origion_url: '/goodsManage/brandManagement',
    component: expect.any(Function),
    hidden: undefined,
    url: undefined,
  },
  permissions: ['edit_anchor'],
};

describe('getRoutes', () => {
  it('defaultConfig is empty & pathname is /', () => {
    expect(
      getRoutes<Permission, YRouterConfig, YRouterItem>(
        permissionList1,
        routerConfig1,
        {},
        '/',
      ),
    ).toEqual(result1);
  });

  // it('primaryDefaultMenu:/exportRecord/settlement  is  & pathname is /', () => {
  //   expect(
  //     getRoutes<Permission, YRouterConfig, YRouterItem>(
  //       permissionList1,
  //       routerConfig1,
  //       { primaryDefaultMenu: 'exportRecord/settlement' },
  //       '/',
  //     ),
  //   ).toEqual(result2);
  // });

  it('primaryDefaultMenu:/exportRecord/settlement  is  & pathname is /exportRecord/order', () => {
    expect(
      getRoutes<Permission, YRouterConfig, YRouterItem>(
        permissionList1,
        routerConfig1,
        { primaryDefaultMenu: 'exportRecord/settlement' },
        '/exportRecord/order',
      ),
    ).toEqual(result1);
  });

  it('primaryDefaultMenu:/exportRecord/settlement  is  & pathname is /', () => {
    expect(
      getRoutes<Permission, YRouterConfig, YRouterItem>(
        permissionList1,
        routerConfig2,
        { primaryDefaultMenu: 'exportRecord/settlement' },
        '/',
      ),
    ).toEqual(result2);
  });

  it('primaryDefaultMenu:/exportRecord/settlement  is  & pathname is /exportRecord/settlement/999', () => {
    expect(
      getRoutes<Permission, YRouterConfig, YRouterItem>(
        permissionList1,
        routerConfig2,
        { primaryDefaultMenu: 'exportRecord/settlement' },
        '/exportRecord/settlement/999',
      ),
    ).toEqual(result2);
  });

  it('permissionList:[]', () => {
    expect(
      getRoutes<Permission, YRouterConfig, YRouterItem>(
        [],
        routerConfig2,
        { primaryDefaultMenu: 'exportRecord/settlement' },
        '/exportRecord/settlement/999',
      ),
    ).toEqual(result3);
  });

  it('有childMenus是null', () => {
    expect(
      getRoutes<Permission, YRouterConfig, YRouterItem>(
        permissionList2,
        routerConfig3,
        { primaryDefaultMenu: 'exportRecord/settlement' },
        '/',
      ),
    ).toEqual(result4);
  });

  it('pathname没有权限', () => {
    expect(
      getRoutes<Permission, YRouterConfig, YRouterItem>(
        permissionList2,
        routerConfig3,
        { primaryDefaultMenu: 'exportRecord/settlement' },
        'exportRecord/77777777',
      ),
    ).toEqual(result5);
  });

  it('菜单多级嵌套', () => {
    expect(
      getRoutes<Permission, YRouterConfig, YRouterItem>(
        permissionList3,
        routerConfig4,
        { primaryDefaultMenu: '' },
        '/',
      ),
    ).toEqual(result6);
  });
});
