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

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

// Currently not important, but left here for reference
const useGetEvokState = (options?: UseQueryOptions<EvokState, Response>) =>
  useQuery<EvokState, Response>(getKeyWithId(), api.EvokApi.getState, {
    refetchInterval: 10 * 1000,
    staleTime: 5 * 1000,
    ...options,
    onError: response => {
      Toast.show(createToastProps(AlertType.Error, 'Could not get evok state'));
      options?.onError && options.onError(response);
    },
  });

useGetEvokState.key = key;
useGetEvokState.getKeyWithId = getKeyWithId;
export default useGetEvokState;
