'use strict';

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

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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

var _reactElements = require('../helpers/reactElements');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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

/**
 * Helper component for Placer
 */
var TargetWrapper = function (_React$Component) {
    _inherits(TargetWrapper, _React$Component);

    function TargetWrapper() {
        _classCallCheck(this, TargetWrapper);

        return _possibleConstructorReturn(this, (TargetWrapper.__proto__ || Object.getPrototypeOf(TargetWrapper)).apply(this, arguments));
    }

    _createClass(TargetWrapper, [{
        key: 'render',
        value: function render() {
            var _this2 = this;

            var _props = this.props,
                children = _props.children,
                rest = _objectWithoutProperties(_props, ['children']);

            if (_react2.default.Children.count(children) > 2) {
                throw new Error('TargetWrapper component: children must have less or equal two');
            }

            var placer = (0, _reactElements.getChildByType)(children, '__PLACER__');
            var target = (0, _reactElements.filterChildrenByNotType)(children, '__PLACER__')[0];

            return _react2.default.cloneElement(target, _extends({}, rest, {
                ref: function ref(c) {
                    return c && (_this2._targetDOMNode = _reactDom2.default.findDOMNode(c));
                },
                children: [].concat(_toConsumableArray(_react2.default.Children.map(target.props.children, function (child, idx) {
                    return (typeof child === 'undefined' ? 'undefined' : _typeof(child)) === 'object' ? _react2.default.cloneElement(child, { key: idx }) : child;
                })), [placer && _react2.default.cloneElement(placer, {
                    key: 'placer',
                    targetDOMNode: this._targetDOMNode || null
                })])
            }));
        }
    }]);

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

exports.default = TargetWrapper;