import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _get from "lodash/get";
import { applyMiddleware, createStore, compose } from "redux";
import invariant from "invariant";
import { getPlugins } from "../../plugins";
import createRootReducer from "./createRootReducer";
import createMiddleware, { wrapMiddleware } from "./createMiddleware";
export { connect } from "./connect";
export var Redux = /*#__PURE__*/function () {
  function Redux() {
    _classCallCheck(this, Redux);

    _defineProperty(this, "actionMeta", void 0);

    _defineProperty(this, "store", void 0);

    _defineProperty(this, "middleware", void 0);

    _defineProperty(this, "reducers", void 0);

    _defineProperty(this, "higherOrderReducers", void 0);

    this.reducers = [];
    this.higherOrderReducers = [];
    this.middleware = [];
    this.actionMeta = {};
  }

  _createClass(Redux, [{
    key: "createAction",
    value: function createAction(type) {
      var _this = this;

      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      return function () {
        var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        var meta = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
        return {
          type: type,
          payload: payload,
          meta: _objectSpread(_objectSpread({}, _this.actionMeta), {}, {
            log: options.hasOwnProperty("log") ? options.log : true
          }, meta)
        };
      };
    }
  }, {
    key: "addReducer",
    value: function addReducer(actions, statePath, reducer) {
      invariant(reducer, "Must pass a valid reducer function!");
      this.reducers.push({
        actions: actions,
        statePath: statePath,
        reducer: reducer
      });
    }
  }, {
    key: "addHigherOrderReducer",
    value: function addHigherOrderReducer(actions, statePath, factory) {
      this.higherOrderReducers.push({
        actions: actions,
        statePath: statePath,
        reducer: factory()
      });
    }
  }, {
    key: "addMiddleware",
    value: function addMiddleware(actions, middleware) {
      this.middleware.push({
        actions: actions,
        middleware: middleware
      });
    }
  }, {
    key: "initStore",
    value: function initStore() {
      var INIT_STATE = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var actionMeta = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      this.actionMeta = actionMeta; // dev tool
      // @ts-ignore

      var reduxDevTools = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
      var composeEnhancers = reduxDevTools && reduxDevTools({
        predicate: function predicate(state, action) {
          return _get(action, "meta.log", true);
        }
      }) || compose;
      var middlewareFromPlugins = getPlugins("pb-editor-redux-middleware").map(function (pl) {
        return wrapMiddleware(pl.middleware, Array.isArray(pl.actions) ? pl.actions : null);
      }).reverse();
      this.store = createStore(createRootReducer(INIT_STATE, this), composeEnhancers(applyMiddleware.apply(void 0, _toConsumableArray(middlewareFromPlugins).concat(_toConsumableArray(createMiddleware(this))))));
      return this.store;
    }
  }]);

  return Redux;
}();
var redux = new Redux();
export { redux };
export var createAction = function createAction(type, options) {
  return redux.createAction(type, options);
};
export var addReducer = function addReducer(actions, statePath, reducer) {
  redux.addReducer(actions, statePath, reducer);
};
export var addHigherOrderReducer = function addHigherOrderReducer(actions, statePath, factory) {
  redux.addHigherOrderReducer(actions, statePath, factory);
};
export var addMiddleware = function addMiddleware(actions, middleware) {
  redux.addMiddleware(actions, middleware);
};
export var dispatch = function dispatch(action) {
  return redux.store.dispatch(action);
};
//# sourceMappingURL=index.js.map