"use strict";

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require("react");

var React = _interopRequireWildcard(_react);

var _reactVisibilitySensor = require("react-visibility-sensor");

var _reactVisibilitySensor2 = _interopRequireDefault(_reactVisibilitySensor);

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

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var LazyImage = function (_React$Component) {
  _inherits(LazyImage, _React$Component);

  function LazyImage(props) {
    _classCallCheck(this, LazyImage);

    var _this = _possibleConstructorReturn(this, (LazyImage.__proto__ || Object.getPrototypeOf(LazyImage)).call(this, props));

    _this.loadImage = function (src) {
      // If there is already an image we nullify the onload
      // and onerror props so it does not incorrectly set state
      // when it resolves
      if (_this.image) {
        _this.image.onload = null;
        _this.image.onerror = null;
      }
      var image = new Image();
      _this.image = image;
      image.onload = _this.onLoad;
      image.onerror = _this.onError;
      image.src = src;
    };

    _this.handleVisibilityChange = function (isVisible) {
      _this.setState({
        isVisible: isVisible
      }, function () {
        // load the image if it is visible and if it isn't already loaded
        if (_this.state.loading && _this.state.isVisible) {
          _this.loadImage(_this.props.src);
        }
      });
    };

    _this.onLoad = function () {
      // use this.image.src instead of this.props.src to
      // avoid the possibility of props being updated and the
      // new image loading before the new props are available as
      // this.props.
      _this.setState({
        image: _this.image.src,
        loading: false
      });
    };

    _this.onError = function (errorEvent) {
      var onError = _this.props.onError;

      if (onError) {
        onError(errorEvent);
      }
    };

    _this.state = {
      image: props.placeholder,
      isVisible: false,
      loading: true
    };
    return _this;
  }
  //
  // componentDidMount() {
  //   const { src } = this.props;
  //   this.loadImage(src);
  // }

  _createClass(LazyImage, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(nextProps) {
      var _this2 = this;

      var src = nextProps.src,
          placeholder = nextProps.placeholder;
      // We only invalidate the current image if the src has changed.

      if (src !== this.props.src) {
        this.setState({ image: placeholder, loading: true }, function () {
          // load the image if in viewport
          if (_this2.state.isVisible) {
            _this2.loadImage(src);
          }
        });
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      if (this.image) {
        this.image.onload = null;
        this.image.onerror = null;
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _state = this.state,
          image = _state.image,
          loading = _state.loading,
          isVisible = _state.isVisible;
      var _props = this.props,
          children = _props.children,
          visibilitySensorProps = _props.visibilitySensorProps;

      if (!children || typeof children !== "function") {
        throw new Error("LazyProgressiveImage requires a function as its only child");
      }
      return React.createElement(
        _reactVisibilitySensor2.default,
        _extends({}, visibilitySensorProps, {
          onChange: this.handleVisibilityChange
        }),
        children(image, loading, isVisible)
      );
    }
  }]);

  return LazyImage;
}(React.Component);

exports.default = LazyImage;