'use strict';

exports.__esModule = true;

var _getIterator2 = require('babel-runtime/core-js/get-iterator');

var _getIterator3 = _interopRequireDefault(_getIterator2);

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactStaticContainer = require('react-static-container');

var _reactStaticContainer2 = _interopRequireDefault(_reactStaticContainer);

var _mergeRouteParams = require('./utils/mergeRouteParams');

var _mergeRouteParams2 = _interopRequireDefault(_mergeRouteParams);

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

var propTypes = {
  queries: _react2.default.PropTypes.object.isRequired,
  routerProps: _react2.default.PropTypes.object.isRequired,
  children: _react2.default.PropTypes.element.isRequired
};

var contextTypes = {
  queryAggregator: _react2.default.PropTypes.object.isRequired
};

function RouteContainer(_ref, _ref2) {
  var queries = _ref.queries;
  var routerProps = _ref.routerProps;
  var children = _ref.children;
  var extraProps = (0, _objectWithoutProperties3.default)(_ref, ['queries', 'routerProps', 'children']);
  var queryAggregator = _ref2.queryAggregator;
  var key = routerProps.key;
  var route = routerProps.route;
  var routes = routerProps.routes;


  var params = {};
  for (var _iterator = routes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
    var _ref3;

    if (_isArray) {
      if (_i >= _iterator.length) break;
      _ref3 = _iterator[_i++];
    } else {
      _i = _iterator.next();
      if (_i.done) break;
      _ref3 = _i.value;
    }

    var ancestorRoute = _ref3;

    params = (0, _mergeRouteParams2.default)(params, ancestorRoute, routerProps);

    if (ancestorRoute === route) {
      break;
    }
  }

  var renderArgs = queryAggregator.getRenderArgs(route, key, queries, params);

  var props = renderArgs.props;
  var render = route.render;

  if (render && (typeof render === 'undefined' ? 'undefined' : (0, _typeof3.default)(render)) === 'object') {
    render = render[key];
  }

  // The below is largely copied from RelayReadyStateRenderer.

  var element = void 0;
  if (render) {
    element = render.call(route, (0, _extends3.default)({}, renderArgs, {
      props: props && (0, _extends3.default)({}, routerProps, extraProps, params, props),
      routerProps: (0, _extends3.default)({}, routerProps, extraProps),
      element: children
    }));
  } else if (props) {
    // The child already has routerProps, so just inject the additional props.
    element = _react2.default.cloneElement(children, (0, _extends3.default)({}, extraProps, params, props));
  }

  var shouldUpdate = void 0;
  if (element === undefined) {
    element = null;
    shouldUpdate = false;
  } else {
    shouldUpdate = true;
  }

  return _react2.default.createElement(
    _reactStaticContainer2.default,
    { shouldUpdate: shouldUpdate },
    element
  );
}

RouteContainer.propTypes = propTypes;
RouteContainer.contextTypes = contextTypes;

exports.default = RouteContainer;
module.exports = exports['default'];