import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import { Component } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import events from '../util/events';

var isDescendant = function isDescendant(el, target) {
    if (target !== null) {
        return el === target || isDescendant(el, target.parentNode);
    }
    return false;
};

var clickAwayEvents = ['mouseup', 'touchend'];
var bind = function bind(callback) {
    return clickAwayEvents.forEach(function (event) {
        return events.on(document, event, callback);
    });
};
var unbind = function unbind(callback) {
    return clickAwayEvents.forEach(function (event) {
        return events.off(document, event, callback);
    });
};

var ClickAwayListener = function (_Component) {
    _inherits(ClickAwayListener, _Component);

    function ClickAwayListener() {
        var _ref;

        var _temp, _this, _ret;

        _classCallCheck(this, ClickAwayListener);

        for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
            args[_key] = arguments[_key];
        }

        return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ClickAwayListener.__proto__ || Object.getPrototypeOf(ClickAwayListener)).call.apply(_ref, [this].concat(args))), _this), _this.handleClickAway = function (event) {
            if (event.defaultPrevented) {
                return;
            }

            // IE11 support, which trigger the handleClickAway even after the unbind
            if (_this.isCurrentlyMounted) {
                var el = ReactDOM.findDOMNode(_this);

                if (document.documentElement.contains(event.target) && !isDescendant(el, event.target)) {
                    _this.props.onClickAway(event);
                }
            }
        }, _temp), _possibleConstructorReturn(_this, _ret);
    }

    _createClass(ClickAwayListener, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            this.isCurrentlyMounted = true;
            if (this.props.onClickAway) {
                bind(this.handleClickAway);
            }
        }
    }, {
        key: 'componentDidUpdate',
        value: function componentDidUpdate(prevProps) {
            if (prevProps.onClickAway !== this.props.onClickAway) {
                unbind(this.handleClickAway);
                if (this.props.onClickAway) {
                    bind(this.handleClickAway);
                }
            }
        }
    }, {
        key: 'componentWillUnmount',
        value: function componentWillUnmount() {
            this.isCurrentlyMounted = false;
            unbind(this.handleClickAway);
        }
    }, {
        key: 'render',
        value: function render() {
            return this.props.children;
        }
    }]);

    return ClickAwayListener;
}(Component);

ClickAwayListener.propTypes = {
    children: PropTypes.element,
    onClickAway: PropTypes.func
};


export default ClickAwayListener;