// packages/editor/src/components/media/media-editor-modal.js
import { useSettings } from "@wordpress/block-editor";
import { useMemo } from "@wordpress/element";
import { privateApis as mediaEditorPrivateApis } from "@wordpress/media-editor";
import { unlock } from "../../lock-unlock.mjs";
import usePostFields from "../post-fields/index.mjs";
import { jsx } from "react/jsx-runtime";
var { MediaEditorModal } = unlock(mediaEditorPrivateApis);
function ratioToNumber(ratio) {
  if (ratio === void 0 || ratio === null) {
    return NaN;
  }
  const [a, b, ...rest] = String(ratio).split("/").map(Number);
  if (a <= 0 || b <= 0 || Number.isNaN(a) || Number.isNaN(b) || rest.length) {
    return NaN;
  }
  return b ? a / b : a;
}
function aspectRatioPresetFromSettings({ name, ratio } = {}) {
  const value = ratioToNumber(ratio);
  if (!name || !Number.isFinite(value) || value <= 0) {
    return null;
  }
  return {
    label: name,
    value
  };
}
function MediaEditorModalMount() {
  const fields = usePostFields({ postType: "attachment" });
  const [defaultRatios, themeRatios, showDefaultRatios] = useSettings(
    "dimensions.aspectRatios.default",
    "dimensions.aspectRatios.theme",
    "dimensions.defaultAspectRatios"
  );
  const aspectRatioPresets = useMemo(() => {
    const hasAspectRatioSettings = Array.isArray(defaultRatios) || Array.isArray(themeRatios) || typeof showDefaultRatios === "boolean";
    if (!hasAspectRatioSettings) {
      return void 0;
    }
    const candidateRatios = [
      ...showDefaultRatios && Array.isArray(defaultRatios) ? defaultRatios : [],
      ...Array.isArray(themeRatios) ? themeRatios : []
    ];
    const presets = candidateRatios.map(aspectRatioPresetFromSettings).filter(Boolean);
    if (presets.length || showDefaultRatios === false) {
      return presets;
    }
    return void 0;
  }, [defaultRatios, themeRatios, showDefaultRatios]);
  return /* @__PURE__ */ jsx(
    MediaEditorModal,
    {
      fields,
      aspectRatioPresets
    }
  );
}
export {
  MediaEditorModalMount as default
};
//# sourceMappingURL=media-editor-modal.mjs.map
