'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var cls = require('classnames');
require('focus-visible');
var utils = require('./utils.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);
var cls__default = /*#__PURE__*/_interopDefault(cls);

var _excluded = ["className", "as", "forwardedAs", "responsive"];
/* eslint-disable no-underscore-dangle */
var _component = function _component(componentConfig) {
  var tag = componentConfig.tag,
    base = componentConfig.base,
    name = componentConfig.name,
    intersections = componentConfig.intersections,
    invariants = componentConfig.invariants,
    responsive = componentConfig.responsive;
  var staticVariants = utils.getStaticVariants(componentConfig);
  var dynamicVariants = utils.getDynamicVariants(componentConfig);
  var component = /*#__PURE__*/React.forwardRef(function (props, ref) {
    var className = props.className,
      as = props.as,
      forwardedAs = props.forwardedAs,
      responsiveProp = props.responsive,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(props, _excluded);
    var variants = dynamicVariants(rest);
    var intersectionStyles = utils.getIntersectionStyles(rest, intersections);
    var rootClassName = cls__default.default.apply(void 0, [className, base].concat(_rollupPluginBabelHelpers.toConsumableArray(staticVariants), _rollupPluginBabelHelpers.toConsumableArray(variants), _rollupPluginBabelHelpers.toConsumableArray(intersectionStyles), [responsiveProp && responsive, invariants]));

    // styled-components do it inside
    // filter props
    var baseProps = {};
    for (var key in rest) {
      if (!(key in componentConfig.variations)) {
        baseProps[key] = rest[key];
      }
    }
    baseProps.ref = ref;

    // styled-components do it inside
    // add props that should be attr( disabled for example)
    var htmlAttrs = {};
    for (var _key in componentConfig.variations || {}) {
      if (_key in rest) {
        var variant = componentConfig.variations[_key];
        var attrs = variant.attrs;
        if (typeof attrs === 'boolean') {
          htmlAttrs[_key] = rest[_key];
        }
      }
    }
    var Root = as || forwardedAs || tag;
    return /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      className: rootClassName
    }, htmlAttrs, baseProps));
  });
  if (name) {
    component.displayName = name;
  }
  return component;
};

exports._component = _component;
