'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _reactImmutableProptypes = require('react-immutable-proptypes');

var _reactImmutableProptypes2 = _interopRequireDefault(_reactImmutableProptypes);

var _extend = require('lodash/extend');

var _extend2 = _interopRequireDefault(_extend);

var _uniqueId = require('lodash/uniqueId');

var _uniqueId2 = _interopRequireDefault(_uniqueId);

var _omit = require('lodash/omit');

var _omit2 = _interopRequireDefault(_omit);

var _redux = require('redux');

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

var _editing = require('../../../actions/editing');

var editingActionCreators = _interopRequireWildcard(_editing);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// ==============================================================

// 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, _temp2;

  return _temp2 = _class = function (_Component) {
    (0, _inherits3.default)(_class, _Component);

    function _class() {
      var _ref;

      var _temp, _this, _ret;

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

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

      return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = _class.__proto__ || Object.getPrototypeOf(_class)).call.apply(_ref, [this].concat(args))), _this), _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());
      }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
    }

    (0, _createClass3.default)(_class, [{
      key: 'componentWillMount',
      value: function componentWillMount() {
        this._uniqId = (0, _uniqueId2.default)(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, _omit2.default)((0, _extend2.default)({}, editingProps, this.props), ['editing']);
      }
    }, {
      key: 'render',
      value: function render() {
        return _react2.default.createElement(ComposedComponent, this.propsForChild());
      }
    }]);
    _class.displayName = '_class'
    ;
    return _class;
  }(_react.Component), _class.propTypes = {
    editing: _reactImmutableProptypes2.default.map,
    editingActions: _propTypes2.default.shape({
      startEdit: _propTypes2.default.func.isRequired,
      endEdit: _propTypes2.default.func.isRequired,
      toggleEditing: _propTypes2.default.func.isRequired
    }).isRequired,
    guid: _propTypes2.default.string
  }, _class.displayName = ComposedComponent.displayName, _class.defaultProps = {
    editing: (0, _immutable.fromJS)({}),
    guid: null
  }, _temp2;
};

exports.default = function (givenComponent) {
  return connectToEditStore(giveEditProps(givenComponent));
};