"use strict";

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

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

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

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

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

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

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

var _Typography = require("../Typography");

var _styledComponents = _interopRequireWildcard(require("styled-components"));

var _templateObject, _templateObject2, _templateObject3;

var FormError = function FormError(_ref) {
  var errors = _ref.errors,
      fieldName = _ref.fieldName,
      text = _ref.text,
      _ref$variant = _ref.variant,
      variant = _ref$variant === void 0 ? '7' : _ref$variant,
      _ref$color = _ref.color,
      color = _ref$color === void 0 ? 'danger' : _ref$color,
      center = _ref.center,
      absolute = _ref.absolute,
      errorRef = _ref.errorRef,
      rest = (0, _objectWithoutPropertiesLoose2["default"])(_ref, ["errors", "fieldName", "text", "variant", "color", "center", "absolute", "errorRef"]);

  var getError = function getError() {
    var _errors$fieldName;

    if (!errors || !fieldName) return null;
    if (typeof errors[fieldName] === 'string' && errors[fieldName].trim()) return errors[fieldName];
    return ((_errors$fieldName = errors[fieldName]) == null ? void 0 : _errors$fieldName.message) || null;
  };

  return /*#__PURE__*/_react["default"].createElement(ErrorWrapper, {
    absolute: absolute,
    center: center,
    className: "mt-1",
    ref: errorRef
  }, /*#__PURE__*/_react["default"].createElement(_Typography.Typography, (0, _extends2["default"])({
    color: color,
    as: "span",
    variant: variant
  }, rest), text || getError()));
};

exports.FormError = FormError;

var ErrorWrapper = _styledComponents["default"].div(_templateObject || (_templateObject = (0, _taggedTemplateLiteralLoose2["default"])(["\n  ", "\n\n  ", "\n"])), function (props) {
  return props.center && (0, _styledComponents.css)(_templateObject2 || (_templateObject2 = (0, _taggedTemplateLiteralLoose2["default"])(["\n      text-align: center;\n    "])));
}, function (props) {
  return props.absolute && (0, _styledComponents.css)(_templateObject3 || (_templateObject3 = (0, _taggedTemplateLiteralLoose2["default"])(["\n      line-height: 0;\n      position: relative;\n\n      & > span {\n        position: absolute;\n        bottom: -0.7rem;\n      }\n    "])));
});
//# sourceMappingURL=index.js.map