import { useMutation, useQueryClient, } from '@tanstack/react-query';
import { fahrenheitToCelsius } from '../../../lib/seam/thermostats/unit-conversion.js';
import { NullSeamClientError, useSeamClient } from '../../../lib/seam/use-seam-client.js';
export function useUpdateThermostatClimatePreset() {
    const { client } = useSeamClient();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (variables) => {
            if (client === null)
                throw new NullSeamClientError();
            await client.thermostats.updateClimatePreset(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;
                });
            });
        },
    });
}
function getUpdatedDevice(device, variables) {
    const preset = {
        ...variables,
        cooling_set_point_celsius: fahrenheitToCelsius(variables.cooling_set_point_fahrenheit),
        heating_set_point_celsius: fahrenheitToCelsius(variables.heating_set_point_fahrenheit),
        display_name: variables.name ?? variables.climate_preset_key,
        can_delete: true,
        can_edit: true,
        can_program: true,
        manual_override_allowed: true,
    };
    return {
        ...device,
        properties: {
            ...device.properties,
            available_climate_presets: [
                preset,
                ...(device.properties.available_climate_presets ?? []),
            ],
        },
    };
}
//# sourceMappingURL=use-update-thermostat-climate-preset.js.map