"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.Collapse = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _reactTransitionGroup = require("react-transition-group");
var _react = _interopRequireWildcard(require("react"));
var _CollapseStCss = require("./Collapse.st.css.js");
var _mobxReactLite = require("mobx-react-lite");
var _mobx = require("mobx");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/Collapse/Collapse.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (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 (const 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); }
function _Collapse(props) {
  const {
    children,
    timeout,
    state,
    onExit,
    onExiting,
    onExited,
    onEnter,
    onEntering,
    onEntered,
    dataHook,
    rootRef,
    style: styleProp,
    ...rest
  } = props;
  const ref = (0, _react.useRef)(null);
  const [initialStyle, setInitialStyle] = (0, _react.useState)(false);
  const enterTimeoutRef = (0, _react.useRef)();
  const exitTimeoutRef = (0, _react.useRef)();
  const {
    status,
    show
  } = state;
  (0, _react.useLayoutEffect)(() => {
    setInitialStyle(true);
    (0, _mobx.runInAction)(() => {
      if (state.show) {
        state.status = 'entered';
      }
    });
  }, []);
  (0, _react.useEffect)(() => {
    return () => {
      window.clearTimeout(enterTimeoutRef.current);
      window.clearTimeout(exitTimeoutRef.current);
    };
  }, []);
  const statusStyle = (0, _react.useMemo)(() => {
    var _ref$current;
    return initialStyle ? {
      ...(status !== 'afterEntered' && {
        overflow: 'hidden'
      }),
      ...(status !== 'afterEntered' && {
        height: status === 'exited' || status === 'exiting' ? 0 : (_ref$current = ref.current) == null ? void 0 : _ref$current.scrollHeight
      })
    } : {};
  }, [status, initialStyle]);
  const style = (0, _react.useMemo)(() => ({
    transitionDuration: `${timeout}ms`,
    ...styleProp,
    ...statusStyle
  }), [timeout, styleProp, statusStyle]);
  return /*#__PURE__*/_react.default.createElement(_reactTransitionGroup.Transition, (0, _extends2.default)({
    in: show,
    onEnter: (node, isAppearing) => {
      state.setStatus('enter');
      onEnter == null || onEnter(node, isAppearing);
    },
    onEntering: (node, isAppearing) => {
      state.setStatus('entering');
      onEntering == null || onEntering(node, isAppearing);
    },
    onEntered: (node, isAppearing) => {
      state.setStatus('entered');
      enterTimeoutRef.current = window.setTimeout((0, _mobx.action)(() => {
        if (state.status === 'entered') {
          state.setStatus('afterEntered');
        }
      }), 0);
      onEntered == null || onEntered(node, isAppearing);
    },
    onExit: node => {
      state.setStatus('exit');
      onExit == null || onExit(node);
    },
    onExiting: node => {
      state.setStatus('beforeExit');
      exitTimeoutRef.current = window.setTimeout((0, _mobx.action)(() => {
        if (state.status === 'beforeExit') {
          state.setStatus('exiting');
        }
      }), 0);
      onExiting == null || onExiting(node);
    },
    onExited: node => {
      state.setStatus('exited');
      onExited == null || onExited(node);
    },
    timeout: timeout
  }, rest, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 92,
      columnNumber: 5
    }
  }), /*#__PURE__*/_react.default.createElement("div", {
    "data-hook": dataHook,
    ref: el => {
      ref.current = el;
      rootRef == null || rootRef(el);
    },
    className: (0, _CollapseStCss.st)(_CollapseStCss.classes.root),
    style: style,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 137,
      columnNumber: 7
    }
  }, children));
}
const Collapse = exports.Collapse = (0, _mobxReactLite.observer)(_Collapse);
//# sourceMappingURL=Collapse.js.map