'use strict';

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _relation = require('../actions/relation');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var handleRelationFindFulfilled = function handleRelationFindFulfilled(state, action) {
  var _action$meta = action.meta,
      subject = _action$meta.subject,
      object = _action$meta.object,
      predicate = _action$meta.predicate;


  return state.setIn(['find', subject.csid, object.csid, predicate, 'result'], _immutable2.default.fromJS(action.payload.data));
};

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

  switch (action.type) {
    case _relation.CLEAR_RELATION_STATE:
      return state.clear();
    case _relation.RELATION_FIND_FULFILLED:
      return handleRelationFindFulfilled(state, action);
    case _relation.SUBJECT_RELATIONS_UPDATED:
      return state.deleteIn(['find', action.meta.subject.csid]);
    default:
      return state;
  }
};

var getFindResult = exports.getFindResult = function getFindResult(state, subject, object, predicate) {
  return state.getIn(['find', subject.csid, object.csid, predicate, 'result']);
};