"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _blockEditor = require("@wordpress/block-editor");
var _editor = require("@wordpress/editor");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _lockUnlock = require("../../lock-unlock");
var _editorCanvasContainer = _interopRequireDefault(require("../editor-canvas-container"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  ExperimentalBlockEditorProvider,
  GlobalStylesContext,
  useGlobalStylesOutputWithConfig,
  __unstableBlockStyleVariationOverridesWithConfig
} = (0, _lockUnlock.unlock)(_blockEditor.privateApis);
const {
  mergeBaseAndUserConfigs
} = (0, _lockUnlock.unlock)(_editor.privateApis);
function isObjectEmpty(object) {
  return !object || Object.keys(object).length === 0;
}
function Revisions({
  userConfig,
  blocks
}) {
  const {
    base: baseConfig
  } = (0, _element.useContext)(GlobalStylesContext);
  const mergedConfig = (0, _element.useMemo)(() => {
    if (!isObjectEmpty(userConfig) && !isObjectEmpty(baseConfig)) {
      return mergeBaseAndUserConfigs(baseConfig, userConfig);
    }
    return {};
  }, [baseConfig, userConfig]);
  const renderedBlocksArray = (0, _element.useMemo)(() => Array.isArray(blocks) ? blocks : [blocks], [blocks]);
  const originalSettings = (0, _data.useSelect)(select => select(_blockEditor.store).getSettings(), []);
  const settings = (0, _element.useMemo)(() => ({
    ...originalSettings,
    isPreviewMode: true
  }), [originalSettings]);
  const [globalStyles] = useGlobalStylesOutputWithConfig(mergedConfig);
  const editorStyles = !isObjectEmpty(globalStyles) && !isObjectEmpty(userConfig) ? globalStyles : settings.styles;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_editorCanvasContainer.default, {
    title: (0, _i18n.__)('Revisions'),
    closeButtonLabel: (0, _i18n.__)('Close revisions'),
    enableResizing: true,
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_blockEditor.__unstableIframe, {
      className: "edit-site-revisions__iframe",
      name: "revisions",
      tabIndex: 0,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("style", {
        children:
        // Forming a "block formatting context" to prevent margin collapsing.
        // @see https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context
        `.is-root-container { display: flow-root; }`
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Disabled, {
        className: "edit-site-revisions__example-preview__content",
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(ExperimentalBlockEditorProvider, {
          value: renderedBlocksArray,
          settings: settings,
          children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.BlockList, {
            renderAppender: false
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.__unstableEditorStyles, {
            styles: editorStyles
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(__unstableBlockStyleVariationOverridesWithConfig, {
            config: mergedConfig
          })]
        })
      })]
    })
  });
}
var _default = exports.default = Revisions;
//# sourceMappingURL=index.js.map