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

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

/**
 * Get heat pump configuration from server
 */
const useGetHeatPumpConfig = (options?: UseQueryOptions<HeatPumpConfig, Response>) =>
  useQuery<HeatPumpConfig, Response>(getKeyWithId(), api.HeatPumpApi.getConfig, {
    refetchInterval: 1.5 * 60 * 1000,
    staleTime: 60 * 1000,
    ...options,
    onError: response => {
      Toast.show(createToastProps(AlertType.Error, 'Could not get heat pump configuration'));
      options?.onError && options.onError(response);
    },
  });

useGetHeatPumpConfig.key = key;
useGetHeatPumpConfig.getKeyWithId = getKeyWithId;
export default useGetHeatPumpConfig;
