"use strict";

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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormControlWrapper = exports.FormControl = exports.useFormControl = exports.useFormControlContext = void 0;

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

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

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

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

var _FormErrorMessage = require("../FormErrorMessage");

var _FormHelperText = require("../FormHelperText");

var _FormLabel = require("../FormLabel");

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;
var FormControlContext = /*#__PURE__*/(0, _react.createContext)(null);

var useFormControlContext = function useFormControlContext() {
  var context = (0, _react.useContext)(FormControlContext);
  return context;
};

exports.useFormControlContext = useFormControlContext;

var useFormControl = function useFormControl(props) {
  var context = useFormControlContext();

  if (!context) {
    return props;
  }

  var keys = Object.keys(context);
  return keys.reduce(function (acc, prop) {
    /** Giving precedence to `props` over `context` */
    acc[prop] = props[prop];

    if (context) {
      if (props[prop] == null) {
        acc[prop] = context[prop];
      }
    }

    return acc;
  }, {});
};

exports.useFormControl = useFormControl;
var FormControl = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var isInvalid = _ref.isInvalid,
      isRequired = _ref.isRequired,
      isDisabled = _ref.isDisabled,
      isReadOnly = _ref.isReadOnly,
      rest = (0, _objectWithoutProperties2["default"])(_ref, ["isInvalid", "isRequired", "isDisabled", "isReadOnly"]);
  var context = {
    isRequired: isRequired,
    isDisabled: isDisabled,
    isInvalid: isInvalid,
    isReadOnly: isReadOnly
  };
  return __jsx(FormControlContext.Provider, {
    value: context
  }, __jsx(_Box.Box, (0, _extends2["default"])({
    role: "group",
    ref: ref
  }, rest)));
}); // template for input groups

exports.FormControl = FormControl;

var FormControlWrapper = function FormControlWrapper(props) {
  var children = props.children,
      label = props.label,
      id = props.id,
      helperText = props.helperText,
      error = props.error,
      rest = (0, _objectWithoutProperties2["default"])(props, ["children", "label", "id", "helperText", "error"]);
  return __jsx(FormControl, (0, _extends2["default"])({
    isInvalid: !!error
  }, rest), label && __jsx(_FormLabel.FormLabel, {
    htmlFor: id
  }, label), children, helperText && __jsx(_FormHelperText.FormHelperText, {
    id: "".concat(id, "-help")
  }, helperText), error && __jsx(_FormErrorMessage.FormErrorMessage, {
    id: "".concat(id, "-error")
  }, error));
};

exports.FormControlWrapper = FormControlWrapper;
//# sourceMappingURL=FormControl.js.map