import { createSelector } from 'reselect';
import { applyMiddleware, createStore as _createStore } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import thunkMiddleware from 'redux-thunk';
import { createLogger } from 'redux-logger';
import promiseMiddleware from 'redux-promise-middleware';
import { persistCombineReducers, persistStore } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
export { PersistGate } from 'redux-persist/lib/integration/react';
export var ALL_PAGES = -1;
export var AVAILABLE_PER_PAGE = [5, 10, 25, 50, ALL_PAGES];
export var LOAD = 'LOAD';
export var LOAD_SUCCESS = 'LOAD_SUCCESS';
export var LOAD_ERROR = 'LOAD_ERROR';
export var SORT = 'SORT';
export var FILTER = 'FILTER';
export var PAGINATION = 'PAGINATION';
export var PARAMS = 'PARAMS';
export var createRegExp = createSelector(function (filter) {
  return filter;
}, function (filter) {
  return new RegExp('(' + filter.split(' ').map(escapeRegExp).join('|') + ')', 'igm');
});
export var filterStr = function filterStr(str, filter) {
  return createRegExp(filter).test(str);
};
export var escapeRegExp = function escapeRegExp(s) {
  return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
};
export 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 = persistCombineReducers({
      debug: true,
      key: storagePrefix,
      whitelist: keysToPersist,
      storage: storage
    }, reducers);
  }

  var predefinedMiddlewares = [thunkMiddleware, promiseMiddleware({
    promiseTypeSuffixes: promiseTypeSuffixes
  }), 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 = _createStore(reducer, initialState, composeWithDevTools(applyMiddleware.apply(void 0, predefinedMiddlewares)));

  if (!reducers) {
    return store;
  }

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