"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.MotionWrapper = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _compiled = require("@atlaskit/primitives/compiled");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var MotionWrapper = exports.MotionWrapper = function MotionWrapper(_ref) {
  var children = _ref.children,
    _ref$isFadeIn = _ref.isFadeIn,
    isFadeIn = _ref$isFadeIn === void 0 ? false : _ref$isFadeIn,
    _ref$minHeight = _ref.minHeight,
    minHeight = _ref$minHeight === void 0 ? 0 : _ref$minHeight,
    show = _ref.show,
    _ref$showTransition = _ref.showTransition,
    showTransition = _ref$showTransition === void 0 ? false : _ref$showTransition;
  var _useState = (0, _react.useState)(0),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    height = _useState2[0],
    setHeight = _useState2[1];
  var ref = (0, _react.useRef)(null);
  var transition = isFadeIn ? 'height, opacity' : 'height';
  (0, _react.useEffect)(function () {
    if (show && ref.current) {
      // set the height of the error message container explicitly to the height
      // of its contents (including anything hidden due to overflow) so that we
      // can animate the height of the container
      setHeight(ref.current.scrollHeight);
    } else {
      setHeight(0);
    }
  }, [children, show]);
  return show ? /*#__PURE__*/_react.default.createElement(_compiled.Box, {
    style: {
      height: height,
      minHeight: minHeight,
      // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop, @atlaskit/design-system/use-tokens-typography -- Ignored via go/DSP-18766
      lineHeight: 0,
      // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
      overflow: 'hidden',
      // a fit for purpose state variable to control when the 'visible' styles should be appled
      opacity: height > 0 ? 1 : 0,
      // we only want to transition opacity and height when the error message is initially shown
      // not if its to be shown on mount
      transitionProperty: showTransition ? transition : 'none',
      // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- Ignored via go/DSP-18766
      transitionDuration: '0.3s'
    },
    ref: ref
  }, children) : null;
};
var _default = exports.default = MotionWrapper;