"use strict";

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

var _immutable = require("immutable");

var _constants = require("@instructure/quiz-common/lib/constants");

var addTagAssociation = function addTagAssociation(state, entityType, entityId, tagAssociation) {
  return state.updateIn([entityType, entityId], function (tags) {
    return tags == null ? (0, _immutable.fromJS)([tagAssociation]) : tags.push((0, _immutable.fromJS)(tagAssociation));
  });
};

var removePendingAdditionTagAssociation = function removePendingAdditionTagAssociation(state, entityType, entityId, tagValue) {
  var additions = state.getIn(['pendingAddition', entityType, entityId]);

  if (additions != null) {
    return state.setIn(['pendingAddition', entityType, entityId], additions.filterNot(function (t) {
      return t.get('tag_value') === tagValue;
    }));
  } else {
    return state;
  }
};

var removePendingDeletionTagAssociation = function removePendingDeletionTagAssociation(state, entityType, entityId, tagAssociationId) {
  var deletions = state.getIn(['pendingDeletion', entityType, entityId]);

  if (deletions != null) {
    return state.setIn(['pendingDeletion', entityType, entityId], deletions.filterNot(function (t) {
      return t.get('id') === tagAssociationId;
    }));
  } else {
    return state;
  }
};

var removeTagAssociation = function removeTagAssociation(state, entityType, entityId, tagAssociationId) {
  var tags = state.getIn([entityType, entityId]);

  if (tags != null) {
    return state.setIn([entityType, entityId], tags.filterNot(function (t) {
      return t.get('id') === tagAssociationId;
    }));
  } else {
    return state;
  }
};

var setPendingAdditions = function setPendingAdditions(state, entityType, entityId, tagsToAdd) {
  return state.setIn(['pendingAddition', entityType, entityId], (0, _immutable.fromJS)(tagsToAdd));
};

var setPendingDeletions = function setPendingDeletions(state, entityType, entityId, tagsToDelete) {
  return state.setIn(['pendingDeletion', entityType, entityId], (0, _immutable.fromJS)(tagsToDelete));
};

var setTagAssociations = function setTagAssociations(state, entityType, entityId, tagAssociations) {
  return state.setIn([entityType, entityId], (0, _immutable.fromJS)(tagAssociations));
};

var setTagInput = function setTagInput(state, entityType, entityId, tagInput) {
  return state.setIn(['tagInput', entityType, entityId], tagInput);
};

var setTagSuggestions = function setTagSuggestions(state, entityType, entityId, tags, text) {
  var tagInput = state.getIn(['tagInput', entityType, entityId]);

  if (tagInput === text) {
    return state.setIn(['tagSuggestions', entityType, entityId], (0, _immutable.fromJS)(tags));
  }

  return state;
};

var toTags = function toTags(tag) {
  return {
    id: tag.id,
    tag_value: tag.tagValue,
    tag_id: tag.tagId
  };
};

var setTags = function setTags(state, entityType, entities) {
  var newState = state;
  entities.forEach(function (entity) {
    if (entity.tagAssociations) {
      newState = setTagAssociations(newState, entityType, entity.id, entity.tagAssociations.map(toTags));
    }
  });
  return newState;
};

var defaultState = (0, _immutable.fromJS)({});

var _default = function _default() {
  var state = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : defaultState;
  var action = arguments.length > 1 ? arguments[1] : void 0;

  switch (action.type) {
    case _constants.ADD_ITEMS:
      return setTags(state, 'Item', action.payload);

    case _constants.ADD_STIMULI:
      return setTags(state, 'Stimulus', action.payload);

    case _constants.ADD_TAG_ASSOCIATION:
      return addTagAssociation(state, action.payload.entityType, action.payload.entityId, action.payload.tagAssociation);

    case _constants.REMOVE_PENDING_ADDITION_TAG_ASSOCIATION:
      return removePendingAdditionTagAssociation(state, action.payload.entityType, action.payload.entityId, action.payload.tagValue);

    case _constants.REMOVE_PENDING_DELETION_TAG_ASSOCIATION:
      return removePendingDeletionTagAssociation(state, action.payload.entityType, action.payload.entityId, action.payload.tagAssociationId);

    case _constants.REMOVE_TAG_ASSOCIATION:
      return removeTagAssociation(state, action.payload.entityType, action.payload.entityId, action.payload.tagAssociationId);

    case _constants.SET_PENDING_ADDITION_TAG_ASSOCIATIONS:
      return setPendingAdditions(state, action.payload.entityType, action.payload.entityId, action.payload.tags);

    case _constants.SET_PENDING_DELETION_TAG_ASSOCIATIONS:
      return setPendingDeletions(state, action.payload.entityType, action.payload.entityId, action.payload.tags);

    case _constants.SET_TAG_INPUT:
      return setTagInput(state, action.payload.entityType, action.payload.entityId, action.payload.tagInput);

    case _constants.SET_TAG_SUGGESTIONS:
      return setTagSuggestions(state, action.payload.entityType, action.payload.entityId, action.payload.tags, action.payload.text);

    default:
      return state;
  }
};

exports.default = _default;