"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createCorrectResponseSession = void 0;
exports.createDefaultModel = createDefaultModel;
exports.getScore = void 0;
exports.model = model;
exports.normalize = void 0;
exports.outcome = outcome;
exports.validate = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

var _defaults = _interopRequireDefault(require("./defaults"));

var _utils = require("./utils");

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; }

function createDefaultModel() {
  var model = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return new Promise(function (resolve) {
    return resolve(_objectSpread(_objectSpread({}, _defaults["default"]), model));
  });
}

var normalize = function normalize(question) {
  return _objectSpread(_objectSpread({}, _defaults["default"]), question);
};
/**
 *
 * @param {*} question
 * @param {*} session
 * @param {*} env
 */


exports.normalize = normalize;

function model(_x, _x2, _x3) {
  return _model.apply(this, arguments);
}

function _model() {
  _model = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(question, session, env) {
    var normalizedQuestion, _ref, scales, excludeZero, parsedScales;

    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            normalizedQuestion = normalize(question);

            if (!env.role || env.role === 'student') {
              normalizedQuestion.visible = normalizedQuestion.visibleToStudent;
            } else {
              normalizedQuestion.visible = true;
            } // todo update pie-ui instead of parsing this here:


            _ref = normalizedQuestion || {}, scales = _ref.scales, excludeZero = _ref.excludeZero;
            parsedScales = (scales || []).map(function (scale) {
              return _objectSpread(_objectSpread({}, scale), {}, {
                excludeZero: excludeZero
              });
            });
            return _context.abrupt("return", _objectSpread(_objectSpread({}, normalizedQuestion), {}, {
              scales: parsedScales
            }));

          case 5:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _model.apply(this, arguments);
}

var getScore = function getScore() {
  return 0;
};
/**
 * @param {Object} model - the main model
 * @param {*} session
 * @param {Object} env
 */


exports.getScore = getScore;

function outcome() {
  return new Promise(function (resolve) {
    return resolve({
      score: 0,
      empty: true
    });
  });
}

var createCorrectResponseSession = function createCorrectResponseSession(question, env) {
  return new Promise(function (resolve) {
    if (env.mode !== 'evaluate' && env.role === 'instructor') {
      resolve({
        id: '1'
      });
    } else {
      resolve(null);
    }
  });
}; // IMPORTANT! If you make any changes to this function, please make sure you also update complex-rubric/controller/validateMultiTraitRubric function!“.


exports.createCorrectResponseSession = createCorrectResponseSession;

var validate = function validate(model, config) {
  var scales = model.scales,
      _model$description = model.description,
      description = _model$description === void 0 ? false : _model$description,
      _model$pointLabels = model.pointLabels,
      pointLabels = _model$pointLabels === void 0 ? false : _model$pointLabels;
  var errors = {};
  var traitsErrors = {};
  var scorePointsErrors = {};
  (scales || []).forEach(function (scale, scaleIndex) {
    var _scale$traits = scale.traits,
        traits = _scale$traits === void 0 ? [] : _scale$traits;
    var _scale$scorePointsLab = scale.scorePointsLabels,
        scorePointsLabels = _scale$scorePointsLab === void 0 ? [] : _scale$scorePointsLab;
    var scaleErrors = {};
    var scorePointsLabelsErrors = {};

    if (pointLabels) {
      scorePointsLabels.forEach(function (scorePointLabel, scoreIndex) {
        if (!scorePointLabel || scorePointLabel === '<div></div>') {
          scorePointsLabelsErrors[scoreIndex] = 'Points labels should not be empty.';
        } else {
          var identicalScorePointLabel = scorePointsLabels.slice(scoreIndex + 1).some(function (s) {
            return (0, _utils.markupToText)(s) === (0, _utils.markupToText)(scorePointLabel);
          });

          if (identicalScorePointLabel) {
            scorePointsLabelsErrors[scoreIndex] = 'Points labels should be unique.';
          }
        }
      });
    }

    if (Object.keys(scorePointsLabelsErrors).length > 0) {
      scorePointsErrors[scaleIndex] = scorePointsLabelsErrors;
    }

    traits.forEach(function (trait, traitIndex) {
      if (!trait.name || trait.name === '<div></div>') {
        scaleErrors[traitIndex] = {
          name: 'Trait names should not be empty.'
        };
      } else {
        var identicalTraitName = traits.slice(traitIndex + 1).some(function (t) {
          return (0, _utils.markupToText)(t.name) === (0, _utils.markupToText)(trait.name);
        });

        if (identicalTraitName) {
          scaleErrors[traitIndex] = {
            name: 'Trait names should be unique.'
          };
        }
      }

      if (description && (!trait.description || trait.description === '<div></div>')) {
        scaleErrors[traitIndex] = _objectSpread(_objectSpread({}, scaleErrors[traitIndex]), {}, {
          description: 'Trait description should not be empty'
        });
      } else {
        var identicalTraitDescr = traits.slice(traitIndex + 1).some(function (t) {
          return (0, _utils.markupToText)(t.description) === (0, _utils.markupToText)(trait.description);
        });

        if (description && identicalTraitDescr) {
          scaleErrors[traitIndex] = _objectSpread(_objectSpread({}, scaleErrors[traitIndex]), {}, {
            description: 'Trait descriptions should be unique.'
          });
        }
      }
    });

    if (Object.keys(scaleErrors).length > 0) {
      traitsErrors[scaleIndex] = scaleErrors;
    }
  });

  if (Object.keys(traitsErrors).length > 0) {
    errors.traitsErrors = traitsErrors;
  }

  if (Object.keys(scorePointsErrors).length > 0) {
    errors.scorePointsErrors = scorePointsErrors;
  }

  return errors;
};

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