"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var _react=_interopRequireWildcard(require("react")),_propTypes=_interopRequireDefault(require("prop-types")),_reactVisibilitySensor=_interopRequireDefault(require("react-visibility-sensor"));function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function _getRequireWildcardCache(){if("function"!=typeof WeakMap)return null;var a=new WeakMap;return _getRequireWildcardCache=function(){return a},a}function _interopRequireWildcard(a){if(a&&a.__esModule)return a;if(null===a||"object"!==_typeof(a)&&"function"!=typeof a)return{default:a};var b=_getRequireWildcardCache();if(b&&b.has(a))return b.get(a);var c={},d=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var e in a)if(Object.prototype.hasOwnProperty.call(a,e)){var f=d?Object.getOwnPropertyDescriptor(a,e):null;f&&(f.get||f.set)?Object.defineProperty(c,e,f):c[e]=a[e]}return c.default=a,b&&b.set(a,c),c}function _typeof(a){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _extends(){return _extends=Object.assign||function(a){for(var b,c=1;c<arguments.length;c++)for(var d in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,d)&&(a[d]=b[d]);return a},_extends.apply(this,arguments)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}function _possibleConstructorReturn(a,b){return b&&("object"===_typeof(b)||"function"==typeof b)?b:_assertThisInitialized(a)}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}var LazyImage=/*#__PURE__*/function(a){function b(a){var c;return _classCallCheck(this,b),c=_possibleConstructorReturn(this,_getPrototypeOf(b).call(this,a)),_defineProperty(_assertThisInitialized(c),"loadImage",function(a){c.image&&(c.image.onload=null,c.image.onerror=null);var b=new Image;c.image=b,b.onload=c.onLoad,b.onerror=c.onError,b.src=a}),_defineProperty(_assertThisInitialized(c),"handleVisibilityChange",function(a){c.setState({isVisible:a},function(){c.state.loading&&c.state.isVisible&&c.loadImage(c.props.src)})}),_defineProperty(_assertThisInitialized(c),"onLoad",function(){c.setState({image:c.image.src,loading:!1})}),_defineProperty(_assertThisInitialized(c),"onError",function(a){var b=c.props.onError;b&&b(a)}),c.image=null,c.state={image:a.placeholder,isVisible:!1,loading:!0},c}return _inherits(b,a),_createClass(b,[{key:"componentDidUpdate",value:function componentDidUpdate(a){var b=this.props,c=b.src,d=b.placeholder;// We only invalidate the current image if the src has changed.
c!==a.src&&(this.state.isVisible?this.loadImage(c):this.setState({image:d,loading:!0}))}},{key:"componentWillUnmount",value:function componentWillUnmount(){this.image&&(this.image.onload=null,this.image.onerror=null)}},{key:"render",value:function render(){var a=this.state,b=a.image,c=a.loading,d=a.isVisible,e=this.props,f=e.children,g=e.visibilitySensorProps;if(!f||"function"!=typeof f)throw new Error("LazyProgressiveImage requires a function as its only child");return _react.default.createElement(_reactVisibilitySensor.default,_extends({},g,{onChange:this.handleVisibilityChange}),f(b,c,d))}}]),b}(_react.Component);LazyImage.propTypes={children:_propTypes.default.func,onError:_propTypes.default.func,placeholder:_propTypes.default.string,src:_propTypes.default.string,visibilitySensorProps:_propTypes.default.any};var _default=LazyImage;exports.default=_default;