import { UseQueryOptions, useQuery } from 'react-query';
import api from '../../services/api';
import { HeatPumpState } from '../../models/heatpump';
import { Toast } from 'native-base';
import { AlertType, createToastProps } from '../../components/ui';

const key = ['heatpump-state'];
const getKeyWithId = <T>(id?: T) => (id ? [...key, id] : key);

/**
 * Get actual heat pump state from server
 */
const useGetHeatPumpState = (options?: UseQueryOptions<HeatPumpState, Response>) =>
  useQuery<HeatPumpState, Response>(getKeyWithId(), api.HeatPumpApi.getState, {
    refetchInterval: 10 * 1000,
    staleTime: 5 * 1000,
    ...options,
    onError: response => {
      Toast.show(createToastProps(AlertType.Error, 'Could not get heat pump state'));
      options?.onError && options.onError(response);
    },
  });

useGetHeatPumpState.key = key;
useGetHeatPumpState.getKeyWithId = getKeyWithId;
export default useGetHeatPumpState;
