import { MailFilled, PrinterFilled } from "@ant-design/icons";
import { Button, Space, Spin } from "antd";
import { useState } from "react";
import { GenerateDocument } from "../../utils/RenderTemplate";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";

export default function PrintWrapperComponent({
  templateObject,
  messageObject = {},
  children,
  id,
  emailOnly = false,
  disabled
}) {
  const [loading, setLoading] = useState(false);
  const notification = useNotification();

  const handlePrint = async (type) => {
    if (disabled) return;
    setLoading(true);
    await GenerateDocument(templateObject, messageObject, type, id, notification);
    setLoading(false);
  };

  return (
    <Space>
      {children || null}
      {!emailOnly && (
        <Button
          style={{ cursor: disabled ? "not-allowed" : null }}
          icon={<PrinterFilled />}
          disabled={disabled}
          onClick={() => handlePrint("p")}
        />
      )}
      <Button
        style={{ cursor: disabled ? "not-allowed" : null }}
        icon={<MailFilled />}
        disabled={disabled}
        onClick={() => handlePrint("e")}
      />
      {loading && <Spin />}
    </Space>
  );
}
