import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["sx", "component"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

/* eslint-disable react/forbid-prop-types */
import React, { useMemo } from "react";
import PropTypes from "prop-types";
import { jsx, useTheme } from "@emotion/react";
import { shortcodes } from "../system/styleSystem";
import getCss from "../system/css";

var isStyleProp = function isStyleProp(prop) {
  return prop in shortcodes || prop in document.documentElement.style && typeof document.documentElement.style[prop] !== "function";
};

var extractStyleProps = function extractStyleProps(props) {
  return Object.keys(props).reduce(function (acc, prop) {
    var type = isStyleProp(prop) ? 0 : 1;
    acc[type][prop] = props[prop];
    return acc;
  }, [{}, {}]);
};

var Box = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var _ref$sx = _ref.sx,
      sx = _ref$sx === void 0 ? {} : _ref$sx,
      _ref$component = _ref.component,
      component = _ref$component === void 0 ? "div" : _ref$component,
      props = _objectWithoutProperties(_ref, _excluded);

  var theme = useTheme();

  var _useMemo = useMemo(function () {
    var _extractStyleProps = extractStyleProps(props),
        _extractStyleProps2 = _slicedToArray(_extractStyleProps, 2),
        styleProps = _extractStyleProps2[0],
        htmlProps = _extractStyleProps2[1];

    return [_objectSpread(_objectSpread({}, sx), styleProps), htmlProps];
  }, [sx, props]),
      _useMemo2 = _slicedToArray(_useMemo, 2),
      allStyleProps = _useMemo2[0],
      otherProps = _useMemo2[1];

  var css = useMemo(function () {
    return getCss(allStyleProps, theme);
  }, [allStyleProps, theme]);
  return jsx(component, _objectSpread({
    css: css,
    ref: ref
  }, otherProps));
});
Box.propTypes = {
  sx: PropTypes.object,
  component: PropTypes.elementType
};

if (process.env.NODE_ENV !== "production") {
  Box.displayName = "Box";
}

export default Box;