import { useGlobal } from "@/context/global";
import Confirm from "../confirm";

export const useConfirm = () => {
  const { openModal, closeModal } = useGlobal();

  const confirm = (options: ConfirmOptions = {}): Promise<boolean> => {
    return new Promise((resolve) => {
      const id = `confirm-${Date.now()}`;

      const handleConfirm = () => {
        closeModal(id);
        resolve(true);
      };

      const handleCancel = () => {
        closeModal(id);
        resolve(false);
      };

      const handleClose = () => {
        resolve(false);
      };

      const content = (
        <Confirm
          handleConfirm={handleConfirm}
          handleCancel={handleCancel}
          {...options}
        />
      );

      openModal({ id, content, onClose: handleClose });
    });
  };

  return { confirm };
};
