'use strict';

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

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

var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);

var _utils = require('./utils');

var _actionTypes = require('./action-types');

var actionTypes = _interopRequireWildcard(_actionTypes);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

function getVuexSearchModule(store) {
  var base = (0, _utils.normalizeNamespaceName)(store.search._base);
  return store._modulesNamespaceMap[base];
}

var transformPayload = exports.transformPayload = function transformPayload(resourceName, actionType, args) {
  switch (actionType) {
    case actionTypes.SEARCH:
      {
        var _args = (0, _slicedToArray3.default)(args, 1),
            searchString = _args[0];

        return [{ resourceName: resourceName, searchString: searchString }];
      }
    default:
      return args;
  }
};

exports.default = function (resourceName, actions) {
  var res = {};
  (0, _utils.normalizeMap)(actions).forEach(function (_ref) {
    var key = _ref.key,
        val = _ref.val;

    res[key] = function mappedAction() {
      var module = getVuexSearchModule(this.$store);
      var dispatch = module.context.dispatch;

      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      var payloads = transformPayload(resourceName, val, args);

      return dispatch.apply(this.$store, [val].concat(payloads));
    };
  });

  return res;
};