<commit_before>import _                        from 'lodash';
import baseSerializer           from './base';
import { scrub, languageText }  from '../../serializer_utils';
import genusTypes               from '../../../../constants/genus_types';
import guid                     from '../../../../utils/guid';

function serializeChoices(originalChoices, newChoiceAttributes, language) {
  const choices = _.map(newChoiceAttributes, (updateValues, id) => {
    const choice = originalChoices[id];
    const originalText = _.get(choice, `texts[${language}]`, '');
    const text = _.get(updateValues, 'text', originalText);
    return {
      id,
      text: languageText(text, language),
      delete: _.get(updateValues, 'delete'),
    };
  });

  if (newChoiceAttributes.new) {
    choices.push({
      id: guid(),
      text: languageText('', language),
    });
  }

  return choices;
}

function serializeQuestion(originalQuestion, newQuestionAttributes, language) {
  const newQuestion = {
    multiAnswer: newQuestionAttributes.multiAnswer,
    shuffle: newQuestionAttributes.shuffle,
    timeValue: newQuestionAttributes.timeValue,
    choices: null,
  };

  if (newQuestionAttributes.choices) {
    newQuestion.choices = serializeChoices(
      originalQuestion.choices,
      newQuestionAttributes.choices,
      language
    );
  }

  return scrub(newQuestion);
}

function serializeAnswers(originalChoices, oldAnswers, feedback) {
  return [{
    id: _.get(_.find(oldAnswers, { genusTypeId: genusTypes.answer.rightAnswer }), 'id'),
    genusTypeId: genusTypes.answer.rightAnswer,
    feedback,
    type: genusTypes.answer.multipleAnswer,
    choiceIds: _.map(originalChoices, 'id'),
  }];
}

function killAnswers(answers) {
  return _.map(answers, answer => ({ id: answer.id, delete: true }));
}


export default function surveySerializer(originalItem, newItemAttributes) {
  const newItem = baseSerializer(originalItem, newItemAttributes);

  const { question, language } = newItemAttributes;
  if (question) {
    newItem.question = {
      ...newItem.question,
      ...serializeQuestion(originalItem.question, question, language)
    };
    if (question.choices || question.correctFeedback) {
      if (newItemAttributes.type && originalItem.type !== newItemAttributes.type) {
        newItem.answers = killAnswers(_.get(originalItem, 'originalItem.answers'));
      } else {
        newItem.answers = serializeAnswers(
          originalItem.question.choices,
          _.get(originalItem, 'originalItem.answers'),
          languageText(_.get(question, 'correctFeedback.text'), language),
        );
      }
    }<commit_msg>keeps choices from deriving out on reflection questions (#445)<commit_after>import _                        from 'lodash';
import baseSerializer           from './base';
import { scrub, languageText }  from '../../serializer_utils';
import genusTypes               from '../../../../constants/genus_types';
import guid                     from '../../../../utils/guid';

function serializeChoices(newChoiceAttributes, language) {
  const choices = [];
  if (newChoiceAttributes.new) {
    choices.push({
      id: guid(),
      text: languageText('', newChoiceAttributes.new.language),
    });
    return choices;
  }

  const serializedChoices = _(newChoiceAttributes)
    .entries()
    .filter(choice => choice[0] !== 'new' && !_.isUndefined(choice[1].text))
    .map(choice => ({
      id: choice[0],
      text: languageText(choice[1].text, language),
      delete: choice[1].delete,
    }))
    .value();

  return choices.concat(serializedChoices);
}

function serializeQuestion(newQuestionAttributes, language) {
  const newQuestion = {
    multiAnswer: newQuestionAttributes.multiAnswer,
    shuffle: newQuestionAttributes.shuffle,
    timeValue: newQuestionAttributes.timeValue,
    choices: null,
  };

  if (newQuestionAttributes.choices) {
    newQuestion.choices = serializeChoices(newQuestionAttributes.choices, language);
  }

  return scrub(newQuestion);
}

function serializeAnswers(originalChoices, oldAnswers, feedback) {
  return [{
    id: _.get(_.find(oldAnswers, { genusTypeId: genusTypes.answer.rightAnswer }), 'id'),
    genusTypeId: genusTypes.answer.rightAnswer,
    feedback,
    type: genusTypes.answer.multipleAnswer,
    choiceIds: _.map(originalChoices, 'id'),
  }];
}

function killAnswers(answers) {
  return _.map(answers, answer => ({ id: answer.id, delete: true }));
}


export default function surveySerializer(originalItem, newItemAttributes) {
  const newItem = baseSerializer(originalItem, newItemAttributes);

  const { question, language } = newItemAttributes;
  if (question) {
    newItem.question = {
      ...newItem.question,
      ...serializeQuestion(question, language)
    };
    if (question.choices || question.correctFeedback) {
      if (newItemAttributes.type && originalItem.type !== newItemAttributes.type) {
        newItem.answers = killAnswers(_.get(originalItem, 'originalItem.answers'));
      } else {
        newItem.answers = serializeAnswers(
          originalItem.question.choices,
          _.get(originalItem, 'originalItem.answers'),
          languageText(_.get(question, 'correctFeedback.text'), language),
        );
      }
    }
{'fraction_non_alphanumeric': 0.13093196112064037, 'fraction_numerical': 0.0015246807699637887, 'mean_word_length': 3.2910874897792315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '138967', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1466, 'n_words': 295}