function _extends() {
  _extends = Object.assign || function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];

      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }

    return target;
  };

  return _extends.apply(this, arguments);
}

var defaults = {
  addScaleEnabled: true,
  description: false,
  excludeZero: false,
  halfScoring: false,
  maxPointsEnabled: true,
  pointLabels: true,
  scales: [],
  standards: false,
  visibleToStudent: true
};

const markupToText = s => {
  return (s || '').replace(/(<([^>]+)>)/ig, '');
};

function createDefaultModel(model = {}) {
  return new Promise(resolve => resolve(_extends({}, defaults, model)));
}
const normalize = question => _extends({}, defaults, question);
/**
 *
 * @param {*} question
 * @param {*} session
 * @param {*} env
 */

async function model(question, session, env) {
  const 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:


  const {
    scales,
    excludeZero
  } = normalizedQuestion || {};
  const parsedScales = (scales || []).map(scale => _extends({}, scale, {
    excludeZero
  }));
  return _extends({}, normalizedQuestion, {
    scales: parsedScales
  });
}
const getScore = () => 0;
/**
 * @param {Object} model - the main model
 * @param {*} session
 * @param {Object} env
 */

function outcome() {
  return new Promise(resolve => resolve({
    score: 0,
    empty: true
  }));
}
const createCorrectResponseSession = (question, env) => {
  return new Promise(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!“.

const validate = (model, config) => {
  const {
    scales,
    description = false,
    pointLabels = false
  } = model;
  const errors = {};
  const traitsErrors = {};
  const scorePointsErrors = {};
  (scales || []).forEach((scale, scaleIndex) => {
    const {
      traits = []
    } = scale;
    const {
      scorePointsLabels = []
    } = scale;
    const scaleErrors = {};
    const scorePointsLabelsErrors = {};

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

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

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

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

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

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

        if (description && identicalTraitDescr) {
          scaleErrors[traitIndex] = _extends({}, 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;
};

export { createCorrectResponseSession, createDefaultModel, getScore, model, normalize, outcome, validate };
//# sourceMappingURL=controller.js.map
