"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.InputGroup = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

var _ = require("..");

var _Box = require("../Box");

var _FormControl = require("../FormControl");

var _ThemeProvider = require("../ThemeProvider");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

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

var __jsx = _react["default"].createElement;

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var InputGroup = function InputGroup(_ref) {
  var children = _ref.children,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? 'md' : _ref$size,
      name = _ref.name,
      isInline = _ref.isInline,
      schema = _ref.schema,
      props = (0, _objectWithoutProperties2["default"])(_ref, ["children", "size", "name", "isInline", "schema"]);

  var _useTheme = (0, _ThemeProvider.useTheme)(),
      space = _useTheme.space;

  var pl = null;
  var pr = null;
  var spacingProps = isInline ? {
    mr: 'spacing'
  } : {
    mb: 'spacing'
  };

  var _useFormField = (0, _.useFormField)({
    name: name,
    schema: schema
  }),
      errors = _useFormField.errors;

  return __jsx(_FormControl.FormControlWrapper, (0, _extends2["default"])({
    id: name,
    error: errors
  }, spacingProps, props), __jsx(_Box.Box, {
    display: "flex",
    position: "relative"
  }, _react.Children.map(children, function (child, index) {
    if (! /*#__PURE__*/(0, _react.isValidElement)(child)) {
      return null;
    }

    if (child.type === _.InputLeftElement) {
      pl = "calc(".concat(space.input[size], " + ").concat(space['spacing-xs'], ")");
    }

    if (child.type === _.InputRightElement) {
      pr = "calc(".concat(space.input[size], " + ").concat(space['spacing-xs'], ")");
    }

    var isElement = child.type === _.InputLeftElement || child.type === _.InputRightElement;
    return /*#__PURE__*/(0, _react.cloneElement)(child, _objectSpread({
      size: child.props.size || size,
      id: child.props.id || name,
      name: child.props.name || name
    }, !isElement && {
      pl: child.props.pl || pl,
      pr: child.props.pr || pr
    }));
  })));
};

exports.InputGroup = InputGroup;
//# sourceMappingURL=InputGroup.js.map