// packages/block-editor/src/components/image-size-control/index.js
import {
  SelectControl,
  __experimentalNumberControl as NumberControl,
  __experimentalHStack as HStack,
  __experimentalVStack as VStack,
  __experimentalToggleGroupControl as ToggleGroupControl,
  __experimentalToggleGroupControlOption as ToggleGroupControlOption
} from "@wordpress/components";
import { __, sprintf } from "@wordpress/i18n";
import useDimensionHandler from "./use-dimension-handler";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
var IMAGE_SIZE_PRESETS = [25, 50, 75, 100];
var noop = () => {
};
function getScaledWidthAndHeight(scale, imageWidth, imageHeight) {
  const scaledWidth = Math.round(imageWidth * (scale / 100));
  const scaledHeight = Math.round(imageHeight * (scale / 100));
  return {
    scaledWidth,
    scaledHeight
  };
}
function ImageSizeControl({
  imageSizeHelp,
  imageWidth,
  imageHeight,
  imageSizeOptions = [],
  isResizable = true,
  slug,
  width,
  height,
  onChange,
  onChangeImage = noop
}) {
  const { currentHeight, currentWidth, updateDimension, updateDimensions } = useDimensionHandler(height, width, imageHeight, imageWidth, onChange);
  const handleUpdateDimensions = (scale) => {
    if (void 0 === scale) {
      updateDimensions();
      return;
    }
    const { scaledWidth, scaledHeight } = getScaledWidthAndHeight(
      scale,
      imageWidth,
      imageHeight
    );
    updateDimensions(scaledHeight, scaledWidth);
  };
  const selectedValue = IMAGE_SIZE_PRESETS.find((scale) => {
    const { scaledWidth, scaledHeight } = getScaledWidthAndHeight(
      scale,
      imageWidth,
      imageHeight
    );
    return currentWidth === scaledWidth && currentHeight === scaledHeight;
  });
  return /* @__PURE__ */ jsxs(VStack, { className: "block-editor-image-size-control", spacing: "4", children: [
    imageSizeOptions && imageSizeOptions.length > 0 && /* @__PURE__ */ jsx(
      SelectControl,
      {
        __nextHasNoMarginBottom: true,
        label: __("Resolution"),
        value: slug,
        options: imageSizeOptions,
        onChange: onChangeImage,
        help: imageSizeHelp,
        size: "__unstable-large"
      }
    ),
    isResizable && /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsxs(HStack, { align: "baseline", spacing: "4", children: [
        /* @__PURE__ */ jsx(
          NumberControl,
          {
            label: __("Width"),
            value: currentWidth,
            min: 1,
            onChange: (value) => updateDimension("width", value),
            size: "__unstable-large"
          }
        ),
        /* @__PURE__ */ jsx(
          NumberControl,
          {
            label: __("Height"),
            value: currentHeight,
            min: 1,
            onChange: (value) => updateDimension("height", value),
            size: "__unstable-large"
          }
        )
      ] }),
      /* @__PURE__ */ jsx(
        ToggleGroupControl,
        {
          label: __("Image size presets"),
          hideLabelFromVision: true,
          onChange: handleUpdateDimensions,
          value: selectedValue,
          isBlock: true,
          __next40pxDefaultSize: true,
          __nextHasNoMarginBottom: true,
          children: IMAGE_SIZE_PRESETS.map((scale) => {
            return /* @__PURE__ */ jsx(
              ToggleGroupControlOption,
              {
                value: scale,
                label: sprintf(
                  /* translators: %d: Percentage value. */
                  __("%d%%"),
                  scale
                )
              },
              scale
            );
          })
        }
      )
    ] })
  ] });
}
export {
  ImageSizeControl as default
};
//# sourceMappingURL=index.js.map
