(function (global, factory) {
	if (typeof define === "function" && define.amd) {
		define(['exports', 'react', 'classnames'], factory);
	} else if (typeof exports !== "undefined") {
		factory(exports, require('react'), require('classnames'));
	} else {
		var mod = {
			exports: {}
		};
		factory(mod.exports, global.react, global.classnames);
		global.IronCollapse = mod.exports;
	}
})(this, function (exports, _react, _classnames) {
	'use strict';

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

	var _react2 = _interopRequireDefault(_react);

	var _classnames2 = _interopRequireDefault(_classnames);

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

	function _defineProperty(obj, key, value) {
		if (key in obj) {
			Object.defineProperty(obj, key, {
				value: value,
				enumerable: true,
				configurable: true,
				writable: true
			});
		} else {
			obj[key] = value;
		}

		return obj;
	}

	function _classCallCheck(instance, Constructor) {
		if (!(instance instanceof Constructor)) {
			throw new TypeError("Cannot call a class as a function");
		}
	}

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

	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 IronCollapse = function (_React$Component) {
		_inherits(IronCollapse, _React$Component);

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

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

			_this.state = {
				noAnimation: props.noAnimation || false,
				horizontal: props.horizontal || false,
				opened: props.opened || false,
				style: null
			};
			_this._handleRef = _this._handleRef.bind(_this);
			_this._transitionEndBound = _this._transitionEnd.bind(_this);
			return _this;
		}

		_createClass(IronCollapse, [{
			key: '_calcSize',
			value: function _calcSize() {
				return this.base.getBoundingClientRect()[this.dimension] + "px";
			}
		}, {
			key: 'toggleClass',
			value: function toggleClass(name, bool) {
				if (bool) {
					this.base.classList.add(name);
				} else {
					this.base.classList.remove(name);
				}
			}
		}, {
			key: '_transitionEnd',
			value: function _transitionEnd() {
				if (this.state.opened) {
					this.base.style[this.dimension] = 'auto';
				}
				this._updateTransition(false);
				this.toggleClass('iron-collapse-closed', !this.state.opened);
			}
		}, {
			key: '_updateTransition',
			value: function _updateTransition(enabled) {
				this.base.style.transitionDuration = enabled && !this.state.noAnimation ? '' : '0s';
			}
		}, {
			key: 'focus',
			value: function focus() {
				this.base.focus();
			}
		}, {
			key: 'updateSize',
			value: function updateSize(size, animated) {
				var style = this.base.style;
				if (this.base.style[this.dimension] === size) {
					return;
				}
				this._updateTransition(false);
				if (animated && !this.state.noAnimation && this._isDisplayed) {
					var startSize = this._calcSize();

					if (size === "auto") {
						this.base.style[this.dimension] = size;
						size = this._calcSize();
					}
					this.base.style[this.dimension] = startSize;
					this.base.style.height = this.base.offsetHeight;
					this._updateTransition(true);
				}
				this.base.style[this.dimension] = size;
			}
		}, {
			key: 'toggle',
			value: function toggle() {
				if (this.state.opened) {
					this.hide();
				} else {
					this.show();
				}
			}
		}, {
			key: 'show',
			value: function show() {
				this.setState({ opened: true });
			}
		}, {
			key: 'hide',
			value: function hide() {
				this.setState({ opened: false });
			}
		}, {
			key: 'componentWillReceiveProps',
			value: function componentWillReceiveProps(nextProps) {
				if (nextProps.opened !== this.state.opened) {
					this.setState({ opened: nextProps.opened });
				}
			}
		}, {
			key: 'componentDidUpdate',
			value: function componentDidUpdate() {
				this.updateSize(this.state.opened ? 'auto' : '0px', true);
				if (this.state.opened) {
					this.focus();
				}
				if (this.state.noAnimation) {
					this._transitionEnd();
				}
				if (this.props.onChange) {
					this.props.onChange(this.state);
				}
			}
		}, {
			key: 'componentDidMount',
			value: function componentDidMount() {
				this.base.addEventListener('transitionend', this._transitionEndBound);
				this._transitionEnd();
			}
		}, {
			key: 'componentWillUnmount',
			value: function componentWillUnmount() {
				this.base.removeEventListener('transitionend', this._transitionEndBound);
			}
		}, {
			key: '_handleRef',
			value: function _handleRef(el) {
				this.base = el;
			}
		}, {
			key: 'render',
			value: function render() {
				var _React$createElement;

				var opened = this.state.opened;
				var _props = this.props,
				    style = _props.style,
				    children = _props.children;


				var baseClassnames = (0, _classnames2.default)('iron-collapse');

				return _react2.default.createElement(
					'div',
					(_React$createElement = { className: baseClassnames,
						ref: this._handleRef
					}, _defineProperty(_React$createElement, 'className', baseClassnames), _defineProperty(_React$createElement, 'style', style), _defineProperty(_React$createElement, 'aria-hidden', !opened), _defineProperty(_React$createElement, 'aria-expanded', opened), _React$createElement),
					children,
					_react2.default.createElement(
						'style',
						null,
						'\n\t\t\t\t\t.iron-collapse {\n\t\t\t\t\t  display: block;\n\t\t\t\t\t  -webkit-transition-duration: 300ms;\n\t\t\t\t\t  -moz-transition-duration: 300ms;\n\t\t\t\t\t  transition-duration: 300ms;\n\t\t\t\t\t  overflow: visible;\n\t\t\t\t\t}\n\t\t\t\t\t.iron-collapse:not(.iron-collapse-opened) {\n\t\t\t\t\t  overflow: hidden;\n\t\t\t\t\t}\n\t\t\t\t'
					)
				);
			}
		}, {
			key: 'dimension',
			get: function get() {
				return this.state.horizontal ? "width" : "height";
			}
		}, {
			key: '_isDisplayed',
			get: function get() {
				var rect = this.base.getBoundingClientRect();
				for (var prop in rect) {
					if (rect[prop] !== 0) return true;
				}
				return false;
			}
		}]);

		return IronCollapse;
	}(_react2.default.Component);

	exports.default = IronCollapse;
});