import type { JSX } from "solid-js";
import { splitProps } from "solid-js";
import { useInternalDrawerContext } from "./drawer-context";

/** Props for Drawer.Close. */
export interface DrawerCloseProps extends JSX.ButtonHTMLAttributes<HTMLButtonElement> {
  children?: JSX.Element;
}

/**
 * Closes the Drawer when clicked.
 */
export function DrawerClose(props: DrawerCloseProps): JSX.Element {
  const [local, rest] = splitProps(props, ["children", "onClick"]);
  const ctx = useInternalDrawerContext();

  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>
  );
}
