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

/*
Copyright (c) 2018 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { createStyled, withStyleDeep } from 'styletron-react';
import { driver, getInitialStyle } from 'styletron-standard';
import { ThemeContext } from './theme-provider.js';

var wrapper = function wrapper(StyledComponent) {
  return function withThemeHOC(props) {
    return React.createElement(ThemeContext.Consumer, null, function (theme) {
      return React.createElement(StyledComponent, _extends({}, props, {
        $theme: theme
      }));
    });
  };
};

var baseStyled = createStyled({
  wrapper: wrapper,
  getInitialStyle: getInitialStyle,
  driver: driver
}); // TODO(#495): Need a flow expert to help remove this 'any' type
// eslint-disable-next-line flowtype/no-weak-types

export default function styledWrapper() {
  // If user is trying to style a styled component
  // use withStyleDeep, otherwise use baseStyled
  var styleFn = baseStyled;

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

  if (args[0] && args[0].__STYLETRON__) {
    styleFn = withStyleDeep;
  } // Also allow passing deep style overrides via $style prop
  // Ex: <StyledDiv $style={{color: 'red'}} />
  // Issue for supporting this natively in styletron:
  // https://github.com/rtsao/styletron/issues/221
  // $FlowFixMe


  return withStyleDeep(styleFn.apply(void 0, args), function (props) {
    var $style = props.$style;

    if (typeof $style === 'function') {
      return $style(props);
    }

    return $style || {};
  });
}