text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.NotifyResize = undefined;
  
  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 _react2 = _interopRequireDefault(_react);
  
  var _reactDom = require('react-dom');
  
  var _propTypes = require('prop-types');
  
  var _shallowequal = require('../../common/shallowequal');
  
  var _shallowequal2 = _interopRequireDefault(_shallowequal);
  
  var _debounce = require('../../common/debounce');
  
  var _debounce2 = _interopRequireDefault(_debounce);
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  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; } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  * Copyright (c) 2015-present, Zippy Technologies
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  *
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  * This source code is licensed under the MIT license found in the
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  * LICENSE file in the root directory of this source tree.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  */
  
  var RENDER_HAS_NATIVE_RESIZE_OBSERVER = _react2.default.createElement('div', {
    style: { display: 'none' },
    'data-name': '@zippytech/react-toolkit/resize-observer-placeholder'
  });
  
  var emptyFn = function emptyFn() {};
  var immediateFn = function immediateFn(fn) {
    return fn();
  };
  
  var notifyResizeStyle = {
    contain: 'strict',
    position: 'absolute',
    top: 0,
    left: 0,
    width: '100%',
    height: '100%',
    zIndex: -1,
    overflow: 'hidden',
    display: 'block',
    pointerEvents: 'none',
    opacity: 0,
    direction: 'ltr',
    textAlign: 'start'
  };
  
  var expandToolStyle = {
    contain: 'strict',
    position: 'absolute',
    top: 0,
    left: 0,
    width: '100%',
    height: '100%',
    overflow: 'auto'
  };
  
  var contractToolStyle = {
    contain: 'strict',
    position: 'absolute',
    top: 0,
    left: 0,
    width: '100%',
    height: '100%',
    overflow: 'auto'
  };
  
  var contractToolInnerStyle = {
    contain: 'strict',
    position: 'absolute',
    top: 0,
    left: 0,
    width: '200%',
    height: '200%'
  };
  
  var ZippyNotifyResize = function (_React$Component) {
    _inherits(ZippyNotifyResize, _React$Component);
  
    function ZippyNotifyResize(props) {
      _classCallCheck(this, ZippyNotifyResize);
  
      var _this = _possibleConstructorReturn(this, (ZippyNotifyResize.__proto__ || Object.getPrototypeOf(ZippyNotifyResize)).call(this, props));
  
      _this.checkResize = _this.checkResize.bind(_this);
      _this.onResize = _this.onResize.bind(_this);
  
      if (props.notifyResizeDelay > 0) {
        _this.onResize = (0, _debounce2.default)(_this.onResize, props.notifyResizeDelay);
      }
  
      if (props.checkResizeDelay > 0) {
        _this.checkResize = (0, _debounce2.default)(_this.checkResize, props.checkResizeDelay);
      }
  
      _this.refNotifyResize = function (node) {
        _this.notifyResizeNode = node;
      };
      _this.refContractTool = function (node) {
        _this.contractToolNode = node;
      };
      _this.refExpandTool = function (node) {
        _this.expandToolNode = node;
      };
      _this.refExpandToolInner = function (node) {
        _this.expandToolInnerNode = node;
      };
  
      _this.state = {
        notifyResizeWidth: 0,
        notifyResizeHeight: 0,
  
        expandToolWidth: 0,
        expandToolHeight: 0,
  
        contractToolWidth: 0,
        contractToolHeight: 0
      };
      return _this;
    }
  
    _createClass(ZippyNotifyResize, [{
      key: 'shouldComponentUpdate',
      value: function shouldComponentUpdate(nextProps, nextState) {
        if (typeof nextProps.shouldComponentUpdate === 'function') {
          return nextProps.shouldComponentUpdate(nextProps, this.props, nextState, this.state);
        }
  
        return !(0, _shallowequal2.default)(nextState, this.state) || !(0, _shallowequal2.default)(nextProps, this.props);
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        this.__willUnmount = true;
  
        if (this.observer) {
          if (this.observer.unobserve) {
            this.observer.unobserve(this.target);
          }
          if (this.observer.disconnect) {
            this.observer.disconnect();
          }
          delete this.observer;
        }
  
        delete this.target;
      }
    }, {
      key: 'componentDidMount',
      value: function componentDidMount() {
        var _this2 = this;
  
        var ResizeObserver = global.ResizeObserver || this.props.ResizeObserver;
        if (this.props.useNativeIfAvailable && ResizeObserver) {
          var node = (0, _reactDom.findDOMNode)(this);
          var target = node.parentNode;
  
          this.target = target;
  
          var observer = new ResizeObserver(function (entries) {
            if (_this2.props.onObserverResize) {
              _this2.props.onObserverResize(entries);
            }
  
            var first = entries[0];
  
            if (first) {
              _this2.onResize(first.contentRect);
            }
          });
  
          observer.observe(target);
  
          this.observer = observer;
        }
  
        if (typeof this.props.onMount === 'function') {
          this.props.onMount(this);
        }
  
        if (this.observer) {
          return;
        }
  
        this.resetResizeTool(function () {
          if (_this2.props.notifyOnMount) {
            var _state = _this2.state,
                width = _state.notifyResizeWidth,
                height = _state.notifyResizeHeight;
  
            _this2.onResize({ width: width, height: height });
          }
        });
      }
    }, {
      key: 'render',
      value: function render() {
        var ResizeObserver = global.ResizeObserver || this.props.ResizeObserver;
        if (this.props.useNativeIfAvailable && ResizeObserver) {
          return RENDER_HAS_NATIVE_RESIZE_OBSERVER;
        }
        return _react2.default.createElement(
          'div',
          {
            ref: this.refNotifyResize,
            style: notifyResizeStyle,
            onScroll: this.checkResize
          },
          this.renderExpandTool(),
          this.renderContractTool()
        );
      }
    }, {
      key: 'renderExpandTool',
      value: function renderExpandTool() {
        return _react2.default.createElement(
          'div',
          { ref: this.refExpandTool, style: expandToolStyle },
          _react2.default.createElement('div', {
            ref: this.refExpandToolInner,
            style: {
              position: 'absolute',
              top: 0,
              left: 0,
              width: this.state.expandToolWidth,
              height: this.state.expandToolHeight
            }
          })
        );
      }
    }, {
      key: 'renderContractTool',
      value: function renderContractTool() {
        return _react2.default.createElement(
          'div',
          { ref: this.refContractTool, style: contractToolStyle },
          _react2.default.createElement('div', { ref: 'contractInner', style: contractToolInnerStyle })
        );
      }
    }, {
      key: 'resetResizeTool',
      value: function resetResizeTool(callback) {
        var _this3 = this;
  
        this.setDimensions(function () {
          _this3.scrollToBottomExpandTool();
          if (typeof callback == 'function') {
            callback();
          }
        });
      }
    }, {
      key: 'setDimensions',
      value: function setDimensions(callback) {
        var _this4 = this;
  
        this.getDimensions(function (size) {
          var notifyResizeWidth = size.notifyResizeWidth,
              notifyResizeHeight = size.notifyResizeHeight;
  
  
          if (_this4.__willUnmount) {
            return;
          }
          // Resize tool will be bigger than its parent by 1 pixel in each direction
          _this4.setState({
            notifyResizeWidth: notifyResizeWidth,
            notifyResizeHeight: notifyResizeHeight,
            expandToolWidth: notifyResizeWidth + 1,
            expandToolHeight: notifyResizeHeight + 1
          }, callback);
        });
      }
    }, {
      key: 'getDimensions',
      value: function getDimensions(callback) {
        var _this5 = this;
  
        if (!callback || typeof callback != 'function') {
          callback = emptyFn;
        }
        var notifyResize = this.notifyResizeNode;
        if (!notifyResize) {
          return;
        }
        var node = notifyResize.parentElement || notifyResize;
  
        var size = void 0;
  
        var fn = this.props.useRaf ? requestAnimationFrame : immediateFn;
  
        fn(function () {
          if (typeof _this5.props.measureSize == 'function') {
            size = _this5.props.measureSize(node, notifyResize);
          } else {
            size = {
              width: node.offsetWidth,
              height: node.offsetHeight
            };
          }
  
          callback({
            notifyResizeWidth: size.width,
            notifyResizeHeight: size.height
          });
        });
      }
    }, {
      key: 'scrollToBottomExpandTool',
      value: function scrollToBottomExpandTool(callback) {
        var _this6 = this;
  
        // so the scroll moves when element resizes
        if (this.notifyResizeNode) {
          requestAnimationFrame(function () {
            // scroll to bottom
            var expandTool = _this6.expandToolNode;
            var contractTool = _this6.contractToolNode;
  
            var expandToolScrollHeight = void 0;
            var expandToolScrollWidth = void 0;
  
            var contractToolScrollHeight = void 0;
            var contractToolScrollWidth = void 0;
  
            if (expandTool) {
              expandToolScrollHeight = expandTool.scrollHeight;
              expandToolScrollWidth = expandTool.scrollWidth;
            }
  
            if (contractTool) {
              contractToolScrollHeight = contractTool.scrollHeight;
              contractToolScrollWidth = contractTool.scrollWidth;
            }
  
            if (expandTool) {
              expandTool.scrollTop = expandToolScrollHeight;
              expandTool.scrollLeft = expandToolScrollWidth;
            }
  
            if (contractTool) {
              contractTool.scrollTop = contractToolScrollHeight;
              contractTool.scrollLeft = contractToolScrollWidth;
            }
  
            if (typeof callback == 'function') {
              callback();
            }
          });
        }
      }
    }, {
      key: 'checkResize',
      value: function checkResize() {
        var _this7 = this;
  
        this.getDimensions(function (_ref) {
          var notifyResizeWidth = _ref.notifyResizeWidth,
              notifyResizeHeight = _ref.notifyResizeHeight;
  
          if (notifyResizeWidth !== _this7.state.notifyResizeWidth || notifyResizeHeight !== _this7.state.notifyResizeHeight) {
            _this7.onResize({
              width: notifyResizeWidth,
              height: notifyResizeHeight
            });
            // reset resizeToolDimensions
            _this7.resetResizeTool();
          }
        });
      }
    }, {
      key: 'onResize',
      value: function onResize(_ref2) {
        var width = _ref2.width,
            height = _ref2.height;
  
        if (this.__willUnmount) {
          return;
        }
        if (typeof this.props.onResize === 'function') {
          this.props.onResize({ width: width, height: height });
        }
      }
    }]);
  
    return ZippyNotifyResize;
  }(_react2.default.Component);
  
  ZippyNotifyResize.defaultProps = {
    useNativeIfAvailable: true,
    useWillChange: false,
    useRaf: true
  };
  
  ZippyNotifyResize.propTypes = {
    ResizeObserver: _propTypes.func,
    onResize: _propTypes.func,
    onObserverResize: _propTypes.func, // only called when native resizeobserver is available
    useNativeIfAvailable: _propTypes.bool,
    onMount: _propTypes.func,
    useWillChange: _propTypes.bool,
    useRaf: _propTypes.bool,
    notifyOnMount: _propTypes.bool,
    notifyResizeDelay: _propTypes.number,
    checkResizeDelay: _propTypes.number
  };
  
  exports.default = ZippyNotifyResize;
  exports.NotifyResize = ZippyNotifyResize;