var _excluded = ["open", "duration", "children", "direction", "persist", "closedValue", "overflow"];
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); }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { memo, useMemo, useState, forwardRef } from "react";
import styled from "styled-components";
import useUpdateEffect from "../../hooks/useUpdateEffect";
import useForwardRef from "../../hooks/useForwardRef";
import Flex from "../templates/flex";
var measurementByDimension = {
  vertical: "height",
  horizontal: "width"
};
var Animated = styled(Flex).attrs(function (props) {
  return _objectSpread({
    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 = /*#__PURE__*/forwardRef(function (_ref3, parentRef) {
  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,
    rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
  var measurement = measurementByDimension[direction] || measurementByDimension.vertical;
  duration = process.env.NODE_ENV === "test" ? 0 : duration;
  var _useState = useState(open ? "initial" : closedValue + "px"),
    dimension = _useState[0],
    setDimension = _useState[1];
  var _useState2 = useState(open),
    animatedOpen = _useState2[0],
    setAnimatedOpen = _useState2[1];
  var _useForwardRef = useForwardRef(parentRef),
    ref = _useForwardRef[0],
    setRef = _useForwardRef[1];
  useUpdateEffect(function () {
    if (!ref.current) return;
    setDimension(!open ? (measurement === measurementByDimension.vertical ? ref.current.scrollHeight : ref.current.scrollHeight) + "px" : closedValue + "px");
    var requestId = requestAnimationFrame(function () {
      if (!ref.current) return;
      setDimension(open ? (measurement === measurementByDimension.vertical ? ref.current.scrollHeight : ref.current.scrollHeight) + "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 = useMemo(function () {
    return (animatedOpen || persist) && (typeof children === "function" ? children(animatedOpen) : children);
  }, [animatedOpen, persist, children]);
  return /*#__PURE__*/React.createElement(Animated, _extends({
    open: open,
    maxDimension: dimension,
    measurement: measurement,
    duration: duration,
    ref: setRef,
    "data-testid": "collapsible",
    overflow: dimension === "initial" ? overflow : "hidden"
  }, rest), child);
});
export default /*#__PURE__*/memo(Collapsible);