import React__default, { useState } from 'react';
import '../../../../../_virtual/index2.mjs';
import { useSWRConfig } from '../../../../../node_modules/swr/_internal/dist/index.mjs';
import { Button } from '../components/Button.mjs';
import { Checkbox } from '../components/Checkbox.mjs';
import { FlexBox } from '../shared/components/FlexBox.mjs';
import { Modal } from '../components/Modal.mjs';
import { SettingsContainer } from '../components/SettingsContainer.mjs';
import { SettingsSection } from '../components/SettingsSection.mjs';
import { Typography } from '../components/Typography.mjs';
import { memberGetKey } from '../utils/memberGetKey.mjs';
import { useMutateWithToast } from '../utils/useMutateWithToast.mjs';
import { useRbac } from '../utils/useRbac.mjs';
import { useSelf } from '../utils/useSelf.mjs';
import { useStytchClient } from '../utils/useStytchClient.mjs';
import { useMemberManagementRouterController } from './MemberManagementRouter.mjs';
import { useModalState } from '../shared/components/Modal.mjs';

const AuthManagementSection = ({ member })=>{
    const { isSelf } = useSelf();
    const isMemberSelf = isSelf(member.member_id);
    const { data: canDeleteMemberMfaPhonePerm } = useRbac('stytch.member', 'update.info.delete.mfa-phone');
    const { data: canDeleteMemberMfaTotpPerm } = useRbac('stytch.member', 'update.info.delete.mfa-totp');
    const { data: canDeleteMemberPasswordPerm } = useRbac('stytch.member', 'update.info.delete.password');
    const { data: canRevokeMemberSessionsPerm } = useRbac('stytch.member', 'revoke-sessions');
    const { data: canDeleteSelfMfaPhonePerm } = useRbac('stytch.self', 'update.info.delete.mfa-phone');
    const { data: canDeleteSelfMfaTotpPerm } = useRbac('stytch.self', 'update.info.delete.mfa-totp');
    const { data: canDeleteSelfPasswordPerm } = useRbac('stytch.self', 'update.info.delete.password');
    const { data: canRevokeSelfSessionsPerm } = useRbac('stytch.self', 'revoke-sessions');
    const canDeleteMfaPhonePerm = canDeleteMemberMfaPhonePerm || canDeleteSelfMfaPhonePerm && isMemberSelf;
    const canDeleteMfaTotpPerm = canDeleteMemberMfaTotpPerm || canDeleteSelfMfaTotpPerm && isMemberSelf;
    const memberHasMfaPhone = !!member.mfa_phone_number;
    const memberHasTotp = !!member.totp_registration_id;
    const canDeleteMfaPhone = canDeleteMfaPhonePerm && memberHasMfaPhone;
    const canDeleteMfaTotp = canDeleteMfaTotpPerm && memberHasTotp;
    const canDeletePassword = canDeleteMemberPasswordPerm || canDeleteSelfPasswordPerm && isMemberSelf;
    const canRevokeSessions = canRevokeMemberSessionsPerm || canRevokeSelfSessionsPerm && isMemberSelf;
    const canPerformAnyAction = canDeleteMfaPhone || canDeleteMfaTotp || canDeletePassword || canRevokeSessions;
    const client = useStytchClient();
    const { mutate: mutateSWR } = useSWRConfig();
    const { mutate: deleteMfaTotp } = useMutateWithToast(async (memberId)=>{
        await mutateSWR(memberGetKey(memberId), async ()=>{
            const result = await client.organization.members.deleteMFATOTP(memberId);
            return result.member;
        }, {
            populateCache: true,
            revalidate: false
        });
    });
    const { open: openResetMfaTotpModal, ...resetMfaTotpModalProps } = useModalState(()=>{
        return deleteMfaTotp(member.member_id);
    });
    const { mutate: deleteMfaPhoneNumber } = useMutateWithToast(async (memberId)=>{
        await mutateSWR(memberGetKey(memberId), async ()=>{
            const result = await client.organization.members.deleteMFAPhoneNumber(memberId);
            return result.member;
        }, {
            populateCache: true,
            revalidate: false
        });
    });
    const { open: openResetMfaPhoneNumberModal, ...resetMfaPhoneNumberModalProps } = useModalState(()=>{
        return deleteMfaPhoneNumber(member.member_id);
    });
    const [sendPasswordReset, setSendPasswordReset] = useState(false);
    const { mutate: resetPassword } = useMutateWithToast(async (memberId)=>{
        await mutateSWR(memberGetKey(memberId), async ()=>{
            const result = member.member_password_id ? (await client.organization.members.deletePassword(member.member_password_id)).member : member;
            if (sendPasswordReset) {
                await client.passwords.resetByEmailStart({
                    email_address: member.email_address,
                    organization_id: member.organization_id
                });
            }
            return result;
        }, {
            populateCache: true,
            revalidate: false
        });
    });
    const { open: openResetPasswordModal, ...resetPasswordModalProps } = useModalState(()=>{
        return resetPassword(member.member_id);
    });
    const { mutate: revokeSessions } = useMutateWithToast(async (memberId)=>{
        await mutateSWR(memberGetKey(memberId), async ()=>{
            await client.session.revokeForMember({
                member_id: memberId
            });
        });
    });
    const { open: openRevokeSessionsModal, ...revokeSessionsModalProps } = useModalState(()=>{
        return revokeSessions(member.member_id);
    });
    const { useBlockNavigation } = useMemberManagementRouterController();
    return canPerformAnyAction ? /*#__PURE__*/ React__default.createElement(SettingsContainer, {
        title: "Authentication management",
        useBlockNavigation: useBlockNavigation
    }, /*#__PURE__*/ React__default.createElement(FlexBox, {
        flexDirection: "column",
        gap: 2
    }, (canDeleteMfaPhone || canDeleteMfaTotp) && /*#__PURE__*/ React__default.createElement(SettingsSection, {
        title: "MFA"
    }, /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "body2"
    }, "Reset the Member’s MFA enrollment. If necessary, the Member will have to re-enroll in MFA the next time they login."), /*#__PURE__*/ React__default.createElement(FlexBox, {
        gap: 1
    }, canDeleteMfaTotp && /*#__PURE__*/ React__default.createElement(React__default.Fragment, null, /*#__PURE__*/ React__default.createElement(Modal, {
        ...resetMfaTotpModalProps,
        title: "Reset Member’s TOTP enrollment",
        confirmButtonText: "Reset TOTP",
        warning: true
    }, /*#__PURE__*/ React__default.createElement(Typography, null, "Reset the Member’s TOTP enrollment. If necessary, the Member will have to re-enroll in MFA the next time they login.")), /*#__PURE__*/ React__default.createElement(Button, {
        compact: true,
        variant: "ghost",
        onClick: openResetMfaTotpModal
    }, "Reset TOTP")), canDeleteMfaPhone && /*#__PURE__*/ React__default.createElement(React__default.Fragment, null, /*#__PURE__*/ React__default.createElement(Modal, {
        ...resetMfaPhoneNumberModalProps,
        title: "Reset Member’s SMS OTP enrollment",
        confirmButtonText: "Reset SMS OTP",
        warning: true
    }, /*#__PURE__*/ React__default.createElement(Typography, null, "Reset the Member’s SMS OTP enrollment. If necessary, the Member will have to re-enroll in MFA the next time they login.")), /*#__PURE__*/ React__default.createElement(Button, {
        compact: true,
        variant: "ghost",
        onClick: openResetMfaPhoneNumberModal
    }, "Reset SMS OTP")))), canDeletePassword && /*#__PURE__*/ React__default.createElement(SettingsSection, {
        title: "Password"
    }, /*#__PURE__*/ React__default.createElement(Modal, {
        ...resetPasswordModalProps,
        title: "Ask Member to reset their password?",
        confirmButtonText: "Reset password",
        warning: true
    }, /*#__PURE__*/ React__default.createElement(Typography, null, "Delete the member’s current password and optionally trigger a password reset email by checking the box below."), /*#__PURE__*/ React__default.createElement(Typography, null, "The Member will need to set a new password or use a passwordless authentication method (if allowed by your auth policy) in order to login again."), /*#__PURE__*/ React__default.createElement(Checkbox, {
        label: "Trigger password reset email",
        checked: sendPasswordReset,
        onClick: (checked)=>{
            setSendPasswordReset(checked);
        }
    })), /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "body2"
    }, "Forcing a password reset will delete the Member’s current password and send a reset password email."), /*#__PURE__*/ React__default.createElement(Button, {
        compact: true,
        variant: "ghost",
        onClick: openResetPasswordModal
    }, "Reset password")), canRevokeSessions && /*#__PURE__*/ React__default.createElement(SettingsSection, {
        title: "Sessions"
    }, /*#__PURE__*/ React__default.createElement(Modal, {
        ...revokeSessionsModalProps,
        title: "Revoke Member’s active sessions",
        confirmButtonText: "Revoke all sessions",
        warning: true
    }, /*#__PURE__*/ React__default.createElement(Typography, null, "The Member will be force logged out and all their active sessions will be revoked.")), /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "body2"
    }, "Force logout this Member. All active sessions will be revoked."), /*#__PURE__*/ React__default.createElement(Button, {
        compact: true,
        variant: "ghost",
        onClick: openRevokeSessionsModal
    }, "Revoke all sessions")))) : null;
};

export { AuthManagementSection };
//# sourceMappingURL=AuthManagementSection.mjs.map
