"use strict";

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

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

exports.__esModule = true;
exports.default = void 0;

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

var _propTypes = _interopRequireDefault(require("prop-types"));

var _useGeneratedId = _interopRequireDefault(require("../hooks/useGeneratedId"));

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

var _contexts = require("./contexts");

// Works only for native inputs where the value is primitive value
var SuperField = function SuperField(_ref) {
  var idProp = _ref.id,
      hasError = _ref.hasError,
      disabled = _ref.disabled,
      readOnly = _ref.readOnly,
      children = _ref.children;

  var _useState = (0, _react.useState)(false),
      isFocused = _useState[0],
      setIsFocused = _useState[1];

  var _useState2 = (0, _react.useState)(false),
      isInputFilledIn = _useState2[0],
      setIsInputFilledIn = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      isLabelShrank = _useState3[0],
      setIsLabelShrank = _useState3[1];

  var generatedStub = (0, _useGeneratedId.default)();
  var generatedId = "field-" + generatedStub;
  var context = (0, _react.useMemo)(function () {
    return {
      id: idProp || generatedId,
      isFocused: isFocused,
      onFocus: function onFocus() {
        return setIsFocused(true);
      },
      onBlur: function onBlur() {
        return setIsFocused(false);
      },
      hasError: hasError,
      isInputFilledIn: isInputFilledIn,
      onFill: function onFill() {
        return setIsInputFilledIn(true);
      },
      onEmpty: function onEmpty() {
        return setIsInputFilledIn(false);
      },
      isLabelShrank: isLabelShrank,
      onLabelExpand: function onLabelExpand() {
        return setIsLabelShrank(false);
      },
      onLabelShrank: function onLabelShrank() {
        return setIsLabelShrank(true);
      },
      isDisabled: disabled,
      isReadOnly: readOnly
    };
  }, [idProp, generatedId, isFocused, hasError, isInputFilledIn, isLabelShrank, disabled, readOnly]);
  return (
    /*#__PURE__*/
    _react.default.createElement(_contexts.SuperFieldContext.Provider, {
      value: context
    },
    /*#__PURE__*/
    _react.default.createElement(_Box.default, {
      sx: {
        position: 'relative',
        pt: 3,
        fontFamily: 'body',
        color: function () {
          if (hasError) {
            return 'danger';
          } else if (disabled) {
            return 'gray';
          } else if (isLabelShrank) {
            return 'inherit';
          } else {
            return 'gray';
          }
        }()
      }
    }, children))
  );
};

process.env.NODE_ENV !== "production" ? SuperField.propTypes = {
  children: _propTypes.default.node,
  disabled: _propTypes.default.bool,
  hasError: _propTypes.default.bool,
  id: _propTypes.default.string,
  readOnly: _propTypes.default.bool
} : void 0;
var _default = SuperField;
exports.default = _default;