import React from 'react';

const Exception403 = () => 403;

export const isComponentClass = component => {
  if (!component) return false;

  const proto = Object.getPrototypeOf(component);
  if (proto === React.component || proto === Function.prototype) return true;
  return isComponentClass(proto);
};

const checkIsInstantiation = target => {
  if (isComponentClass(target)) {
    const Target = target;
    return props => <Target {...props} />;
  }

  if (React.isValidElement(target)) {
    // eslint-disable-next-line react/jsx-no-undef
    return props => <Target {...props} />;
  }
  return () => target;
};

const authorize = (authority, error) => {
  let classError = false;
  if (error) {
    classError = () => error;
  }

  if (!authority) {
    throw new Error('authority is required');
  }

  return function decideAuthority(target) {
    // eslint-disable-next-line no-undef
    const component = CheckPermissions(
      authority,
      target,
      classError || Exception403,
    );
    return checkIsInstantiation(component);
  };
};

export default authorize;