import { CURRENT } from './renderAuthorize';

const checkPermissions = (
  authority,
  flatMenu,
  currentAuthority,
  target,
  Exception,
) => {
  const filterPath = [
    { name: '/', path: '/' },
    { name: 'civbase', path:`/civbase/?client=${window.globalConfig.client}` },
    { name: 'industry', path: '/industry' },
    { name: 'civweb4', path: `/civweb4/` },
    { name: 'civweb4', path: `/civweb4/?client=${window.globalConfig.client}` },
    { name: '404', path: '/404' },
    { name: '403', path: '/403' },
    { name: '500', path: '/500' },
  ];

  if (!authority) {
    return target;
  }

  if (Array.isArray(flatMenu)) {
    debugger
    const data = flatMenu.concat(filterPath);
    const findIndex = data.findIndex(item => item.path === authority);
    if (findIndex > -1) {
      return target;
    }

    return Exception;
  }
  throw new Error('unsupported parameters');
};

function check(authority, flatMenu, target, Exception) {
  return checkPermissions(
    authority,
    flatMenu,
    CURRENT,
    target,
    Exception,
  );
}

export default check;
