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

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

	var _react2 = _interopRequireDefault(_react);

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

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

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

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

			_this._mq = null;
			_this._boundMQHandler = _this.queryHandler.bind(_this);
			_this.defaultProps = {
				queryMatches: false,
				query: null,
				onMatch: null,
				onChange: null
			};
			_this.state = {
				full: props.full || false,
				queryMatches: false,
				query: props.query || null
			};
			_this.namedQueries = {
				xs: "(max-width: 21.33rem)",
				sm: "(min-width: 22rem) and (max-width: 49.06rem)",
				md: "(min-width: 51.2rem) and (max-width: 68.2rem)",
				lg: "(min-width: 68.26rem) and (max-width: 80rem)",
				xl: "(min-width: 85.33rem)"
			};
			return _this;
		}

		_createClass(IronMediaQuery, [{
			key: "_setQueryMatches",
			value: function _setQueryMatches(m) {
				var s = this.state;
				s.queryMatches = m;
				this.setState(s);
				if (this.props.onChange) {
					this.props.onChange(s);
				}
				if (m && this.props.onMatch) {
					this.props.onMatch(s);
				}
			}
		}, {
			key: "_add",
			value: function _add() {
				if (this._mq) {
					this._mq.addListener(this._boundMQHandler);
				}
			}
		}, {
			key: "_remove",
			value: function _remove() {
				if (this._mq) {
					this._mq.removeListener(this._boundMQHandler);
				}
				this._mq = null;
			}
		}, {
			key: "queryChanged",
			value: function queryChanged() {
				this._remove();
				var query = this.state.query;
				if (this.namedQueries[query]) {
					query = this.namedQueries[query];
				}
				if (!query) {
					return;
				}
				if (!this.props.full && query[0] !== "(") {
					query = "(" + query + ")";
				}
				this._mq = window.matchMedia(query);
				this._add();
				this.queryHandler(this._mq);
			}
		}, {
			key: "queryHandler",
			value: function queryHandler(mq) {
				this.fire("px-media-query-change", mq);
				this._setQueryMatches(mq.matches);
			}
		}, {
			key: "fire",
			value: function fire(e, d) {}
		}, {
			key: "componentDidMount",
			value: function componentDidMount() {
				this.queryChanged();
			}
		}, {
			key: "componentWillUnmount",
			value: function componentWillUnmount() {
				this._remove();
			}
		}, {
			key: "render",
			value: function render() {
				var children = this.props.children;
				var queryMatches = this.state.queryMatches;

				return _react2.default.createElement(
					"div",
					null,
					queryMatches && children
				);
			}
		}]);

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

	exports.default = IronMediaQuery;
});