import { useMutation, useQueryClient, } from '@tanstack/react-query';
import { getCoolingSetPointCelsius, getCoolingSetPointFahrenheit, } from '../../../lib/seam/thermostats/unit-conversion.js';
import { NullSeamClientError, useSeamClient } from '../../../lib/seam/use-seam-client.js';
export function useCoolThermostat() {
    const { client } = useSeamClient();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (variables) => {
            if (client === null)
                throw new NullSeamClientError();
            await client.thermostats.cool(variables);
        },
        onSuccess: (_data, variables) => {
            queryClient.setQueryData(['devices', 'get', { device_id: variables.device_id }], (device) => {
                if (device == null) {
                    return;
                }
                return getUpdatedDevice(device, variables);
            });
            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);
                    }
                    return device;
                });
            });
        },
    });
}
const getUpdatedDevice = (device, variables) => {
    const { properties } = device;
    if ('current_climate_setting' in properties &&
        properties.current_climate_setting != null) {
        return {
            ...device,
            properties: {
                ...properties,
                current_climate_setting: {
                    ...properties.current_climate_setting,
                    hvac_mode_setting: 'cool',
                    heating_set_point_celsius: undefined,
                    heating_set_point_fahrenheit: undefined,
                    cooling_set_point_celsius: getCoolingSetPointCelsius(variables, device),
                    cooling_set_point_fahrenheit: getCoolingSetPointFahrenheit(variables, device),
                },
            },
        };
    }
    return device;
};
//# sourceMappingURL=use-cool-thermostat.js.map