"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CollapsibleCard = void 0;
var _framerMotion = require("framer-motion");
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _lodash = require("lodash");
var _ = require("../..");
var _Hooks = require("../../common/Hooks");
var _utils = require("../../common/utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var StyledButton = _styledComponents.default.div.withConfig({
  displayName: "CollapsibleCard__StyledButton",
  componentId: "sc-xk1kem-0"
})(["position:relative;background:none;cursor:pointer;display:block;width:100%;border:0;box-sizing:border-box;padding:", ";"], function (p) {
  var py = p.theme.spacing.unit(p.$py);
  var px = p.theme.spacing.unit(p.$px);
  return p.$actionExists ? "".concat(py, "px ").concat(px / 2, "px ").concat(py, "px ").concat(px, "px") : "".concat(py, "px ").concat(px, "px");
});
var StyledFlexbox = (0, _styledComponents.default)(_.Flexbox).withConfig({
  displayName: "CollapsibleCard__StyledFlexbox",
  componentId: "sc-xk1kem-1"
})(["padding-right:", "px;"], function (p) {
  return p.theme.spacing.unit(p.$px);
});
var AnimatedChevronUp = (0, _styledComponents.default)(_.Icon.ChevronUp8).withConfig({
  displayName: "CollapsibleCard__AnimatedChevronUp",
  componentId: "sc-xk1kem-2"
})(["transform:", ";transform-origin:center center;transition:transform 0.16s ease-out;"], function (p) {
  return p.$collapsed ? 'rotate(180deg)' : 'rotate(0)';
});
var CollapsibleCard = exports.CollapsibleCard = function CollapsibleCard(_ref) {
  var className = _ref.className,
    title = _ref.title,
    children = _ref.children,
    _ref$collapsedInitial = _ref.collapsedInitial,
    collapsedInitial = _ref$collapsedInitial === void 0 ? false : _ref$collapsedInitial,
    _ref$heading = _ref.heading,
    heading = _ref$heading === void 0 ? 'h2' : _ref$heading,
    _ref$noIndicator = _ref.noIndicator,
    noIndicator = _ref$noIndicator === void 0 ? false : _ref$noIndicator,
    _ref$onClick = _ref.onClick,
    onClick = _ref$onClick === void 0 ? function () {} : _ref$onClick,
    expandElement = _ref.expandElement,
    _ref$titleRowPaddingX = _ref.titleRowPaddingX,
    titleRowPaddingX = _ref$titleRowPaddingX === void 0 ? 5 : _ref$titleRowPaddingX,
    _ref$titleRowPaddingY = _ref.titleRowPaddingY,
    titleRowPaddingY = _ref$titleRowPaddingY === void 0 ? 5 : _ref$titleRowPaddingY,
    _ref$action = _ref.action,
    ActionComponent = _ref$action === void 0 ? false : _ref$action,
    _ref$fullWidthTitle = _ref.fullWidthTitle,
    fullWidthTitle = _ref$fullWidthTitle === void 0 ? false : _ref$fullWidthTitle;
  var _useState = (0, _react.useState)('0px'),
    _useState2 = _slicedToArray(_useState, 2),
    height = _useState2[0],
    setHeight = _useState2[1];
  var _useState3 = (0, _react.useState)(collapsedInitial),
    _useState4 = _slicedToArray(_useState3, 2),
    collapsed = _useState4[0],
    setCollapsed = _useState4[1];
  var collapsibleRef = (0, _react.useRef)(null);
  var contentRef = (0, _react.useRef)(null);
  var debouncedSetHeight = (0, _lodash.debounce)(function () {
    var _contentRef$current;
    return setHeight("".concat(contentRef === null || contentRef === void 0 || (_contentRef$current = contentRef.current) === null || _contentRef$current === void 0 ? void 0 : _contentRef$current.scrollHeight, "px"));
  }, 50);
  (0, _Hooks.useSafeLayoutEffect)(function () {
    var _collapsibleRef$curre;
    var collapsibleHeight = collapsibleRef === null || collapsibleRef === void 0 || (_collapsibleRef$curre = collapsibleRef.current) === null || _collapsibleRef$curre === void 0 ? void 0 : _collapsibleRef$curre.scrollHeight;
    setHeight("".concat(collapsed ? 0 : collapsibleHeight, "px"));
  }, [collapsed]);
  (0, _react.useEffect)(function () {
    if (!contentRef.current || collapsed) return;
    var resizeObserver = new ResizeObserver(function () {
      debouncedSetHeight();
    });
    resizeObserver.observe(contentRef.current);
    return function () {
      return resizeObserver.disconnect();
    };
  }, [debouncedSetHeight, collapsed]);
  var toggle = function toggle(e) {
    onClick(e);
    setCollapsed(function (prev) {
      return !prev;
    });
  };
  var CollapseButton = _react.default.createElement(StyledButton, {
    onClick: toggle,
    $collapsed: collapsed,
    $noIndicator: noIndicator,
    "aria-expanded": !collapsed,
    $py: titleRowPaddingY,
    $px: titleRowPaddingX,
    $actionExists: !!ActionComponent
  }, _react.default.createElement(_.Flexbox, {
    container: true,
    gap: 4,
    alignItems: "center",
    justifyContent: "space-between"
  }, _react.default.createElement(_.Flexbox, {
    item: true,
    width: fullWidthTitle ? '100%' : 'auto'
  }, _react.default.createElement(_.Typography, {
    type: "title3",
    as: heading
  }, title)), _react.default.createElement(_.Flexbox, {
    item: true
  }, function () {
    if ((0, _utils.isFunction)(expandElement)) return expandElement(collapsed);
    if ((0, _utils.isElement)(expandElement)) return expandElement;
    return _react.default.createElement(AnimatedChevronUp, {
      $collapsed: collapsed
    });
  }())));
  var CardHeaderTop = function CardHeaderTop() {
    return ActionComponent ? _react.default.createElement(StyledFlexbox, {
      container: true,
      $px: titleRowPaddingX
    }, CollapseButton, ActionComponent) : CollapseButton;
  };
  return _react.default.createElement(_.Card, {
    className: className
  }, _react.default.createElement(CardHeaderTop, null), _react.default.createElement(_framerMotion.motion.div, {
    ref: collapsibleRef,
    initial: !collapsedInitial,
    style: {
      overflow: 'hidden'
    },
    animate: {
      height: height
    },
    transition: {
      type: 'linear',
      duration: 0.16
    }
  }, _react.default.createElement("div", {
    ref: contentRef
  }, children)));
};