function _extends() { _extends = Object.assign ? Object.assign.bind() : 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) 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, withStyle as styletronWithStyle, useStyletron as styletronUseStyletron, withWrapper as styletronWithWrapper } from 'styletron-react';
import { driver, getInitialStyle } from 'styletron-standard';
import { ThemeContext } from './theme-provider';

const wrapper = StyledComponent => {
  // eslint-disable-next-line react/display-name
  return /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(ThemeContext.Consumer, null, theme => /*#__PURE__*/React.createElement(StyledComponent, _extends({
    ref: ref
  }, props, {
    $theme: theme
  }))));
};

/* eslint-enable flowtype/generic-spacing */

/* flowlint unclear-type:error */
export function createThemedStyled() {
  return createStyled({
    wrapper,
    getInitialStyle,
    driver
  });
}
export const styled = createThemedStyled();
export function createThemedWithStyle() {
  return styletronWithStyle;
}
export const withStyle = createThemedWithStyle();
export function createThemedUseStyletron() {
  return function () {
    const theme = React.useContext(ThemeContext);
    const [css] = styletronUseStyletron();
    return [css, theme];
  };
}
export const useStyletron = createThemedUseStyletron();
export function withWrapper(StyledElement, wrapperFn) {
  return styletronWithWrapper(StyledElement, Styled => {
    // eslint-disable-next-line react/display-name
    return /*#__PURE__*/React.forwardRef((props, ref) => /*#__PURE__*/React.createElement(ThemeContext.Consumer, null, theme => // @ts-ignore
    wrapperFn(Styled)({
      ref: ref,
      ...props,
      $theme: theme
    })));
  });
}