import { useCallback } from "react";
import { useAppDispatch } from "../store/hooks";
import { addNotification, removeNotification } from "../store/slices/uiSlice";

export const useNotification = () => {
  const dispatch = useAppDispatch();

  const showNotification = useCallback(
    (
      message: string,
      type: "info" | "success" | "error" | "warning" = "info"
    ) => {
      const id = `notification-${Date.now()}-${Math.random()
        .toString(36)
        .substr(2, 9)}`;
      dispatch(addNotification({ message, type, id }));
      setTimeout(() => {
        dispatch(removeNotification(id));
      }, 4000);
    },
    [dispatch]
  );

  return { showNotification };
};
