import { useTranslation } from "react-i18next";
import { DIALOG_NAMESPACE } from "@/i18n/resources";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";

type ConfirmDeleteDialogProps = {
  open?: boolean;
  children?: React.ReactNode;
  description: string;
  isDeleting?: boolean;
  onOpen?: () => void;
  onConfirm?: () => void;
  onCancel?: () => void;
};

export function ConfirmDeleteDialog({
  children,
  description,
  open,
  isDeleting = false,
  onOpen,
  onConfirm,
  onCancel,
}: ConfirmDeleteDialogProps) {
  const { t } = useTranslation(DIALOG_NAMESPACE);

  const handleOpenChange = (open_: boolean) => {
    if (open_) {
      onOpen?.();
    } else if (!isDeleting) {
      onCancel?.();
    }
  };
  return (
    <AlertDialog open={open} onOpenChange={handleOpenChange}>
      {children && <AlertDialogTrigger asChild>{children}</AlertDialogTrigger>}
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>{t("confirm_delete.title")}</AlertDialogTitle>
          <AlertDialogDescription>{description}</AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel onClick={onCancel} disabled={isDeleting}>
            {t("confirm_delete.cancel")}
          </AlertDialogCancel>
          <AlertDialogAction onClick={onConfirm} disabled={isDeleting}>
            {isDeleting ? t("confirm_delete.deleting") : t("confirm_delete.confirm")}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}
