"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getFeedbackForCorrectness = exports.getFeedback = exports.getActualFeedbackForCorrectness = exports.getActualFeedback = exports.defaults = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

var defaults = {
  correct: {
    type: 'default',
    "default": 'Correct',
    custom: 'Correct'
  },
  incorrect: {
    type: 'default',
    "default": 'Incorrect',
    custom: 'Incorrect'
  },
  partial: {
    type: 'default',
    "default": 'Nearly',
    custom: 'Nearly'
  },
  unanswered: {
    type: 'default',
    "default": 'You have not entered a response',
    custom: 'You have not entered a response'
  }
};
/**
 * @typedef {Object} FeedbackConfig
 * @property {'default'|'none'|'custom'} type
 * @property {string} default
 * @property {string} custom
 *
 * @typedef {Object} Feedback
 *  @property {FeedbackConfig} correct
 *  @property {FeedbackConfig} incorrect
 *  @property {FeedbackConfig} partial
 */

exports.defaults = defaults;

var normalizeCorrectness = function normalizeCorrectness(c) {
  if (c === 'partially-correct') {
    return 'partial';
  }

  return c;
};
/**
 * Get the feedback for the correctness
 *
 * @param {'correct'|'incorrect'|'partial'} correctness
 * @param {Feedback} feedback
 */


var getFeedbackForCorrectness = function getFeedbackForCorrectness(correctness, feedback) {
  return new Promise(function (resolve) {
    feedback = _objectSpread(_objectSpread({}, defaults), feedback);
    correctness = normalizeCorrectness(correctness);
    var fb = feedback[correctness] || defaults[correctness] || {};
    var d = defaults[correctness] || {};
    getFeedback(fb, d[fb.type || 'default']).then(function (result) {
      return resolve(result);
    });
  });
};
/**
 * Get the feedback from a {FeedbackConfig}
 *
 * @param {FeedbackConfig} feedback
 * @param {string} fallback
 */


exports.getFeedbackForCorrectness = getFeedbackForCorrectness;

var getFeedback = function getFeedback(feedback, fallback) {
  return new Promise(function (resolve) {
    if (!feedback || feedback.type === 'none') {
      resolve(undefined);
      return;
    }

    feedback = feedback || {};
    var out = feedback[feedback.type] || fallback;
    resolve(out);
  });
}; // TODO: should replace getFeedbackForCorrectness

/**
 * Get feedback for correctness
 * @param {'correct'|'incorrect'|'partial'} correctness
 * @param {Feedback} feedback
 */


exports.getFeedback = getFeedback;

var getActualFeedbackForCorrectness = function getActualFeedbackForCorrectness(correctness, feedback) {
  feedback = _objectSpread(_objectSpread({}, defaults), feedback); // normalize correctness

  correctness = correctness === 'partially-correct' ? 'partial' : correctness;
  var defaultFeedback = defaults[correctness] || {};
  var fb = feedback[correctness] || defaultFeedback;
  return getActualFeedback(fb, defaultFeedback[fb.type || 'default']);
}; // TODO: should replace getFeedback

/**
 * Get the feedback from a {FeedbackConfig}
 * @param {FeedbackConfig} feedback
 * @param {string} fallback
 */


exports.getActualFeedbackForCorrectness = getActualFeedbackForCorrectness;

var getActualFeedback = function getActualFeedback(feedback, fallback) {
  if (!feedback || feedback.type === 'none') {
    return undefined;
  }

  return feedback[feedback.type] || fallback;
};

exports.getActualFeedback = getActualFeedback;
//# sourceMappingURL=index.js.map