'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.deleteTagAssociations = exports.createTagAssociations = exports.getStimulusTagAssociations = exports.getItemTagAssociations = undefined;

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _Fetcher = require('../../common/util/Fetcher');

var _Fetcher2 = _interopRequireDefault(_Fetcher);

var _helpers = require('./helpers');

var _tagAssociations = require('../../common/actions/tagAssociations');

var _constants = require('../../constants');

var _formatMessage = require('../../common/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

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

var getItemTagAssociations = exports.getItemTagAssociations = function getItemTagAssociations(itemId) {
  var url = '/api/items/' + itemId + '/tag_associations';

  return function (dispatch, getState) {
    var fetcher = new _Fetcher2.default({
      callType: _constants.GET_ITEM_TAG_ASSOCIATIONS,
      onError: (0, _helpers.handleTagAssociationError)(dispatch)
    });

    return fetcher.get(url).then(function (response) {
      return dispatch((0, _tagAssociations.setTagAssociations)('Item', itemId, response));
    });
  };
};

var getStimulusTagAssociations = exports.getStimulusTagAssociations = function getStimulusTagAssociations(itemId) {
  var url = '/api/stimuli/' + itemId + '/tag_associations';

  return function (dispatch, getState) {
    var fetcher = new _Fetcher2.default({
      callType: _constants.GET_STIMULUS_TAG_ASSOCIATIONS,
      onError: (0, _helpers.handleTagAssociationError)(dispatch)
    });

    return fetcher.get(url).then(function (response) {
      return dispatch((0, _tagAssociations.setTagAssociations)('Stimulus', itemId, response));
    });
  };
};

var createTagAssociation = function createTagAssociation(dispatch, bankEntryId, entityType, entityId, tagValue) {
  var url = '/api/bank_entries/' + bankEntryId + '/tag_associations';
  var fetcher = new _Fetcher2.default({
    callType: _constants.CREATE_TAG_ASSOCIATION,
    onError: (0, _helpers.handleTagAssociationError)(dispatch, 'Failed to create tag.')
  });
  return fetcher.post(url, {
    body: JSON.stringify({
      tag_value: tagValue
    })
  }).then(function (tagAssociation) {
    return dispatch((0, _tagAssociations.addTagAssociation)(entityType, entityId, tagAssociation));
  });
};

var createTagAssociations = exports.createTagAssociations = function createTagAssociations(bankEntryId, entityType, entityId, tagValues) {
  return function (dispatch, getState) {
    return Promise.all(tagValues.map(function () {
      var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(tagValue) {
        return _regenerator2.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return createTagAssociation(dispatch, bankEntryId, entityType, entityId, tagValue);

              case 2:
                return _context.abrupt('return', dispatch((0, _tagAssociations.removePendingAdditionTagAssociation)(entityType, entityId, tagValue)));

              case 3:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, undefined);
      }));

      return function (_x) {
        return _ref.apply(this, arguments);
      };
    }()));
  };
};

var deleteTagAssociation = function deleteTagAssociation(dispatch, bankEntryId, entityType, entityId, tagAssociationId) {
  var url = '/api/bank_entries/' + bankEntryId + '/tag_associations/' + tagAssociationId;
  var fetcher = new _Fetcher2.default({
    callType: _constants.DELETE_TAG_ASSOCIATION,
    onError: (0, _helpers.handleTagAssociationError)(dispatch, 'Failed to delete tag.')
  });
  return fetcher.delete(url).then(function (response) {
    return dispatch([(0, _tagAssociations.removeTagAssociation)(entityType, entityId, tagAssociationId)]);
  });
};

var deleteTagAssociations = exports.deleteTagAssociations = function deleteTagAssociations(bankEntryId, entityType, entityId, tagAssociationIds) {
  return function (dispatch, getState) {
    return Promise.all(tagAssociationIds.map(function () {
      var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(tagAssociationId) {
        return _regenerator2.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return deleteTagAssociation(dispatch, bankEntryId, entityType, entityId, tagAssociationId);

              case 2:
                return _context2.abrupt('return', dispatch((0, _tagAssociations.removePendingDeletionTagAssociation)(entityType, entityId, tagAssociationId)));

              case 3:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, undefined);
      }));

      return function (_x2) {
        return _ref2.apply(this, arguments);
      };
    }()));
  };
};