import { useMutation, useQueryClient, } from '@tanstack/react-query';
import { NullSeamClientError, useSeamClient } from '../../../lib/seam/use-seam-client.js';
export function useSetThermostatFanMode() {
    const { client } = useSeamClient();
    const queryClient = useQueryClient();
    return useMutation({
        mutationFn: async (variables) => {
            if (client === null)
                throw new NullSeamClientError();
            await client.thermostats.setFanMode(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 ('fan_mode_setting' in properties && properties.fan_mode_setting != null) {
        return {
            ...device,
            properties: {
                ...properties,
                fan_mode_setting: variables.fan_mode_setting,
            },
        };
    }
    return device;
};
//# sourceMappingURL=use-set-thermostat-fan-mode.js.map