import React from 'react';

import { CURRENT } from './renderAuthorize';

const checkPermissions = (
  authority,
  flatMenu,
  currentAuthority,
  target,
  Exception,
) => {
  debugger;
  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)) {
    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) {
  debugger;
  return checkPermissions(
    authority,
    flatMenu,
    CURRENT,
    <div id="micro-container" className="subapp-container" />,
    Exception,
  );
}

export default check;
