import React__default, { useState } from 'react';
import '../../../../../_virtual/index2.mjs';
import { useSWRConfig } from '../../../../../node_modules/swr/_internal/dist/index.mjs';
import { Modal } from '../components/Modal.mjs';
import { Typography } from '../components/Typography.mjs';
import { memberGetKey } from '../utils/memberGetKey.mjs';
import { useMutateWithToast } from '../utils/useMutateWithToast.mjs';
import { useStytchClient } from '../utils/useStytchClient.mjs';
import { useUpdateCachedMember } from '../utils/useUpdateCachedMember.mjs';
import { useModalState } from '../shared/components/Modal.mjs';

const getActionCopy = ({ status, isEmailVerified })=>{
    if (status === 'invited') {
        const description = 'The invited Member will be deleted and cannot be reactivated. Resending an invite to this email will create a new Member record.';
        return {
            action: 'Revoke invite',
            description,
            modalTitle: 'Revoke invite?',
            modalAction: 'Revoke invite',
            modalDescription: description
        };
    }
    if (status === 'deleted') {
        const description = 'Reactivating the Member will allow them to log in under their previous account.';
        return {
            action: 'Reactivate Member',
            description,
            modalTitle: 'Reactivate Member?',
            modalAction: 'Reactivate',
            modalDescription: description
        };
    }
    const description = isEmailVerified ? 'All of the Member’s authentication factors will be deleted and all active sessions will be revoked. You will be able to reactivate the Member and restore their email, but other authentication factors and Role assignments will not be restored.' : 'All of the Member’s authentication factors will be deleted and all active sessions will be revoked. The Member’s email is not verified, so you will not be able to reactivate the Member or restore their email.';
    const modalDescription = isEmailVerified ? description : /*#__PURE__*/ React__default.createElement(React__default.Fragment, null, /*#__PURE__*/ React__default.createElement(Typography, null, "All of the Member’s authentication factors will be deleted and all active sessions will be revoked."), /*#__PURE__*/ React__default.createElement(Typography, {
        color: "error"
    }, "The Member’s email is not verified, so you will not be able to reactivate the Member or restore their email."));
    return {
        action: 'Deactivate Member',
        description,
        modalTitle: 'Deactivate Member?',
        modalAction: 'Deactivate Member',
        modalDescription
    };
};
const useMemberActivation = (revalidateMemberList)=>{
    const client = useStytchClient();
    const { mutate: mutateSWR } = useSWRConfig();
    const { mutate: deleteMember } = useMutateWithToast(async (memberId)=>{
        return await mutateSWR(memberGetKey(memberId), async ()=>{
            await client.organization.members.delete(memberId);
        });
    });
    const { mutate: reactivateMember } = useMutateWithToast(async (memberId)=>{
        return await mutateSWR(memberGetKey(memberId), async ()=>{
            const result = await client.organization.members.reactivate(memberId);
            return result.member;
        }, {
            populateCache: true,
            revalidate: false
        });
    });
    const { markMemberDeleted, setMember } = useUpdateCachedMember();
    // Store a cached member upon opening the confirmation modal so that the
    // action being confirmed does not change after the confirmation modal is
    // shown. e.g., if an admin clicks "deactivate" for an active member just
    // before the Member is deactivated out of band, we do not want the admin
    // "confirming" the action to erroneously reactivate the (now-deleted) Member.
    const [memberPendingAction, setMemberPendingAction] = useState();
    const { open: openConfirmationModal, close: closeConfirmationModal, ...deleteMemberModalProps } = useModalState(async ()=>{
        if (!memberPendingAction) {
            // member should be available at this point
            return;
        }
        if (memberPendingAction.status === 'deleted') {
            const member = await reactivateMember(memberPendingAction.member_id);
            if (member) {
                setMember(member);
            }
        } else {
            await deleteMember(memberPendingAction.member_id);
            markMemberDeleted(memberPendingAction.member_id);
        }
        revalidateMemberList?.();
    });
    const close = ()=>{
        closeConfirmationModal();
    };
    const openModal = (member)=>{
        setMemberPendingAction(member);
        openConfirmationModal();
    };
    const actionCopy = getActionCopy({
        status: memberPendingAction?.status ?? 'active',
        isEmailVerified: memberPendingAction?.email_address_verified ?? false
    });
    const modal = /*#__PURE__*/ React__default.createElement(Modal, {
        ...deleteMemberModalProps,
        close: close,
        title: actionCopy.modalTitle,
        confirmButtonText: actionCopy.modalAction,
        warning: true
    }, typeof actionCopy.modalDescription === 'string' ? /*#__PURE__*/ React__default.createElement(Typography, null, actionCopy.modalDescription) : actionCopy.modalDescription);
    return {
        openModal,
        modal
    };
};

export { getActionCopy, useMemberActivation };
//# sourceMappingURL=useMemberActivation.mjs.map
