'use strict';

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

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 _BaseComponent2 = require('../px/BaseComponent');

var _BaseComponent3 = _interopRequireDefault(_BaseComponent2);

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; }

var IronResizable = function (_BaseComponent) {
	_inherits(IronResizable, _BaseComponent);

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

		var _this = _possibleConstructorReturn(this, (IronResizable.__proto__ || Object.getPrototypeOf(IronResizable)).call(this, props, { displayName: 'IronResizable' }));

		_this.defaultProps = {
			_parentResizable: {},

			_notifyingDescendant: false
		};
		_this.isAttached = false;
		_this.state = {};
		_this._interestedResizables = [];
		_this._boundNotifyResize = _this.notifyResize.bind(_this);
		return _this;
	}

	_createClass(IronResizable, [{
		key: 'componentWillMount',
		value: function componentWillMount() {
			this.isAttached = true;
		}
	}, {
		key: 'componentDidMount',
		value: function componentDidMount() {
			this._log('componentDidMount', 'setup');
			if (!this._parentResizable) {
				window.addEventListener('resize', this._boundNotifyResize);
				this.notifyResize();
			}
			this.notifyResize();
		}
	}, {
		key: 'componentWillUnmount',
		value: function componentWillUnmount() {
			this._log('componentWillUnmount', 'cleanup');
			if (this._parentResizable) {
				this._parentResizable.stopResizeNotificationsFor(this);
			} else {
				window.removeEventListener('resize', this._boundNotifyResize);
			}

			this._parentResizable = null;
		}
	}, {
		key: 'notifyResize',
		value: function notifyResize() {
			if (!this.isAttached) {
				return;
			}

			this._interestedResizables.forEach(function (resizable) {
				if (this.resizerShouldNotify(resizable)) {
					this._notifyDescendant(resizable);
				}
			}, this);

			this._fireResize();
		}
	}, {
		key: 'stopResizeNotificationsFor',
		value: function stopResizeNotificationsFor(target) {
			var index = this._interestedResizables.indexOf(target);

			if (index > -1) {
				this._interestedResizables.splice(index, 1);
				this.unlisten(target, 'iron-resize', '_onDescendantIronResize');
			}
		}
	}, {
		key: 'resizerShouldNotify',
		value: function resizerShouldNotify(element) {
			return true;
		}
	}, {
		key: '_notifyDescendant',
		value: function _notifyDescendant() {
			if (!this.isAttached) {
				return;
			}

			this._notifyingDescendant = true;
			descendant.notifyResize();
			this._notifyingDescendant = false;
		}
	}, {
		key: '_fireResize',
		value: function _fireResize() {
			this._log('fireResize', this);
		}
	}, {
		key: 'render',
		value: function render() {
			var children = this.props.children;

			return _react2.default.createElement(
				'div',
				null,
				children
			);
		}
	}]);

	return IronResizable;
}(_BaseComponent3.default);

exports.default = IronResizable;