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