import { jsx } from "react/jsx-runtime";
import { useEntity, useMutationState, EntityAccessor } from "@contember/binding";
import { Button, toThemeClass, Icon } from "@contember/ui";
import { listClassName } from "@contember/utilities";
import { memo, useCallback } from "react";
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__ */ jsx(
    Button,
    {
      distinction: "primary",
      ...defaultProps,
      ...rest,
      className: listClassName([
        className,
        toThemeClass(null, "danger", ":hover")
      ]),
      disabled: isMutating || rest.disabled,
      onClick,
      children: children || /* @__PURE__ */ jsx(Icon, { blueprintIcon: "trash" })
    }
  );
});
DeleteEntityButton.displayName = "DeleteEntityButton";
export {
  DeleteEntityButton
};
//# sourceMappingURL=DeleteEntityButton.js.map
