import { UseMutationOptions, useMutation } from 'react-query';
import api from '../../services/api';
import { RelayPostRequest } from '../../models/evok';
import { Toast } from 'native-base';
import { AlertType, createToastProps } from '../../components/ui';

const useSetRelay = (
  options?: Omit<UseMutationOptions<RelayPostRequest, Response, RelayPostRequest>, 'mutationFn'>,
) => {
  return useMutation<RelayPostRequest, Response, RelayPostRequest>(
    data => api.EvokApi.setRelay(data),
    {
      ...options,
      onError: (error, variables, ctx) => {
        Toast.show(
          createToastProps(AlertType.Error, `Relay ${variables.circuit} state change failed`),
        );
        options?.onError && options.onError(error, variables, ctx);
      },
      onSuccess: (result, requestData, ctx) => {
        Toast.show(
          createToastProps(AlertType.Success, `Relay ${requestData.circuit} state changed`),
        );
        options?.onSuccess && options.onSuccess(result, requestData, ctx);
      },
    },
  );
};

export default useSetRelay;
