'use strict';

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

var _jsx = function () { var REACT_ELEMENT_TYPE = typeof Symbol === "function" && Symbol.for && Symbol.for("react.element") || 0xeac7; return function createRawReactElement(type, props, key, children) { var defaultProps = type && type.defaultProps; var childrenLength = arguments.length - 3; if (!props && childrenLength !== 0) { props = {}; } if (props && defaultProps) { for (var propName in defaultProps) { if (props[propName] === void 0) { props[propName] = defaultProps[propName]; } } } else if (!props) { props = defaultProps || {}; } if (childrenLength === 1) { props.children = children; } else if (childrenLength > 1) { var childArray = Array(childrenLength); for (var i = 0; i < childrenLength; i++) { childArray[i] = arguments[i + 3]; } props.children = childArray; } return { $$typeof: REACT_ELEMENT_TYPE, type: type, key: key === undefined ? null : '' + key, ref: null, props: props, _owner: null }; }; }();

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 _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

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 idCount = 0;

var Teleport = function (_React$Component) {
    _inherits(Teleport, _React$Component);

    function Teleport() {
        var _ref;

        _classCallCheck(this, Teleport);

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

        var _this = _possibleConstructorReturn(this, (_ref = Teleport.__proto__ || Object.getPrototypeOf(Teleport)).call.apply(_ref, [this].concat(args)));

        _this._componentID = Math.random().toString(36) + ':' + ++idCount;
        _this._parentDOMNode = null;
        return _this;
    }

    _createClass(Teleport, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            this._parentDOMNode = _reactDom2.default.findDOMNode(this).parentNode;

            if (this.context.teleport.isAdded(this._componentID)) {
                this._update(this.props.children);

                return;
            }

            this._moveToDestination();
        }
    }, {
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(nextProps) {
            if (nextProps.children !== this.props.children) {
                this._update(nextProps.children);
            }

            this._update(nextProps.children);
        }
    }, {
        key: 'componentWillUnmount',
        value: function componentWillUnmount() {
            this._destroy();
        }
    }, {
        key: 'getParentDOMNode',
        value: function getParentDOMNode() {
            return this._parentDOMNode || null;
        }
    }, {
        key: 'getRootDOMNode',
        value: function getRootDOMNode() {
            return this.context.teleport.getRootDOMNode();
        }
    }, {
        key: 'getParentBoundingClientRect',
        value: function getParentBoundingClientRect() {
            return this._parentDOMNode ? this._parentDOMNode.getBoundingClientRect() : null;
        }
    }, {
        key: '_moveToDestination',
        value: function _moveToDestination(callback) {
            this.context.teleport.move(this._componentID, this._getValidChildren(), callback);
        }
    }, {
        key: '_update',
        value: function _update(newChildren, callback) {
            this.context.teleport.update(this._componentID, newChildren, callback);
        }
    }, {
        key: '_destroy',
        value: function _destroy(callback) {
            this.context.teleport.remove(this._componentID, callback);
        }
    }, {
        key: '_getValidChildren',
        value: function _getValidChildren(children) {
            children = children || this.props.children;

            return typeof children === 'string' || typeof children === 'number' ? children : _react2.default.Children.only(children);
        }
    }, {
        key: 'render',
        value: function render() {
            return _jsx('div', {});
        }
    }]);

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

Teleport.contextTypes = {
    teleport: _react.PropTypes.shape({
        move: _react.PropTypes.func,
        remove: _react.PropTypes.func,
        update: _react.PropTypes.func,
        isAdded: _react.PropTypes.func,
        getRootDOMNode: _react.PropTypes.func,
        getBoundingClientRect: _react.PropTypes.func,
        getContextLevel: _react.PropTypes.func
    })
};
exports.default = Teleport;