import Swal from "sweetalert2";
import withReactContent from "sweetalert2-react-content";
import Button from "../Forms/Button";

export const handleSweetAlert = async (
  options: any,
  resultConfirmed?: () => void,
): Promise<any> => {
  return withReactContent(Swal)
    .fire({
      showDenyButton: true,
      confirmButtonText: "Confirmar",
      confirmButtonColor: "#4CAF50",
      denyButtonText: "Cancelar",
      denyButtonColor: "#FF0000",
      customClass: {
        popup: "dark:bg-form-strokedark dark:text-white",
      },
      ...options,
    })
    .then((result) => {
      if (result.isConfirmed && resultConfirmed) {
        resultConfirmed();
      }
      return result;
    });
};

const SweetAlertConfirmacao = ({
  resultConfirmed,
  icon,
  tittle,
  html,
  className,
  children,
  loading = false,
}: {
  resultConfirmed?: () => void;
  icon?: string;
  tittle?: string;
  html?: string;
  className?: string;
  children?: React.ReactNode;
  loading?: boolean;
}) => {
  return (
    <Button
      loading={loading}
      className={className}
      type="button"
      onClick={() =>
        handleSweetAlert({ icon, title: tittle, html } as any, resultConfirmed)
      }
    >
      {children}
    </Button>
  );
};

export default SweetAlertConfirmacao;
