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