import { useCallback } from 'react';

import { UseToastOptions, useToast } from '@chakra-ui/react';

import { RestErrorResponse } from '@/back-api';

export const toastDefaultConfig: UseToastOptions = {
  duration: 3000,
  isClosable: true,
  position: 'top-right',
  variant: 'solid',
};

export const useToastError = () =>
  useToast({
    ...toastDefaultConfig,
    status: 'error',
    duration: 5000,
  });

export const useToastWarning = () =>
  useToast({
    ...toastDefaultConfig,
    status: 'warning',
  });

export const useToastSuccess = () =>
  useToast({
    ...toastDefaultConfig,
    status: 'success',
  });

export const useToastInfo = () =>
  useToast({
    ...toastDefaultConfig,
    status: 'info',
  });

export const useToastAPIError = () => {
  const toastError = useToastError();
  return useCallback(
    (error: RestErrorResponse) => {
      toastError({
        title: `[${error.status}] ${error.statusMessage}`,
        description: error.message,
      });
    },
    [toastError]
  );
};
