"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _useUpdateEffect = _interopRequireDefault(require("../../hooks/useUpdateEffect"));
var _useForwardRef2 = _interopRequireDefault(require("../../hooks/useForwardRef"));
var _flex = _interopRequireDefault(require("../templates/flex"));
var _excluded = ["open", "duration", "children", "direction", "persist", "closedValue", "overflow", "ref", "initial"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
var measurementByDimension = {
  vertical: "height",
  horizontal: "width"
};
var Animated = (0, _styledComponents["default"])(_flex["default"]).attrs(function (props) {
  return _extends({
    column: true
  }, props);
}).withConfig({
  displayName: "collapsible__Animated",
  componentId: "sc-1yto4nf-0"
})(["transition:", ";", ";"], function (_ref) {
  var duration = _ref.duration,
    measurement = _ref.measurement;
  return "max-" + measurement + " " + duration + "ms ease-in-out";
}, function (_ref2) {
  var measurement = _ref2.measurement,
    maxDimension = _ref2.maxDimension;
  return "max-" + measurement + ": " + maxDimension;
});
var Collapsible = function Collapsible(_ref3) {
  var _ref3$open = _ref3.open,
    open = _ref3$open === void 0 ? false : _ref3$open,
    _ref3$duration = _ref3.duration,
    duration = _ref3$duration === void 0 ? 150 : _ref3$duration,
    children = _ref3.children,
    direction = _ref3.direction,
    _ref3$persist = _ref3.persist,
    persist = _ref3$persist === void 0 ? false : _ref3$persist,
    _ref3$closedValue = _ref3.closedValue,
    closedValue = _ref3$closedValue === void 0 ? 0 : _ref3$closedValue,
    _ref3$overflow = _ref3.overflow,
    overflow = _ref3$overflow === void 0 ? "visible" : _ref3$overflow,
    parentRef = _ref3.ref,
    initial = _ref3.initial,
    rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
  var measurement = measurementByDimension[direction] || measurementByDimension.vertical;
  duration = process.env.NODE_ENV === "test" ? 0 : duration;
  var _useState = (0, _react.useState)(open ? initial : closedValue + "px"),
    dimension = _useState[0],
    setDimension = _useState[1];
  var _useState2 = (0, _react.useState)(open),
    animatedOpen = _useState2[0],
    setAnimatedOpen = _useState2[1];
  var _useForwardRef = (0, _useForwardRef2["default"])(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  (0, _react.useEffect)(function () {
    if (open && ref.current) {
      var size = measurement === measurementByDimension.vertical ? ref.current.scrollHeight : ref.current.scrollWidth;
      setDimension(size + "px");
      var id = setTimeout(function () {
        setDimension("initial"); // let it grow naturally after animation
      }, duration);
      return function () {
        return clearTimeout(id);
      };
    }
  }, []);
  (0, _useUpdateEffect["default"])(function () {
    if (!ref.current) return;
    setDimension(!open ? (measurement === measurementByDimension.vertical ? ref.current.scrollHeight : ref.current.scrollWidth) + "px" : closedValue + "px");
    var requestId = requestAnimationFrame(function () {
      if (!ref.current) return;
      setDimension(open ? (measurement === measurementByDimension.vertical ? ref.current.scrollHeight : ref.current.scrollWidth) + "px" : closedValue + "px");
    });
    if (open) setAnimatedOpen(open);
    var timeoutId = setTimeout(function () {
      cancelAnimationFrame(requestId);
      if (open) {
        setDimension("initial");
        return;
      }
      setDimension(closedValue + "px");
      setAnimatedOpen(false);
    }, duration);
    return function () {
      cancelAnimationFrame(requestId);
      clearTimeout(timeoutId);
    };
  }, [open]);
  var child = (0, _react.useMemo)(function () {
    return (animatedOpen || persist) && (typeof children === "function" ? children(animatedOpen) : children);
  }, [animatedOpen, persist, children]);
  return /*#__PURE__*/_react["default"].createElement(Animated, _extends({
    open: open,
    maxDimension: dimension,
    measurement: measurement,
    duration: duration,
    ref: setRef,
    "data-testid": "collapsible",
    overflow: dimension === "initial" ? overflow : "hidden"
  }, rest), child);
};
var _default = exports["default"] = /*#__PURE__*/(0, _react.memo)(Collapsible);