"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.GraphContainer = void 0;

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

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

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

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

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

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

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

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

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

var _reactRedux = require("react-redux");

var _react = _interopRequireDefault(require("react"));

var _redux = require("redux");

var _reducer = _interopRequireDefault(require("./reducer"));

var _actions = require("./actions");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

var _reduxUndo = require("redux-undo");

var _graphWithControls = _interopRequireDefault(require("../graph-with-controls"));

var _middleware = require("./middleware");

var _excluded = ["onChangeMarks", "marks"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var mapStateToProps = function mapStateToProps(s) {
  return {
    marks: s.marks.present
  };
};

var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    onChangeMarks: function onChangeMarks(m) {
      return dispatch((0, _actions.changeMarks)(m));
    },
    onUndo: function onUndo() {
      return dispatch(_reduxUndo.ActionCreators.undo());
    },
    onRedo: function onRedo() {
      return dispatch(_reduxUndo.ActionCreators.redo());
    },
    onReset: function onReset() {
      return dispatch((0, _actions.changeMarks)([]));
    }
  };
};

var GraphContainer = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(_graphWithControls["default"]);
/**
 * The graph component entry point with undo/redo
 * Redux is an implementation detail, hide it in the react component.
 */

exports.GraphContainer = GraphContainer;

var Root = /*#__PURE__*/function (_React$Component) {
  (0, _inherits2["default"])(Root, _React$Component);

  var _super = _createSuper(Root);

  function Root(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, Root);
    _this = _super.call(this, props);
    (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onStoreChange", function () {
      var _this$props = _this.props,
          marks = _this$props.marks,
          onChangeMarks = _this$props.onChangeMarks;

      var storeState = _this.store.getState();

      if (!(0, _isEqual["default"])(storeState.marks.present, marks)) {
        onChangeMarks(storeState.marks.present);
      }
    });
    var r = (0, _reducer["default"])();
    _this.store = (0, _redux.createStore)(r, {
      marks: props.marks
    }, (0, _redux.applyMiddleware)(_middleware.lastActionMiddleware));

    _this.store.subscribe(_this.onStoreChange);

    return _this;
  }

  (0, _createClass2["default"])(Root, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var marks = this.props.marks;
      var storeState = this.store.getState();

      if ((0, _isEqual["default"])(storeState.marks.present, marks)) {
        return;
      }

      if (!(0, _isEqual["default"])(prevProps.marks, marks)) {
        this.store.dispatch((0, _actions.changeMarks)(marks));
      }
    }
  }, {
    key: "render",
    value: function render() {
      // eslint-disable-next-line no-unused-vars
      var _this$props2 = this.props,
          onChangeMarks = _this$props2.onChangeMarks,
          marks = _this$props2.marks,
          rest = (0, _objectWithoutProperties2["default"])(_this$props2, _excluded);
      var correctnessSet = marks && marks.find(function (m) {
        return m.correctness;
      });

      if (correctnessSet) {
        return /*#__PURE__*/_react["default"].createElement(_graphWithControls["default"], (0, _extends2["default"])({}, rest, {
          marks: marks,
          disabled: correctnessSet
        }));
      }

      return /*#__PURE__*/_react["default"].createElement(_reactRedux.Provider, {
        store: this.store
      }, /*#__PURE__*/_react["default"].createElement(GraphContainer, rest));
    }
  }]);
  return Root;
}(_react["default"].Component);

(0, _defineProperty2["default"])(Root, "propTypes", {
  onChangeMarks: _propTypes["default"].func,
  marks: _propTypes["default"].array
});
var _default = Root;
exports["default"] = _default;
//# sourceMappingURL=index.js.map