"use strict";

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

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.unconnectedExtension = 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 _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

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

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

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

var _reactDom = _interopRequireDefault(require("react-dom"));

var _redux = require("redux");

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

var scrollingActionCreators = _interopRequireWildcard(require("../../../actions/scrolling"));

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 =
  /*#__PURE__*/
  function (_ComposedComponent) {
    (0, _inherits2.default)(WithScrollableItems, _ComposedComponent);

    function WithScrollableItems() {
      (0, _classCallCheck2.default)(this, WithScrollableItems);
      return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(WithScrollableItems).apply(this, arguments));
    }

    (0, _createClass2.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 = _reactDom.default.findDOMNode(this);

        node.scrollIntoView();
        if (scrollingFocus) node.focus(); // Focus on the item once scrolled into view
      }
    }, {
      key: "setFocus",
      value: function setFocus() {
        var node = _reactDom.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 _this$props = this.props,
            scrolledComponentKey = _this$props.scrolledComponentKey,
            scrollingFocus = _this$props.scrollingFocus,
            scrollKey = _this$props.scrollKey,
            scrollingActions = _this$props.scrollingActions;

        if (prevProps.scrolledComponentKey !== scrolledComponentKey && scrolledComponentKey === scrollKey) {
          this.scrollIntoView(scrollingFocus);
          scrollingActions.clearsScrolledComponentKey();
        }

        (0, _get2.default)((0, _getPrototypeOf2.default)(WithScrollableItems.prototype), "componentDidUpdate", this) && (0, _get2.default)((0, _getPrototypeOf2.default)(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 = function unconnectedExtension(givenComponent) {
  return makeScrollable(givenComponent);
};

exports.unconnectedExtension = unconnectedExtension;

var _default = function _default(givenComponent) {
  return connectToScrollingStore(unconnectedExtension(givenComponent));
};

exports.default = _default;