"use strict";

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

var React = _interopRequireWildcard(require("react"));

var _styletronReact = require("styletron-react");

var _styletronStandard = require("styletron-standard");

var _themeProvider = require("./theme-provider.js");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

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

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

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

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 = _styletronReact.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 (0, _styletronReact.withStyleDeep)(styleFn.apply(void 0, args), function (props) {
    var $style = props.$style;

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

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