import { useMutation, useQueryClient, } from '@tanstack/react-query';
import { NullSeamClientError, useSeamClient } from '../../../lib/seam/use-seam-client.js';
export function useCoolThermostat() {
    const { client } = useSeamClient();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (mutationParams) => {
            if (client === null)
                throw new NullSeamClientError();
            return await client.thermostats.cool(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,
            current_climate_setting: {
                ...thermostat.properties.current_climate_setting,
                cooling_set_point_fahrenheit: variables.cooling_set_point_fahrenheit ??
                    thermostat.properties.current_climate_setting
                        .cooling_set_point_fahrenheit,
                cooling_set_point_celsius: variables.cooling_set_point_celsius ??
                    thermostat.properties.current_climate_setting
                        .cooling_set_point_celsius,
            },
        },
    };
}
//# sourceMappingURL=use-cool-thermostat.js.map