import { useMutation, useQueryClient, } from '@tanstack/react-query';
import { NullSeamClientError, useSeamClient } from '../../../lib/seam/use-seam-client.js';
export function useUpdateAccessCode() {
    const { client } = useSeamClient();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (variables) => {
            if (client === null)
                throw new NullSeamClientError();
            await client.accessCodes.update(variables);
        },
        onSuccess: (_data, variables) => {
            queryClient.setQueryData(['access_codes', 'get', { access_code_id: variables.access_code_id }], (accessCode) => {
                if (accessCode == null) {
                    return;
                }
                return {
                    ...accessCode,
                    code: variables.code ?? accessCode.code,
                    name: variables.name ?? accessCode.name,
                    starts_at: variables.starts_at ?? accessCode.starts_at,
                    ends_at: variables.ends_at ?? accessCode.ends_at,
                    type: variables.type ?? accessCode.type,
                    status: 'setting',
                };
            });
            queryClient.setQueryData(['access_codes', 'list'], (accessCodes) => {
                if (accessCodes == null) {
                    return accessCodes;
                }
                return accessCodes.map((accessCode) => {
                    if (accessCode.access_code_id === variables.access_code_id) {
                        return {
                            ...accessCode,
                            code: variables.code ?? accessCode.code,
                            name: variables.name ?? accessCode.name,
                            status: 'setting',
                        };
                    }
                    return accessCode;
                });
            });
        },
    });
}
//# sourceMappingURL=use-update-access-code.js.map