import type { JSX } from "solid-js";
import { splitProps } from "solid-js";
import { useInternalAlertDialogContext } from "./alert-dialog-context";

/** Props for AlertDialog.Cancel. */
export interface AlertDialogCancelProps extends JSX.ButtonHTMLAttributes<HTMLButtonElement> {
  children?: JSX.Element;
}

/** Cancels the action and closes the AlertDialog. */
export function AlertDialogCancel(props: AlertDialogCancelProps): JSX.Element {
  const [local, rest] = splitProps(props, ["children", "onClick"]);
  const ctx = useInternalAlertDialogContext();

  const handleClick: JSX.EventHandler<HTMLButtonElement, MouseEvent> = (e) => {
    if (typeof local.onClick === "function") local.onClick(e);
    ctx.setOpen(false);
  };

  return (
    <button type="button" onClick={handleClick} {...rest}>
      {local.children}
    </button>
  );
}
