'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.removeBankEntry = exports.updateBankEntry = exports.createBankEntry = exports.createEntryAndBankEntry = exports.cloneBankEntry = exports.getBankEntry = exports.getBankEntries = undefined;

var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

var _ui = require('../../common/actions/ui');

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

var _Fetcher2 = _interopRequireDefault(_Fetcher);

var _PaginatedFetcher = require('../../common/util/PaginatedFetcher');

var _PaginatedFetcher2 = _interopRequireDefault(_PaginatedFetcher);

var _banks = require('../../common/actions/banks');

var _callHandlers = require('../../common/api/callHandlers');

var _bankEntries = require('../../common/actions/bankEntries');

var _banks2 = require('./banks');

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

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

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

var getBankEntries = exports.getBankEntries = function getBankEntries(bankId, useBankSearch) {
  var params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

  var basePath = '/api/banks/' + bankId + '/bank_entries';
  var searchPath = basePath + '/search';

  return function (dispatch, getState) {
    var getSearchIds = function getSearchIds(json) {
      return json.entries.map(function (e) {
        return e.id;
      });
    };
    var getRecordIds = useBankSearch ? getSearchIds : null;
    var fetcher = new _PaginatedFetcher2.default({
      callType: _constants.GET_BANK_ENTRIES_CALL,
      onError: (0, _helpers.handleBankError)(dispatch),
      getRecordIds: getRecordIds
    });

    var path = useBankSearch ? searchPath : basePath;
    return fetcher.get(path, params).then(function (r) {
      if (useBankSearch) {
        dispatch([(0, _banks.setBankFilters)(bankId, r[0].filters)]);
        return [r[0].entries, r[1]];
      } else {
        return r;
      }
    }).then(function (_ref) {
      var _ref2 = (0, _slicedToArray3.default)(_ref, 2),
          response = _ref2[0],
          pageData = _ref2[1];

      dispatch([(0, _callHandlers.handleBankEntriesResponse)(response), (0, _banks.associateEntries)(bankId, response.map(function (be) {
        return be.id;
      }))]);
      return pageData;
    });
  };
};

var getBankEntry = exports.getBankEntry = function getBankEntry(bankId, bankEntryId) {
  var url = '/api/banks/' + bankId + '/bank_entries/' + bankEntryId;
  return function (dispatch, getState) {
    var fetcher = new _Fetcher2.default({
      callType: _constants.GET_BANK_ENTRY_CALL,
      onError: (0, _helpers.handleBankError)(dispatch)
    });
    return fetcher.get(url).then(function (response) {
      return dispatch([(0, _callHandlers.handleBankEntryResponse)(response)]);
    });
  };
};

var cloneBankEntry = exports.cloneBankEntry = function cloneBankEntry(bankEntry, createEntryCb) {
  var updateBankEntryCb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : updateBankEntry;

  var entry = bankEntry.getEntry();
  var workingEntry = entry.getWorkingInstance();
  var bankId = bankEntry.bankId;
  return function (dispatch, getState) {
    return dispatch(createEntryCb(workingEntry, bankId, 'bank', entry.id)).then(function (entryData) {
      return dispatch(updateBankEntryCb(bankId, bankEntry.id, { entryId: entryData.id }));
    });
  };
};

var createEntryAndBankEntry = exports.createEntryAndBankEntry = function createEntryAndBankEntry(bankId, entry, bankEntry, createEntryCb) {
  var createBankEntryCb = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : createBankEntry;

  return function (dispatch, getState) {
    return dispatch(createEntryCb(entry, bankId, 'bank')).then(function (entryData) {
      return dispatch(createBankEntryCb(bankId, bankEntry.entryType, entryData.id));
    });
  };
};

var createBankEntry = exports.createBankEntry = function createBankEntry(bankId, entryType, entryId) {
  var url = '/api/banks/' + bankId + '/bank_entries';
  return function (dispatch, getState) {
    var newBankEntryData = JSON.stringify({
      bank_entry: {
        bank_id: bankId,
        entry_type: entryType,
        entry_id: entryId
      }
    });

    var fetcher = new _Fetcher2.default({
      callType: _constants.CREATE_BANK_ENTRY_CALL,
      onError: (0, _helpers.handleBankError)(dispatch)
    });
    return fetcher.post(url, { body: newBankEntryData }).then(function (response) {
      dispatch([(0, _callHandlers.handleBankEntryResponse)(response), (0, _banks.associateEntries)(bankId, [response.id]), (0, _ui.clear)(_constants.BANKS_BANK_ENTRY_PAGINATION + ':' + bankId), (0, _banks2.getBank)(bankId)]);
      return response;
    });
  };
};

var updateBankEntry = exports.updateBankEntry = function updateBankEntry(bankId, bankEntryId, params) {
  var url = '/api/banks/' + bankId + '/bank_entries/' + bankEntryId;
  return function (dispatch, getState) {
    var fetcher = new _Fetcher2.default({
      callType: _constants.UPDATE_BANK_ENTRY_CALL,
      onError: (0, _helpers.handleBankError)(dispatch)
    });
    var reqParams = { bank_id: params.bankId, entry_id: params.entryId };
    var updateBankEntryParams = {
      body: JSON.stringify({ bank_entry: reqParams })
    };
    return fetcher.patch(url, updateBankEntryParams).then(function (response) {
      dispatch([(0, _callHandlers.handleBankEntryResponse)(response), (0, _banks.associateEntries)(response.bank_id, [response.id])]);
      if (params.bankId && bankId !== params.bankId) {
        dispatch([(0, _ui.clear)(_constants.BANKS_BANK_ENTRY_PAGINATION + ':' + bankId), (0, _ui.clear)(_constants.BANKS_BANK_ENTRY_PAGINATION + ':' + params.bankId), (0, _banks2.getBank)(bankId), (0, _banks2.getBank)(params.bankId)]);
      }
      return response;
    });
  };
};

var removeBankEntry = exports.removeBankEntry = function removeBankEntry(bankId, bankEntryId) {
  var url = '/api/banks/' + bankId + '/bank_entries/' + bankEntryId;
  return function (dispatch, getState) {
    var fetcher = new _Fetcher2.default({
      callType: _constants.REMOVE_BANK_ENTRY_CALL,
      onError: (0, _helpers.handleBankError)(dispatch)
    });
    return fetcher.delete(url).then(function (response) {
      return dispatch([(0, _banks.disassociateEntry)(bankId, bankEntryId), (0, _ui.clear)(_constants.BANKS_BANK_ENTRY_PAGINATION + ':' + bankId), (0, _banks2.getBank)(bankId), (0, _bankEntries.archiveBankEntry)(bankEntryId)]);
    });
  };
};