'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

exports.requireable = requireable;

var _propTypes = require('prop-types');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var selectors = _propTypes.PropTypes.oneOfType([_propTypes.PropTypes.string, _propTypes.PropTypes.arrayOf(_propTypes.PropTypes.string)]);

var attemptHistory = _propTypes.PropTypes.arrayOf(_propTypes.PropTypes.shape({
  authoritative: _propTypes.PropTypes.bool.isRequired,
  displayName: _propTypes.PropTypes.string,
  percentage: _propTypes.PropTypes.number,
  pointsPossible: _propTypes.PropTypes.number,
  score: _propTypes.PropTypes.number,
  quizSessionId: _propTypes.PropTypes.string
}));

function higherOrderFunctionValidator(props, propName, componentName, location) {
  var propValue = props[propName];
  var propValueType = typeof propValue === 'undefined' ? 'undefined' : (0, _typeof3.default)(propValue);

  if (propValueType !== 'function') {
    return new Error('Invalid ' + location + ' `' + propName + '` of type `' + propValueType + '` ' + ('supplied to `' + componentName + '`, expected a higher order function'));
  }

  var returnValue = propValue();
  var returnValueType = typeof returnValue === 'undefined' ? 'undefined' : (0, _typeof3.default)(returnValue);

  if (returnValueType !== 'function') {
    return new Error('Invalid ' + location + ' `' + propName + '` supplied to `' + componentName + '`, ' + ('the prop function returned a type of ' + returnValueType + ', expected a function'));
  }
}

function requireable(func) {
  function required(isRequired, props, propName, componentName, location) {
    if (props[propName] != null) {
      return func(props, propName, componentName, location);
    }

    if (isRequired) {
      return new Error('Required ' + location + ' `' + propName + '` was not supplied to ' + componentName);
    }
  }

  var requireableFunc = required.bind(null, false);
  requireableFunc.isRequired = required.bind(null, true);

  return requireableFunc;
}

var higherOrderFunction = requireable(higherOrderFunctionValidator);

exports.default = {
  attemptHistory: attemptHistory,
  higherOrderFunction: higherOrderFunction,
  selectors: selectors
};