"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/block-preview/auto.js
var auto_exports = {};
__export(auto_exports, {
  default: () => AutoBlockPreview
});
module.exports = __toCommonJS(auto_exports);
var import_compose = require("@wordpress/compose");
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_components = require("@wordpress/components");
var import_block_list = __toESM(require("../block-list"));
var import_iframe = __toESM(require("../iframe"));
var import_editor_styles = __toESM(require("../editor-styles"));
var import_store = require("../../store");
var import_jsx_runtime = require("react/jsx-runtime");
var MemoizedBlockList = (0, import_element.memo)(import_block_list.default);
var MAX_HEIGHT = 2e3;
var EMPTY_ADDITIONAL_STYLES = [];
function ScaledBlockPreview({
  viewportWidth,
  containerWidth,
  minHeight,
  additionalStyles = EMPTY_ADDITIONAL_STYLES
}) {
  if (!viewportWidth) {
    viewportWidth = containerWidth;
  }
  const [contentResizeListener, { height: contentHeight }] = (0, import_compose.useResizeObserver)();
  const { styles } = (0, import_data.useSelect)((select) => {
    const settings = select(import_store.store).getSettings();
    return {
      styles: settings.styles
    };
  }, []);
  const editorStyles = (0, import_element.useMemo)(() => {
    if (styles) {
      return [
        ...styles,
        {
          css: "body{height:auto;overflow:hidden;border:none;padding:0;}",
          __unstableType: "presets"
        },
        ...additionalStyles
      ];
    }
    return styles;
  }, [styles, additionalStyles]);
  const scale = containerWidth / viewportWidth;
  const aspectRatio = contentHeight ? containerWidth / (contentHeight * scale) : 0;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.Disabled,
    {
      className: "block-editor-block-preview__content",
      style: {
        transform: `scale(${scale})`,
        // Using width + aspect-ratio instead of height here triggers browsers' native
        // handling of scrollbar's visibility. It prevents the flickering issue seen
        // in https://github.com/WordPress/gutenberg/issues/52027.
        // See https://github.com/WordPress/gutenberg/pull/52921 for more info.
        aspectRatio,
        maxHeight: contentHeight > MAX_HEIGHT ? MAX_HEIGHT * scale : void 0,
        minHeight
      },
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
        import_iframe.default,
        {
          contentRef: (0, import_compose.useRefEffect)((bodyElement) => {
            const {
              ownerDocument: { documentElement }
            } = bodyElement;
            documentElement.classList.add(
              "block-editor-block-preview__content-iframe"
            );
            documentElement.style.position = "absolute";
            documentElement.style.width = "100%";
            bodyElement.style.boxSizing = "border-box";
            bodyElement.style.position = "absolute";
            bodyElement.style.width = "100%";
          }, []),
          "aria-hidden": true,
          tabIndex: -1,
          style: {
            position: "absolute",
            width: viewportWidth,
            height: contentHeight,
            pointerEvents: "none",
            // This is a catch-all max-height for patterns.
            // See: https://github.com/WordPress/gutenberg/pull/38175.
            maxHeight: MAX_HEIGHT,
            minHeight: scale !== 0 && scale < 1 && minHeight ? minHeight / scale : minHeight
          },
          children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_editor_styles.default, { styles: editorStyles }),
            contentResizeListener,
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MemoizedBlockList, { renderAppender: false })
          ]
        }
      )
    }
  );
}
function AutoBlockPreview(props) {
  const [containerResizeListener, { width: containerWidth }] = (0, import_compose.useResizeObserver)();
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { position: "relative", width: "100%", height: 0 }, children: containerResizeListener }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor-block-preview__container", children: !!containerWidth && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      ScaledBlockPreview,
      {
        ...props,
        containerWidth
      }
    ) })
  ] });
}
//# sourceMappingURL=auto.js.map
