"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ExpandableContent = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _faChevronDown = require("@fortawesome/free-solid-svg-icons/faChevronDown");
var _faChevronUp = require("@fortawesome/free-solid-svg-icons/faChevronUp");
var _button = require("../button");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
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 (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); }
/** Clips content to a maximum height and shows a Show more/Show less ghost button when the content overflows.
 * @midnight-gray-supported
 * @alpine-light-supported
 */
const ExpandableContent = ({
  children,
  maxHeight,
  className
}) => {
  const [isExpanded, setIsExpanded] = (0, _react.useState)(false);
  const [isOverflowing, setIsOverflowing] = (0, _react.useState)(false);
  const contentRef = (0, _react.useRef)(null);
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  (0, _react.useLayoutEffect)(() => {
    if (contentRef.current) {
      setIsOverflowing(contentRef.current.scrollHeight > maxHeight);
    }
  }, [children, maxHeight]);
  const isClipped = isOverflowing && !isExpanded;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: (0, _classnames.default)('expandable-content', className),
    "test-id": "expandable-content",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      ref: contentRef,
      className: "expandable-content-body"
      // eslint-disable-next-line react/forbid-dom-props
      ,
      style: isClipped ? {
        maxHeight,
        overflow: 'hidden'
      } : undefined,
      "test-id": "expandable-content-body",
      children: children
    }), isOverflowing && /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.Button, {
      accentType: _button.ACCENT_TYPES.GHOST,
      onClick: () => setIsExpanded(prev => !prev),
      className: "expandable-content-toggle",
      rightIcon: isExpanded ? _faChevronUp.faChevronUp : _faChevronDown.faChevronDown,
      testId: "expandable-content-toggle",
      children: isExpanded ? t('Show less') : t('Show more')
    })]
  });
};
exports.ExpandableContent = ExpandableContent;