function _extends() { _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; }; return _extends.apply(this, arguments); } import React from 'react'; import forwardRef from './forwardRef'; var getDisplayName = function getDisplayName(Component) { var name = typeof Component === 'string' ? Component : Component.name || Component.displayName; return name ? "ContextTransform(" + name + ")" : 'ContextTransform'; }; var ensureConsumer = function ensureConsumer(c) { return c.Consumer || c; }; function $mapContextToProps(_ref, Component) { var maybeArrayOfConsumers = _ref.consumers, mapToProps = _ref.mapToProps, displayName = _ref.displayName, _ref$forwardRefAs = _ref.forwardRefAs, forwardRefAs = _ref$forwardRefAs === void 0 ? 'ref' : _ref$forwardRefAs; var consumers = maybeArrayOfConsumers; if (!Array.isArray(maybeArrayOfConsumers)) { consumers = [maybeArrayOfConsumers]; } var SingleConsumer = ensureConsumer(consumers[0]); function singleRender(props, ref) { var _extends2; var propsWithRef = _extends((_extends2 = {}, _extends2[forwardRefAs] = ref, _extends2), props); return React.createElement(SingleConsumer, null, function (value) { return React.createElement(Component, _extends({}, propsWithRef, mapToProps(value, props))); }); } function multiRender(props, ref) { var _extends3; var propsWithRef = _extends((_extends3 = {}, _extends3[forwardRefAs] = ref, _extends3), props); return consumers.reduceRight(function (inner, Context) { return function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } var Consumer = ensureConsumer(Context); return React.createElement(Consumer, null, function (value) { return inner.apply(void 0, args.concat([value])); }); }; }, function () { for (var _len2 = arguments.length, contexts = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { contexts[_key2] = arguments[_key2]; } return React.createElement(Component, _extends({}, propsWithRef, mapToProps.apply(void 0, contexts.concat([props])))); })(); } var contextTransform = consumers.length === 1 ? singleRender : multiRender; return forwardRef(contextTransform, { displayName: displayName || getDisplayName(Component) }); } export default function mapContextToProps(maybeOpts, mapToProps, Component) { if (arguments.length === 2) return $mapContextToProps(maybeOpts, mapToProps); return $mapContextToProps({ consumers: maybeOpts, mapToProps: mapToProps }, Component); }