'use strict';

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

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 _get2 = require('babel-runtime/helpers/get');

var _get3 = _interopRequireDefault(_get2);

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

var _inherits3 = _interopRequireDefault(_inherits2);

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

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _redux = require('redux');

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

var _scrolling = require('../../../actions/scrolling');

var scrollingActionCreators = _interopRequireWildcard(_scrolling);

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 }; }

var connectToScrollingStore = function connectToScrollingStore(ComposedComponent) {
  var mapDispatchToProps = function mapDispatchToProps(dispatch) {
    return {
      scrollingActions: (0, _redux.bindActionCreators)(scrollingActionCreators, dispatch)
    };
  };

  var mapStateToProps = function mapStateToProps(state) {
    // TODO: change the name in state
    return {
      scrolledComponentKey: state.getIn(['scrolling', 'scrolledComponentKey']),
      scrollingFocus: state.getIn(['scrolling', 'scrollingFocus'])
    };
  };

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

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

  return _temp = _class = function (_ComposedComponent) {
    (0, _inherits3.default)(WithScrollableItems, _ComposedComponent);

    function WithScrollableItems() {
      (0, _classCallCheck3.default)(this, WithScrollableItems);
      return (0, _possibleConstructorReturn3.default)(this, (WithScrollableItems.__proto__ || Object.getPrototypeOf(WithScrollableItems)).apply(this, arguments));
    }

    (0, _createClass3.default)(WithScrollableItems, [{
      key: 'isScrolled',
      value: function isScrolled() {
        return this.props.scrollKey === this.props.scrolledComponentKey;
      }
    }, {
      key: 'scrollIntoView',
      value: function scrollIntoView() {
        var scrollingFocus = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;

        var node = _reactDom2.default.findDOMNode(this);
        node.scrollIntoView();

        if (scrollingFocus) node.focus(); // Focus on the item once scrolled into view
      }
    }, {
      key: 'setFocus',
      value: function setFocus() {
        var node = _reactDom2.default.findDOMNode(this);
        node && node.focus();
      }
    }, {
      key: 'componentDidMount',
      value: function componentDidMount() {
        if (this.props.shouldFocusOnMount) {
          this.setFocus();
        }
      }
    }, {
      key: 'componentDidUpdate',
      value: function componentDidUpdate(prevProps, prevState) {
        var _props = this.props,
            scrolledComponentKey = _props.scrolledComponentKey,
            scrollingFocus = _props.scrollingFocus,
            scrollKey = _props.scrollKey,
            scrollingActions = _props.scrollingActions;


        if (prevProps.scrolledComponentKey !== scrolledComponentKey && scrolledComponentKey === scrollKey) {
          this.scrollIntoView(scrollingFocus);
          scrollingActions.clearsScrolledComponentKey();
        }
        (0, _get3.default)(WithScrollableItems.prototype.__proto__ || Object.getPrototypeOf(WithScrollableItems.prototype), 'componentDidUpdate', this) && (0, _get3.default)(WithScrollableItems.prototype.__proto__ || Object.getPrototypeOf(WithScrollableItems.prototype), 'componentDidUpdate', this).call(this, prevProps, prevState);
      }
    }]);
    return WithScrollableItems;
  }(ComposedComponent), _class.propTypes = Object.assign({}, ComposedComponent.propTypes || {}, {
    scrollKey: _propTypes.PropTypes.string,
    scrolledComponentKey: _propTypes.PropTypes.string,
    scrollingFocus: _propTypes.PropTypes.bool,
    scrollingActions: _propTypes.PropTypes.shape({
      clearsScrolledComponentKey: _propTypes.PropTypes.func.isRequired
    }).isRequired,
    shouldFocusOnMount: _propTypes.PropTypes.bool
  }), _temp;
};

var unconnectedExtension = exports.unconnectedExtension = function unconnectedExtension(givenComponent) {
  return makeScrollable(givenComponent);
};

exports.default = function (givenComponent) {
  return connectToScrollingStore(unconnectedExtension(givenComponent));
};