import { useEffect } from "react";
import { useToast } from "@chakra-ui/react";
import { useTranslation } from "react-i18next";

import { isErrorMessage } from "../../../models/api";

export const useHandleUploadCarImageResponse = (query: {
  isSuccess: boolean;
  isError: boolean;
  error?: unknown;
}) => {
  const toast = useToast();
  const { t } = useTranslation('~', {
    keyPrefix: 'dry-wash.order-view.upload-car-image-query',
  });

  useEffect(() => {
    if (query.isError) {
      toast({
        status: 'error',
        title: t('error.title'),
        description: isErrorMessage(query.error) ? query.error : undefined,
      });
    }
  }, [query.isError]);

  useEffect(() => {
    if (query.isSuccess) {
      toast({
        status: 'success',
        title: t('success.title'),
      });
    }
  }, [query.isSuccess]);
};