import { useCallback } from 'react';
import '../../../../../_virtual/index2.mjs';
import { useSWRConfig } from '../../../../../node_modules/swr/_internal/dist/index.mjs';
import { memberGetKey } from './memberGetKey.mjs';
import { useMutateWithToast } from './useMutateWithToast.mjs';
import { useStytchClient } from './useStytchClient.mjs';
import { useUpdateCachedMember } from './useUpdateCachedMember.mjs';

const useMutateMember = ()=>{
    const { mutate: mutateSWR } = useSWRConfig();
    const client = useStytchClient();
    const { setMember } = useUpdateCachedMember();
    return useMutateWithToast(useCallback(async (properties)=>{
        const result = await mutateSWR(memberGetKey(properties.member_id), async ()=>{
            const resp = await client.organization.members.update(properties);
            return resp.member;
        }, {
            // note that optimisticData is typed as `any`
            optimisticData: (member)=>{
                // Exclude roles from the optimistically updated version, because
                // its shape is different in the response and requires special
                // handling
                const { roles, ...rest } = properties;
                return {
                    ...member,
                    ...rest
                };
            },
            rollbackOnError: true,
            populateCache: true,
            revalidate: false
        });
        if (result) {
            // Update the member in the cache, which includes matching entries in
            // any cached member lists
            setMember(result);
        }
    }, [
        client.organization.members,
        mutateSWR,
        setMember
    ]));
};

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