"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports["default"]=void 0;var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")),_createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass")),_assertThisInitialized2=_interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")),_inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits")),_possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")),_getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")),_defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty")),_react=_interopRequireDefault(require("react")),_recompose=require("recompose"),_propTypes=_interopRequireDefault(require("prop-types"));function _createSuper(Derived){var hasNativeReflectConstruct=_isNativeReflectConstruct();return function(){var result,Super=(0,_getPrototypeOf2["default"])(Derived);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("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],function(){})),!0}catch(e){return!1}}var ScrollObserver=/*#__PURE__*/function(_React$Component){function ScrollObserver(){var _this;(0,_classCallCheck2["default"])(this,ScrollObserver);for(var _len=arguments.length,args=Array(_len),_key=0;_key<_len;_key++)args[_key]=arguments[_key];return _this=_super.call.apply(_super,[this].concat(args)),(0,_defineProperty2["default"])((0,_assertThisInitialized2["default"])(_this),"el",void 0),(0,_defineProperty2["default"])((0,_assertThisInitialized2["default"])(_this),"isBottomReached",function(){var _this$props=_this.props,setShouldSendBottomEvent=_this$props.setShouldSendBottomEvent,shouldSendBottomEvent=_this$props.shouldSendBottomEvent,onBottomReachedThreshold=_this$props.onBottomReachedThreshold;if(_this.el){var bottom=_this.el.getBoundingClientRect().bottom,offset=onBottomReachedThreshold;return bottom>window.innerHeight+offset&&!shouldSendBottomEvent&&setShouldSendBottomEvent(!0),bottom<=window.innerHeight+offset}}),(0,_defineProperty2["default"])((0,_assertThisInitialized2["default"])(_this),"isTopReached",function(){var _this$props2=_this.props,setShouldSendTopEvent=_this$props2.setShouldSendTopEvent,onTopReachedThreshold=_this$props2.onTopReachedThreshold,shouldSendTopEvent=_this$props2.shouldSendTopEvent;if(_this.el){var top=_this.el.getBoundingClientRect().top,offset=onTopReachedThreshold;return 0>top+offset&&!shouldSendTopEvent&&setShouldSendTopEvent(!0),0<=top+offset}}),(0,_defineProperty2["default"])((0,_assertThisInitialized2["default"])(_this),"observeScrolling",function(){var _this$props3=_this.props,onBottomReached=_this$props3.onBottomReached,onTopReached=_this$props3.onTopReached,setShouldSendBottomEvent=_this$props3.setShouldSendBottomEvent,setShouldSendTopEvent=_this$props3.setShouldSendTopEvent,shouldSendTopEvent=_this$props3.shouldSendTopEvent,shouldSendBottomEvent=_this$props3.shouldSendBottomEvent;onBottomReached&&_this.isBottomReached()&&shouldSendBottomEvent&&(onBottomReached(_this.el),setShouldSendBottomEvent(!1)),onTopReached&&_this.isTopReached()&&shouldSendTopEvent&&(onTopReached(_this.el),setShouldSendTopEvent(!1))}),_this}(0,_inherits2["default"])(ScrollObserver,_React$Component);var _super=_createSuper(ScrollObserver);return(0,_createClass2["default"])(ScrollObserver,[{key:"componentDidMount",value:function componentDidMount(){this.observeScrolling(),document.addEventListener("scroll",this.observeScrolling)}},{key:"componentWillUnmount",value:function componentWillUnmount(){document.removeEventListener("scroll",this.observeScrolling)}},{key:"render",value:function render(){var _this2=this;return/*#__PURE__*/_react["default"].createElement("span",{ref:function ref(_ref){return _this2.el=_ref}},this.props.children)}}]),ScrollObserver}(_react["default"].Component);(0,_defineProperty2["default"])(ScrollObserver,"propTypes",{onBottomReached:_propTypes["default"].func,onBottomReachedThreshold:_propTypes["default"].number,onTopReached:_propTypes["default"].func,onTopReachedThreshold:_propTypes["default"].number}),(0,_defineProperty2["default"])(ScrollObserver,"defaultProps",{onBottomReachedThreshold:0,onTopReachedThreshold:0});var _default=(0,_recompose.compose)((0,_recompose.withState)("shouldSendBottomEvent","setShouldSendBottomEvent",!0),(0,_recompose.withState)("shouldSendTopEvent","setShouldSendTopEvent",!0))(ScrollObserver);exports["default"]=_default;