/* eslint-disable react/require-default-props */
import { Box, Card, CardContent, Collapse } from '@mui/material';
import { useCreation, useMemoizedFn, useReactive } from 'ahooks';
import { Icon } from '@iconify/react';
import expandMoreIcon from '@iconify-icons/material-symbols/expand-more';
import isUndefined from 'lodash/isUndefined';
import PropTypes from 'prop-types';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export default function BlockletSecurityCollapse({
  title,
  actions,
  children = null,
  defaultValue = true,
  value,
  onChange,
  showExpand = true
}) {
  const isControlled = !isUndefined(value);
  const currentState = useReactive({
    open: defaultValue
  });
  const showOpen = useCreation(() => {
    if (isControlled) {
      return value;
    }
    return currentState.open;
  }, [value, isControlled, currentState.open]);
  const handleChange = useMemoizedFn(() => {
    if (isControlled) {
      onChange(!showOpen);
    } else {
      currentState.open = !currentState.open;
    }
  });
  return /*#__PURE__*/_jsxs(Card, {
    variant: "outlined",
    children: [/*#__PURE__*/_jsxs(Box, {
      sx: {
        display: 'flex',
        justifyContent: 'space-between',
        alignItems: 'center',
        cursor: 'pointer',
        px: 2,
        py: 1,
        backgroundColor: 'grey.50'
      },
      onClick: handleChange,
      children: [/*#__PURE__*/_jsx(Box, {
        sx: {
          fontWeight: 'bold'
        },
        children: title
      }), /*#__PURE__*/_jsxs(Box, {
        sx: {
          display: 'flex',
          alignItems: 'center'
        },
        children: [actions, showExpand ? /*#__PURE__*/_jsx(Box, {
          component: Icon,
          icon: expandMoreIcon,
          sx: {
            transform: showOpen ? 'rotate(180deg)' : 'rotate(0deg)',
            transition: 'transform 0.2s ease',
            fontSize: '2rem'
          }
        }) : null]
      })]
    }), /*#__PURE__*/_jsx(Collapse, {
      in: showOpen,
      children: /*#__PURE__*/_jsx(CardContent, {
        children: children
      })
    })]
  });
}
BlockletSecurityCollapse.propTypes = {
  title: PropTypes.string.isRequired,
  defaultValue: PropTypes.bool,
  value: PropTypes.bool,
  actions: PropTypes.node,
  children: PropTypes.node,
  onChange: PropTypes.func,
  showExpand: PropTypes.bool
};