import { useMutation, useQueryClient, } from '@tanstack/react-query';
import { NullSeamClientError, useSeamClient } from '../../../lib/seam/use-seam-client.js';
export function useUpdateDeviceName(params) {
    const { client } = useSeamClient();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (variables) => {
            if (client === null)
                throw new NullSeamClientError();
            await client.devices.update(variables);
        },
        onSuccess: (_data, variables) => {
            queryClient.setQueryData(['devices', 'get', params], (device) => {
                if (device == null) {
                    return;
                }
                return getUpdatedDevice(device, variables.name ?? device.properties.name);
            });
            queryClient.setQueryData(['devices', 'list', { device_id: variables.device_id }], (devices) => {
                if (devices == null) {
                    return [];
                }
                return devices.map((device) => {
                    if (device.device_id === variables.device_id) {
                        return getUpdatedDevice(device, variables.name ?? device.properties.name);
                    }
                    return device;
                });
            });
        },
    });
}
const getUpdatedDevice = (device, name) => {
    const { properties } = device;
    return {
        ...device,
        properties: {
            ...properties,
            name,
        },
    };
};
//# sourceMappingURL=use-update-device-name.js.map