// packages/block-editor/src/components/image-size-control/use-dimension-handler.js
import { useEffect, useState } from "@wordpress/element";
function useDimensionHandler(customHeight, customWidth, defaultHeight, defaultWidth, onChange) {
  const [currentWidth, setCurrentWidth] = useState(
    customWidth ?? defaultWidth ?? ""
  );
  const [currentHeight, setCurrentHeight] = useState(
    customHeight ?? defaultHeight ?? ""
  );
  useEffect(() => {
    if (customWidth === void 0 && defaultWidth !== void 0) {
      setCurrentWidth(defaultWidth);
    }
    if (customHeight === void 0 && defaultHeight !== void 0) {
      setCurrentHeight(defaultHeight);
    }
  }, [defaultWidth, defaultHeight]);
  useEffect(() => {
    if (customWidth !== void 0 && Number.parseInt(customWidth) !== Number.parseInt(currentWidth)) {
      setCurrentWidth(customWidth);
    }
    if (customHeight !== void 0 && Number.parseInt(customHeight) !== Number.parseInt(currentHeight)) {
      setCurrentHeight(customHeight);
    }
  }, [customWidth, customHeight]);
  const updateDimension = (dimension, value) => {
    const parsedValue = value === "" ? void 0 : parseInt(value, 10);
    if (dimension === "width") {
      setCurrentWidth(parsedValue);
    } else {
      setCurrentHeight(parsedValue);
    }
    onChange({
      [dimension]: parsedValue
    });
  };
  const updateDimensions = (nextHeight, nextWidth) => {
    setCurrentHeight(nextHeight ?? defaultHeight);
    setCurrentWidth(nextWidth ?? defaultWidth);
    onChange({ height: nextHeight, width: nextWidth });
  };
  return {
    currentHeight,
    currentWidth,
    updateDimension,
    updateDimensions
  };
}
export {
  useDimensionHandler as default
};
//# sourceMappingURL=use-dimension-handler.js.map
