'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 _Teleport = require('../Teleport');

var _Teleport2 = _interopRequireDefault(_Teleport);

var _PlacerWrapper = require('./PlacerWrapper');

var _PlacerWrapper2 = _interopRequireDefault(_PlacerWrapper);

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

    function Placer(props) {
        var _ref;

        _classCallCheck(this, Placer);

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

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

        _this._teleportComponent = null;
        _this._parentDOMNodeWithScroll = null;
        _this._onWrapperMountHandler = _this._onWrapperMountHandler.bind(_this);
        _this._onTeleportMountHandler = _this._onTeleportMountHandler.bind(_this);
        _this._getTargetRect = _this._getTargetRect.bind(_this);
        return _this;
    }

    _createClass(Placer, [{
        key: '_onWrapperMountHandler',
        value: function _onWrapperMountHandler(c) {
            this._wrapperComponent = c;
        }
    }, {
        key: '_onTeleportMountHandler',
        value: function _onTeleportMountHandler(c) {
            this._teleportComponent = c;
        }
    }, {
        key: '_getTargetRect',
        value: function _getTargetRect() {
            var props = this.props;

            if (props.targetRect) {
                return props.targetRect;
            }

            if (props.targetDOMNode) {
                return this.props.targetDOMNode.getBoundingClientRect();
            }

            if (!this._teleportComponent) {
                return null;
            }

            return this._teleportComponent.getParentBoundingClientRect();
        }
    }, {
        key: 'render',
        value: function render() {
            var props = this.props;

            return _react2.default.createElement(
                _Teleport2.default,
                { ref: this._onTeleportMountHandler },
                _jsx(_PlacerWrapper2.default, {
                    zIndex: props.zIndex,
                    getTargetRect: this._getTargetRect,
                    presets: props.presets,
                    viewportAccuracyFactor: props.viewportAccuracyFactor,
                    onDidMount: this._onWrapperMountHandler
                }, void 0, props.children)
            );
        }
    }]);

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

Placer.__PLACER__ = true;
Placer.defaultProps = {
    zIndex: 999,
    viewportAccuracyFactor: .9
};
Placer.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 = Placer;