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