'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _immutable = require('immutable');

var _constants = require('../../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 defaultState = (0, _immutable.fromJS)({});

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

  switch (action.type) {
    case _constants.SET_TAG_ASSOCIATIONS:
      return setTagAssociations(state, action.payload.entityType, action.payload.entityId, action.payload.tagAssociations);
    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);
    default:
      return state;
  }
};