import { useState, useContext } from 'react';
import PropTypes from 'prop-types';
import MenuItem from '@mui/material/MenuItem';
import AddIcon from '@mui/icons-material/Add';
import LogoutIcon from '@mui/icons-material/Logout';
import ListItemIcon from '@mui/material/ListItemIcon';
import ListItemText from '@mui/material/ListItemText';
import { LocaleContext } from '@arcblock/ux/lib/Locale/context';
import BlockAccessIcon from '@arcblock/icons/lib/ForbidLoginIcon';
import AllowAccessIcon from '@arcblock/icons/lib/AllowLoginIcon';
import { getWalletDid } from '@blocklet/meta/lib/did-utils';
import { useConfirm } from '@arcblock/ux/lib/Dialog';
import { useMemoizedFn } from 'ahooks';
import { useTeamContext } from '../../contexts/team';
import Actions from '../../actions';
import IssuePassport from './issue-passport';
import ToggleAccess from './toggle-access';
import { useNodeContext } from '../../contexts/node';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
export default function MemberActions({
  type,
  user,
  onIssuePassport = () => {},
  onRefresh = () => {}
}) {
  const {
    teamDid,
    roles
  } = useTeamContext();
  const {
    confirmHolder,
    confirmApi
  } = useConfirm();
  const {
    t
  } = useContext(LocaleContext);
  const [enablePassportIssuance, setEnablePassportIssuance] = useState(false);
  const {
    api
  } = useNodeContext();
  const logoutUser = useMemoizedFn(async (removeAll = false) => {
    const params = {
      teamDid,
      userDid: user.did,
      remove: removeAll
    };
    if (!removeAll) {
      params.appPid = teamDid;
    }
    await api.logoutUser({
      input: params
    });
  });
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [confirmHolder, /*#__PURE__*/_jsx(Actions, {
      "data-cy": "member-actions",
      actions: [{
        icon: /*#__PURE__*/_jsx(AddIcon, {
          fontSize: "small"
        }),
        text: t('team.passport.issue'),
        'data-cy': 'action-issue-passport',
        onClick: e => {
          e.stopPropagation();
          setEnablePassportIssuance(true);
        }
      },
      // eslint-disable-next-line react/no-unstable-nested-components
      ({
        close
      }) => /*#__PURE__*/_jsx(ToggleAccess, {
        user: user,
        children: ({
          open: o
        }) => /*#__PURE__*/_jsxs(MenuItem, {
          dense: true,
          onClick: e => {
            e.stopPropagation();
            close();
            o(e);
          },
          "data-cy": "action-toggle-block",
          children: [/*#__PURE__*/_jsx(ListItemIcon, {
            style: {
              minWidth: 24,
              marginRight: 8
            },
            children: user.approved ? /*#__PURE__*/_jsx(BlockAccessIcon, {}) : /*#__PURE__*/_jsx(AllowAccessIcon, {})
          }), /*#__PURE__*/_jsx(ListItemText, {
            primary: user.approved ? t('team.member.blockAccess') : t('team.member.allowAccess')
          })]
        })
      }, "toggle-approval"), type === 'blocklet' && {
        icon: /*#__PURE__*/_jsx(LogoutIcon, {
          fontSize: "small"
        }),
        text: t('team.member.forceLogout'),
        onClick: e => {
          e.stopPropagation();
          confirmApi.open({
            title: t('team.member.forceLogout'),
            content: e.altKey ? t('team.member.forceLogoutRemoveDescription') : t('team.member.forceLogoutDescription'),
            confirmButtonText: t('common.confirm'),
            cancelButtonText: t('common.cancel'),
            onClick: event => event.stopPropagation(),
            onConfirm: async () => {
              await logoutUser(e.altKey);
              await confirmApi.close();
              onRefresh();
            }
          });
        }
      }].filter(Boolean)
    }), enablePassportIssuance && /*#__PURE__*/_jsx(IssuePassport, {
      teamDid: teamDid,
      ownerDid: user.did,
      needReceive: getWalletDid(user) === user.did,
      roles: roles,
      onCancel: () => setEnablePassportIssuance(false),
      onSuccess: () => {
        onIssuePassport();
        setEnablePassportIssuance(false);
      }
    })]
  });
}
MemberActions.propTypes = {
  type: PropTypes.oneOf(['server', 'blocklet']).isRequired,
  user: PropTypes.object.isRequired,
  onIssuePassport: PropTypes.func,
  onRefresh: PropTypes.func
};