"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/dimensions-tool/index.js
var dimensions_tool_exports = {};
__export(dimensions_tool_exports, {
  default: () => dimensions_tool_default
});
module.exports = __toCommonJS(dimensions_tool_exports);
var import_element = require("@wordpress/element");
var import_aspect_ratio_tool = __toESM(require("./aspect-ratio-tool.cjs"));
var import_scale_tool = __toESM(require("./scale-tool.cjs"));
var import_width_height_tool = __toESM(require("./width-height-tool.cjs"));
var import_jsx_runtime = require("react/jsx-runtime");
function DimensionsTool({
  panelId,
  value = {},
  onChange = () => {
  },
  aspectRatioOptions,
  // Default options handled by AspectRatioTool.
  defaultAspectRatio = "auto",
  // Match CSS default value for aspect-ratio.
  scaleOptions,
  // Default options handled by ScaleTool.
  defaultScale = "cover",
  unitsOptions,
  // Default options handled by UnitControl.
  tools = ["aspectRatio", "widthHeight", "scale"]
}) {
  const width = value.width === void 0 || value.width === "auto" ? null : value.width;
  const height = value.height === void 0 || value.height === "auto" ? null : value.height;
  const aspectRatio = value.aspectRatio === void 0 || value.aspectRatio === "auto" ? null : value.aspectRatio;
  const scale = value.scale === void 0 ? null : value.scale;
  const [lastScale, setLastScale] = (0, import_element.useState)(scale);
  const [lastAspectRatio, setLastAspectRatio] = (0, import_element.useState)(aspectRatio);
  const hasCustomAspectRatio = !!(width && height);
  const aspectRatioValue = hasCustomAspectRatio ? "custom" : aspectRatio;
  const showScaleControl = aspectRatio || width && height;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    tools.includes("aspectRatio") && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_aspect_ratio_tool.default,
      {
        panelId,
        options: aspectRatioOptions,
        defaultValue: defaultAspectRatio,
        value: aspectRatioValue,
        onChange: (nextAspectRatio) => {
          const nextValue = { ...value };
          nextAspectRatio = nextAspectRatio === "auto" ? null : nextAspectRatio;
          setLastAspectRatio(nextAspectRatio);
          if (!nextAspectRatio) {
            delete nextValue.aspectRatio;
          } else {
            nextValue.aspectRatio = nextAspectRatio;
          }
          if (!nextAspectRatio) {
            delete nextValue.scale;
          } else if (lastScale) {
            nextValue.scale = lastScale;
          } else {
            nextValue.scale = defaultScale;
            setLastScale(defaultScale);
          }
          if ("custom" !== nextAspectRatio && width && height) {
            delete nextValue.height;
          }
          onChange(nextValue);
        }
      }
    ),
    tools.includes("widthHeight") && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_width_height_tool.default,
      {
        panelId,
        units: unitsOptions,
        value: { width, height },
        onChange: ({ width: nextWidth, height: nextHeight }) => {
          const nextValue = { ...value };
          nextWidth = nextWidth === "auto" ? null : nextWidth;
          nextHeight = nextHeight === "auto" ? null : nextHeight;
          if (!nextWidth) {
            delete nextValue.width;
          } else {
            nextValue.width = nextWidth;
          }
          if (!nextHeight) {
            delete nextValue.height;
          } else {
            nextValue.height = nextHeight;
          }
          if (nextWidth && nextHeight) {
            delete nextValue.aspectRatio;
          } else if (lastAspectRatio) {
            nextValue.aspectRatio = lastAspectRatio;
          } else {
          }
          if (!lastAspectRatio && !!nextWidth !== !!nextHeight) {
            delete nextValue.scale;
          } else if (lastScale) {
            nextValue.scale = lastScale;
          } else {
            nextValue.scale = defaultScale;
            setLastScale(defaultScale);
          }
          onChange(nextValue);
        }
      }
    ),
    tools.includes("scale") && showScaleControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_scale_tool.default,
      {
        panelId,
        options: scaleOptions,
        defaultValue: defaultScale,
        value: lastScale,
        onChange: (nextScale) => {
          const nextValue = { ...value };
          setLastScale(nextScale);
          if (!nextScale) {
            delete nextValue.scale;
          } else {
            nextValue.scale = nextScale;
          }
          onChange(nextValue);
        }
      }
    )
  ] });
}
var dimensions_tool_default = DimensionsTool;
//# sourceMappingURL=index.cjs.map
