"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.requireable = requireable;
exports.default = void 0;

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

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 numberInRange(start, end) {
  return function (props, propName, componentName, location) {
    var error = _propTypes.PropTypes.number.apply(null, arguments);

    if (error) return error;
    var value = props[propName];

    if (value != null && value < start || value > end) {
      return new Error("Invalid ".concat(location, " `").concat(propName, "` supplied to `").concat(componentName, "`, ") + "must be between ".concat(start, " and ").concat(end));
    }
  };
}

function higherOrderFunctionValidator(props, propName, componentName, location) {
  var propValue = props[propName];
  var propValueType = typeof propValue;

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

  var returnValue = propValue();
  var returnValueType = typeof returnValue;

  if (returnValueType !== 'function') {
    return new Error("Invalid ".concat(location, " `").concat(propName, "` supplied to `").concat(componentName, "`, ") + "the prop function returned a type of ".concat(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 ".concat(location, " `").concat(propName, "` was not supplied to ").concat(componentName));
    }
  }

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

var higherOrderFunction = requireable(higherOrderFunctionValidator);

function recordId(props, propName, componentName, location) {
  var error = _propTypes.PropTypes.string.apply(null, arguments);

  if (error) return error;

  if (!/^[1-9]\d*$/.test(props[propName])) {
    return new Error("Invalid ".concat(location, " `").concat(propName, "` supplied to `").concat(componentName, "`, must be a positive integer."));
  }
}

var _default = {
  attemptHistory: attemptHistory,
  higherOrderFunction: higherOrderFunction,
  numberInRange: numberInRange,
  recordId: recordId,
  selectors: selectors
};
exports.default = _default;