import React, { useEffect } from "react";
import { useDocumentVersion } from "../../hooks/useDocument";
import { useDocumentContext } from "./Documents";
import { error } from "pdf-lib";

const DocumentViewerModal = () => {
  const { viewDoc,setOpenDocument } = useDocumentContext();
  const { data, isLoading, isError,error } = useDocumentVersion(viewDoc.document);

  useEffect(() => {
    if (data?.data) {
      const fileUrl = data.data; 
      window.open(fileUrl, "_blank");
      setOpenDocument(false)
    }
  }, [data]);

  if (isLoading) return <p>Loading document...</p>;
  if (isError) return <div>
    <p className="danger-text">{error.message}</p>
  </div>;

  // Nothing to render inside modal since we redirect
  return null;
};

export default DocumentViewerModal;
