/* wp:polyfill */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MaybeEditor = MaybeEditor;
var _coreData = require("@wordpress/core-data");
var _data = require("@wordpress/data");
var _i18n = require("@wordpress/i18n");
var _editor = _interopRequireDefault(require("../editor"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function MaybeEditor({
  showEditor = true
}) {
  const {
    isBlockBasedTheme,
    siteUrl
  } = (0, _data.useSelect)(select => {
    const {
      getEntityRecord,
      getCurrentTheme
    } = select(_coreData.store);
    const siteData = getEntityRecord('root', '__unstableBase');
    return {
      isBlockBasedTheme: getCurrentTheme()?.is_block_theme,
      siteUrl: siteData?.home
    };
  }, []);

  // If theme is block based, return the Editor, otherwise return the site preview.
  return isBlockBasedTheme || showEditor ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.default, {}) : /*#__PURE__*/(0, _jsxRuntime.jsx)("iframe", {
    src: siteUrl,
    title: (0, _i18n.__)('Site Preview'),
    style: {
      display: 'block',
      width: '100%',
      height: '100%',
      backgroundColor: '#fff'
    },
    onLoad: event => {
      // Hide the admin bar in the front-end preview.
      const document = event.target.contentDocument;
      document.getElementById('wpadminbar').remove();
      document.getElementsByTagName('html')[0].setAttribute('style', 'margin-top: 0 !important;');
      // Make interactive elements unclickable.
      const interactiveElements = document.querySelectorAll('a, button, input, details, audio');
      interactiveElements.forEach(element => {
        element.style.pointerEvents = 'none';
        element.tabIndex = -1;
        element.setAttribute('aria-hidden', 'true');
      });
    }
  });
}
//# sourceMappingURL=index.js.map