import { useCallback } from 'react';
import { unstable_serialize } from '../../../../../node_modules/swr/core/dist/index.mjs';
import { isFetchMembersResponse } from './fetchMembers.mjs';
import { memberGetKey } from './memberGetKey.mjs';
import { useSWRConfig } from '../../../../../node_modules/swr/_internal/dist/index.mjs';

const useUpdateCachedMember = ()=>{
    const { mutate, cache } = useSWRConfig();
    const updateCachedMember = useCallback((memberId, setMember)=>{
        for (const key of cache.keys()){
            const data = cache.get(key);
            if (isFetchMembersResponse(data?.data)) {
                cache.set(key, {
                    ...data,
                    data: {
                        ...data.data,
                        members: data.data.members.map((cachedMember)=>cachedMember.member_id === memberId ? setMember(cachedMember) : cachedMember)
                    }
                });
            }
            if (key === unstable_serialize(()=>memberGetKey(memberId))) {
                const cachedMember = cache.get(key)?.data;
                if (cachedMember) {
                    mutate(key, setMember(cachedMember));
                }
            }
        }
    }, [
        cache,
        mutate
    ]);
    const setMember = useCallback((member)=>{
        updateCachedMember(member.member_id, ()=>member);
    }, [
        updateCachedMember
    ]);
    const markMemberDeleted = useCallback((memberId)=>{
        updateCachedMember(memberId, (member)=>({
                ...member,
                status: 'deleted'
            }));
    }, [
        updateCachedMember
    ]);
    return {
        setMember,
        markMemberDeleted
    };
};

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