"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

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

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

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

var _immutable = require("immutable");

var _reactImmutableProptypes = _interopRequireDefault(require("react-immutable-proptypes"));

var _extend = _interopRequireDefault(require("lodash/extend"));

var _uniqueId = _interopRequireDefault(require("lodash/uniqueId"));

var _omit = _interopRequireDefault(require("lodash/omit"));

var _redux = require("redux");

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

var editingActionCreators = _interopRequireWildcard(require("../../../actions/editing"));

// ==============================================================
// two components which are composed to enable editing actions
// component one: connect to redux store for state & action creators
// component two: filter state, apply unique component ID to
// actions, render component
// ==============================================================
var connectToEditStore = function connectToEditStore(ComposedComponent) {
  var mapDispatchToProps = function mapDispatchToProps(dispatch) {
    return {
      editingActions: (0, _redux.bindActionCreators)(editingActionCreators, dispatch)
    };
  };

  var mapStateToProps = function mapStateToProps(state) {
    return {
      editing: state.get('editing')
    };
  };

  return (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(ComposedComponent);
};

var giveEditProps = function giveEditProps(ComposedComponent) {
  var _class, _temp;

  return _temp = _class =
  /*#__PURE__*/
  function (_Component) {
    (0, _inherits2.default)(_class, _Component);

    function _class() {
      var _getPrototypeOf2;

      var _this;

      (0, _classCallCheck2.default)(this, _class);

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

      _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(_class)).call.apply(_getPrototypeOf2, [this].concat(args)));

      _this.switchOnEditing = function (initialFocusId) {
        _this.props.editingActions.startEdit(_this.uniqueId(), initialFocusId);
      };

      _this.switchOffEditing = function () {
        _this.props.editingActions.endEdit(_this.uniqueId());
      };

      _this.toggleEditing = function () {
        _this.props.editingActions.toggleEditing(_this.uniqueId());
      };

      return _this;
    }

    (0, _createClass2.default)(_class, [{
      key: "componentWillMount",
      value: function componentWillMount() {
        this._uniqId = (0, _uniqueId.default)("".concat(ComposedComponent.displayName, "_"));
      }
    }, {
      key: "uniqueId",
      value: function uniqueId() {
        return this.props.guid || this._uniqId;
      }
    }, {
      key: "propsForChild",
      value: function propsForChild() {
        var uniqueId = this.uniqueId();
        var editingProps = {
          isEditing: !!this.props.editing.get(uniqueId),
          editingKey: uniqueId,
          switchOnEditing: this.switchOnEditing,
          switchOffEditing: this.switchOffEditing,
          toggleEditing: this.toggleEditing // Don't inject the `editing` prop in, or else all editable
          // components will have to rerender when one switches editing
          // on/off.

        };
        return (0, _omit.default)((0, _extend.default)({}, editingProps, this.props), ['editing']);
      }
    }, {
      key: "render",
      value: function render() {
        return _react.default.createElement(ComposedComponent, this.propsForChild());
      }
    }]);
    _class.displayName = "_class";
    return _class;
  }(_react.Component), _class.propTypes = {
    editing: _reactImmutableProptypes.default.map,
    editingActions: _propTypes.default.shape({
      startEdit: _propTypes.default.func.isRequired,
      endEdit: _propTypes.default.func.isRequired,
      toggleEditing: _propTypes.default.func.isRequired
    }).isRequired,
    guid: _propTypes.default.string
  }, _class.displayName = ComposedComponent.displayName, _class.defaultProps = {
    editing: (0, _immutable.fromJS)({}),
    guid: null
  }, _temp;
};

var _default = function _default(givenComponent) {
  return connectToEditStore(giveEditProps(givenComponent));
};

exports.default = _default;