import { memo, useCallback, createElement } from "react";
import { useEntity, useMutationState, EntityAccessor } from "@contember/binding";
import { Button, toThemeClass, Icon } from "@contember/ui";
import cn from "classnames";
import { usePersistWithFeedback } from "../../../ui/usePersistWithFeedback.js";
const DeleteEntityButton = memo((props) => {
  const { children, immediatePersist, className, ...rest } = props;
  const parentEntity = useEntity();
  const triggerPersist = usePersistWithFeedback();
  const isMutating = useMutationState();
  const onClick = useCallback(() => {
    if (props.immediatePersist && !confirm("Really?")) {
      return;
    }
    parentEntity.deleteEntity();
    if (props.immediatePersist && triggerPersist) {
      triggerPersist().catch(() => {
      });
    }
  }, [triggerPersist, props.immediatePersist, parentEntity]);
  if (!(parentEntity instanceof EntityAccessor)) {
    return null;
  }
  let defaultProps = {
    size: "small",
    flow: "circular",
    distinction: "seamless",
    bland: true
  };
  return /* @__PURE__ */ createElement(Button, {
    distinction: "primary",
    ...defaultProps,
    ...rest,
    className: cn(className, toThemeClass(null, "danger", ":hover")),
    disabled: isMutating || rest.disabled,
    onClick
  }, children || /* @__PURE__ */ createElement(Icon, {
    blueprintIcon: "trash"
  }));
});
DeleteEntityButton.displayName = "DeleteEntityButton";
export { DeleteEntityButton };
//# sourceMappingURL=DeleteEntityButton.js.map
