(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(['exports', 'react', 'classnames', '../BaseComponent', 'styled-components'], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require('react'), require('classnames'), require('../BaseComponent'), require('styled-components'));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.react, global.classnames, global.BaseComponent, global.styledComponents);
    global.index = mod.exports;
  }
})(this, function (exports, _react, _classnames, _BaseComponent2, _styledComponents) {
  'use strict';

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

  var _react2 = _interopRequireDefault(_react);

  var _classnames2 = _interopRequireDefault(_classnames);

  var _BaseComponent3 = _interopRequireDefault(_BaseComponent2);

  var _styledComponents2 = _interopRequireDefault(_styledComponents);

  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 _templateObject = _taggedTemplateLiteral(['\n  position        : fixed;\n  top             : 0;\n  right           : 0;\n  bottom          : 0;\n  left            : 0;\n  z-index         : 900;\n  background-color: var(--px-modal-overlay-color, rgba(0, 0, 0, 0.5));\n  transition      : background-color 0.2s cubic-bezier(.78,.13,.16,.87);\n  \n  ', '\n'], ['\n  position        : fixed;\n  top             : 0;\n  right           : 0;\n  bottom          : 0;\n  left            : 0;\n  z-index         : 900;\n  background-color: var(--px-modal-overlay-color, rgba(0, 0, 0, 0.5));\n  transition      : background-color 0.2s cubic-bezier(.78,.13,.16,.87);\n  \n  ', '\n']),
      _templateObject2 = _taggedTemplateLiteral(['\n     visibility      : hidden;\n     background-color: transparent;\n  '], ['\n     visibility      : hidden;\n     background-color: transparent;\n  ']);

  function _taggedTemplateLiteral(strings, raw) {
    return Object.freeze(Object.defineProperties(strings, {
      raw: {
        value: Object.freeze(raw)
      }
    }));
  }

  var Overlay = _styledComponents2.default.div(_templateObject, function (props) {
    return props.invisible && (0, _styledComponents.css)(_templateObject2);
  });

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

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

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

      _this.state = {
        opened: props.opened
      };
      _this._handleOverlayClick = _this._handleOverlayClick.bind(_this);
      _this._handleEscKeyUp = _this._handleEscKeyUp.bind(_this);
      return _this;
    }

    _createClass(OverlayComponent, [{
      key: 'componentDidMount',
      value: function componentDidMount() {
        window.addEventListener('keyup', this._handleEscKeyUp);
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        window.removeEventListener('keyup', this._handleEscKeyUp);
      }
    }, {
      key: 'componentWillReceiveProps',
      value: function componentWillReceiveProps(nextProps) {
        this.setState({ opened: nextProps.opened });
      }
    }, {
      key: '_handleEscKeyUp',
      value: function _handleEscKeyUp(e) {
        if (e.keyCode === 27 && !this.props.ignoreEscapeKeyUp) {
          if (this.props.onEscapeKeyUp) {
            this.props.onEscapeKeyUp(e);
          }
        }
      }
    }, {
      key: '_handleOverlayClick',
      value: function _handleOverlayClick(e) {
        if (this.props.onOverlayClick && !this.props.ignoreOverlayClick) {
          this.props.onOverlayClick(e);
        }
      }
    }, {
      key: 'close',
      value: function close() {
        this.setState({ opened: false });
      }
    }, {
      key: 'open',
      value: function open() {
        this.setState({ opened: true });
      }
    }, {
      key: 'render',
      value: function render() {
        var _props = this.props,
            style = _props.style,
            onOverlayClick = _props.onOverlayClick,
            ignoreOverlayClick = _props.ignoreOverlayClick,
            ignoreEscapeKeyUp = _props.ignoreEscapeKeyUp,
            _props$zIndex = _props.zIndex,
            zIndex = _props$zIndex === undefined ? 900 : _props$zIndex,
            children = _props.children;
        var opened = this.state.opened;

        return _react2.default.createElement(
          Overlay,
          { invisible: !opened, onClick: this._handleOverlayClick },
          children
        );
      }
    }]);

    return OverlayComponent;
  }(_BaseComponent3.default);

  OverlayComponent.defaultProps = {
    fullScreen: false,
    onEscapeKeyUp: null,
    onOverlayClick: null,
    onRequestClose: null,
    ignoreEscapeKeyUp: false,
    ignoreBackdropClick: false
  };
  OverlayComponent.displayName = 'Overlay';

  exports.default = OverlayComponent;
});