// packages/block-editor/src/components/image-editor/cropper.js
import Cropper from "react-easy-crop";
import clsx from "clsx";
import { Spinner } from "@wordpress/components";
import { useResizeObserver } from "@wordpress/compose";
import { MIN_ZOOM, MAX_ZOOM } from "./constants";
import { useImageEditingContext } from "./context";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function ImageCropper({
  url,
  width,
  height,
  naturalHeight,
  naturalWidth,
  borderProps
}) {
  const {
    isInProgress,
    editedUrl,
    position,
    zoom,
    aspect,
    setPosition,
    setCrop,
    setZoom,
    rotation
  } = useImageEditingContext();
  const [contentResizeListener, { width: clientWidth }] = useResizeObserver();
  let editedHeight = height || clientWidth * naturalHeight / naturalWidth;
  if (rotation % 180 === 90) {
    editedHeight = clientWidth * naturalWidth / naturalHeight;
  }
  const area = /* @__PURE__ */ jsxs(
    "div",
    {
      className: clsx(
        "wp-block-image__crop-area",
        borderProps?.className,
        {
          "is-applying": isInProgress
        }
      ),
      style: {
        ...borderProps?.style,
        width: width || clientWidth,
        height: editedHeight
      },
      children: [
        /* @__PURE__ */ jsx(
          Cropper,
          {
            image: editedUrl || url,
            disabled: isInProgress,
            minZoom: MIN_ZOOM / 100,
            maxZoom: MAX_ZOOM / 100,
            crop: position,
            zoom: zoom / 100,
            aspect,
            onCropChange: (pos) => {
              setPosition(pos);
            },
            onCropComplete: (newCropPercent) => {
              setCrop(newCropPercent);
            },
            onZoomChange: (newZoom) => {
              setZoom(newZoom * 100);
            }
          }
        ),
        isInProgress && /* @__PURE__ */ jsx(Spinner, {})
      ]
    }
  );
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    contentResizeListener,
    area
  ] });
}
export {
  ImageCropper as default
};
//# sourceMappingURL=cropper.js.map
