"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.createStore = exports.escapeRegExp = exports.filterStr = exports.createRegExp = exports.PARAMS = exports.PAGINATION = exports.FILTER = exports.SORT = exports.LOAD_ERROR = exports.LOAD_SUCCESS = exports.LOAD = exports.AVAILABLE_PER_PAGE = exports.ALL_PAGES = exports.PersistGate = void 0;

var _reselect = require("reselect");

var _redux = require("redux");

var _reduxDevtoolsExtension = require("redux-devtools-extension");

var _reduxThunk = _interopRequireDefault(require("redux-thunk"));

var _reduxLogger = require("redux-logger");

var _reduxPromiseMiddleware = _interopRequireDefault(require("redux-promise-middleware"));

var _reduxPersist = require("redux-persist");

var _storage = _interopRequireDefault(require("redux-persist/lib/storage"));

var _react = require("redux-persist/lib/integration/react");

exports.PersistGate = _react.PersistGate;
var ALL_PAGES = -1;
exports.ALL_PAGES = ALL_PAGES;
var AVAILABLE_PER_PAGE = [5, 10, 25, 50, ALL_PAGES];
exports.AVAILABLE_PER_PAGE = AVAILABLE_PER_PAGE;
var LOAD = 'LOAD';
exports.LOAD = LOAD;
var LOAD_SUCCESS = 'LOAD_SUCCESS';
exports.LOAD_SUCCESS = LOAD_SUCCESS;
var LOAD_ERROR = 'LOAD_ERROR';
exports.LOAD_ERROR = LOAD_ERROR;
var SORT = 'SORT';
exports.SORT = SORT;
var FILTER = 'FILTER';
exports.FILTER = FILTER;
var PAGINATION = 'PAGINATION';
exports.PAGINATION = PAGINATION;
var PARAMS = 'PARAMS';
exports.PARAMS = PARAMS;
var createRegExp = (0, _reselect.createSelector)(function (filter) {
  return filter;
}, function (filter) {
  return new RegExp('(' + filter.split(' ').map(escapeRegExp).join('|') + ')', 'igm');
});
exports.createRegExp = createRegExp;

var filterStr = function filterStr(str, filter) {
  return createRegExp(filter).test(str);
};

exports.filterStr = filterStr;

var escapeRegExp = function escapeRegExp(s) {
  return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
};

exports.escapeRegExp = escapeRegExp;

var createStore = function createStore(_ref) {
  var _ref$initialState = _ref.initialState,
      initialState = _ref$initialState === void 0 ? undefined : _ref$initialState,
      _ref$reducer = _ref.reducer,
      reducer = _ref$reducer === void 0 ? null : _ref$reducer,
      _ref$reducers = _ref.reducers,
      reducers = _ref$reducers === void 0 ? null : _ref$reducers,
      _ref$keysToPersist = _ref.keysToPersist,
      keysToPersist = _ref$keysToPersist === void 0 ? [] : _ref$keysToPersist,
      _ref$storagePrefix = _ref.storagePrefix,
      storagePrefix = _ref$storagePrefix === void 0 ? 'wmc' : _ref$storagePrefix,
      _ref$promiseTypeSuffi = _ref.promiseTypeSuffixes,
      promiseTypeSuffixes = _ref$promiseTypeSuffi === void 0 ? ['REQUEST', 'SUCCESS', 'ERROR'] : _ref$promiseTypeSuffi,
      _ref$collapseLogs = _ref.collapseLogs,
      collapseLogs = _ref$collapseLogs === void 0 ? true : _ref$collapseLogs,
      _ref$middlewares = _ref.middlewares,
      middlewares = _ref$middlewares === void 0 ? [] : _ref$middlewares;

  if (!reducer && !reducers) {
    throw new Error('Neither reducer nor reducers were provided');
  }

  if (reducer && reducers) {
    throw new Error('Both reducer and reducers were provided, you need to pick one or the other');
  }

  if (reducers) {
    reducer = (0, _reduxPersist.persistCombineReducers)({
      debug: true,
      key: storagePrefix,
      whitelist: keysToPersist,
      storage: _storage.default
    }, reducers);
  }

  var predefinedMiddlewares = [_reduxThunk.default, (0, _reduxPromiseMiddleware.default)({
    promiseTypeSuffixes: promiseTypeSuffixes
  }), (0, _reduxLogger.createLogger)({
    level: process.env.NODE_ENV !== 'production' ? 'log' : 'error',
    collapsed: function collapsed(getState, action, logEntry) {
      return !logEntry.error || collapseLogs;
    },
    diff: true
  })].concat(middlewares);
  var store = (0, _redux.createStore)(reducer, initialState, (0, _reduxDevtoolsExtension.composeWithDevTools)(_redux.applyMiddleware.apply(void 0, predefinedMiddlewares)));

  if (!reducers) {
    return store;
  }

  var persistor = (0, _reduxPersist.persistStore)(store);
  return {
    persistor: persistor,
    store: store
  };
};

exports.createStore = createStore;