"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = LayoutWithGlobalStylesProvider;
var _clsx = _interopRequireDefault(require("clsx"));
var _components = require("@wordpress/components");
var _compose = require("@wordpress/compose");
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _commands = require("@wordpress/commands");
var _blockEditor = require("@wordpress/block-editor");
var _editor = require("@wordpress/editor");
var _coreCommands = require("@wordpress/core-commands");
var _router = require("@wordpress/router");
var _plugins = require("@wordpress/plugins");
var _notices = require("@wordpress/notices");
var _data = require("@wordpress/data");
var _preferences = require("@wordpress/preferences");
var _siteHub = _interopRequireWildcard(require("../site-hub"));
var _resizableFrame = _interopRequireDefault(require("../resizable-frame"));
var _lockUnlock = require("../../lock-unlock");
var _saveKeyboardShortcut = _interopRequireDefault(require("../save-keyboard-shortcut"));
var _hooks = require("./hooks");
var _animation = _interopRequireDefault(require("./animation"));
var _sidebar = require("../sidebar");
var _saveHub = _interopRequireDefault(require("../save-hub"));
var _savePanel = _interopRequireDefault(require("../save-panel"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useCommands
} = (0, _lockUnlock.unlock)(_coreCommands.privateApis);
const {
  useGlobalStyle
} = (0, _lockUnlock.unlock)(_blockEditor.privateApis);
const {
  NavigableRegion,
  GlobalStylesProvider
} = (0, _lockUnlock.unlock)(_editor.privateApis);
const {
  useLocation
} = (0, _lockUnlock.unlock)(_router.privateApis);
const ANIMATION_DURATION = 0.3;
function Layout() {
  const {
    query,
    name: routeKey,
    areas,
    widths
  } = useLocation();
  const {
    canvas = 'view'
  } = query;
  useCommands();
  const isMobileViewport = (0, _compose.useViewportMatch)('medium', '<');
  const toggleRef = (0, _element.useRef)();
  const navigateRegionsProps = (0, _components.__unstableUseNavigateRegions)();
  const disableMotion = (0, _compose.useReducedMotion)();
  const [canvasResizer, canvasSize] = (0, _compose.useResizeObserver)();
  const isEditorLoading = (0, _hooks.useIsSiteEditorLoading)();
  const [isResizableFrameOversized, setIsResizableFrameOversized] = (0, _element.useState)(false);
  const animationRef = (0, _animation.default)({
    triggerAnimationOnChange: routeKey + '-' + canvas
  });
  const {
    showIconLabels
  } = (0, _data.useSelect)(select => {
    return {
      showIconLabels: select(_preferences.store).get('core', 'showIconLabels')
    };
  });
  const [backgroundColor] = useGlobalStyle('color.background');
  const [gradientValue] = useGlobalStyle('color.gradient');
  const previousCanvaMode = (0, _compose.usePrevious)(canvas);
  (0, _element.useEffect)(() => {
    if (previousCanvaMode === 'edit') {
      toggleRef.current?.focus();
    }
    // Should not depend on the previous canvas mode value but the next.
  }, [canvas]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.UnsavedChangesWarning, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_commands.CommandMenu, {}), canvas === 'view' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_saveKeyboardShortcut.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      ...navigateRegionsProps,
      ref: navigateRegionsProps.ref,
      className: (0, _clsx.default)('edit-site-layout', navigateRegionsProps.className, {
        'is-full-canvas': canvas === 'edit',
        'show-icon-labels': showIconLabels
      }),
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "edit-site-layout__content",
        children: [(!isMobileViewport || !areas.mobile) && /*#__PURE__*/(0, _jsxRuntime.jsx)(NavigableRegion, {
          ariaLabel: (0, _i18n.__)('Navigation'),
          className: "edit-site-layout__sidebar-region",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__unstableAnimatePresence, {
            children: canvas === 'view' && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__unstableMotion.div, {
              initial: {
                opacity: 0
              },
              animate: {
                opacity: 1
              },
              exit: {
                opacity: 0
              },
              transition: {
                type: 'tween',
                duration:
                // Disable transition in mobile to emulate a full page transition.
                disableMotion || isMobileViewport ? 0 : ANIMATION_DURATION,
                ease: 'easeOut'
              },
              className: "edit-site-layout__sidebar",
              children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_siteHub.default, {
                ref: toggleRef,
                isTransparent: isResizableFrameOversized
              }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebar.SidebarNavigationProvider, {
                children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebar.SidebarContent, {
                  shouldAnimate: routeKey !== 'styles',
                  routeKey: routeKey,
                  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.ErrorBoundary, {
                    children: areas.sidebar
                  })
                })
              }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_saveHub.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_savePanel.default, {})]
            })
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.EditorSnackbars, {}), isMobileViewport && areas.mobile && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "edit-site-layout__mobile",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebar.SidebarNavigationProvider, {
            children: canvas !== 'edit' ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
              children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_siteHub.SiteHubMobile, {
                ref: toggleRef,
                isTransparent: isResizableFrameOversized
              }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_sidebar.SidebarContent, {
                routeKey: routeKey,
                children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.ErrorBoundary, {
                  children: areas.mobile
                })
              }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_saveHub.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_savePanel.default, {})]
            }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.ErrorBoundary, {
              children: areas.mobile
            })
          })
        }), !isMobileViewport && areas.content && canvas !== 'edit' && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "edit-site-layout__area",
          style: {
            maxWidth: widths?.content
          },
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.ErrorBoundary, {
            children: areas.content
          })
        }), !isMobileViewport && areas.edit && canvas !== 'edit' && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "edit-site-layout__area",
          style: {
            maxWidth: widths?.edit
          },
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.ErrorBoundary, {
            children: areas.edit
          })
        }), !isMobileViewport && areas.preview && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "edit-site-layout__canvas-container",
          children: [canvasResizer, !!canvasSize.width && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: (0, _clsx.default)('edit-site-layout__canvas', {
              'is-right-aligned': isResizableFrameOversized
            }),
            ref: animationRef,
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_editor.ErrorBoundary, {
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_resizableFrame.default, {
                isReady: !isEditorLoading,
                isFullWidth: canvas === 'edit',
                defaultSize: {
                  width: canvasSize.width - 24 /* $canvas-padding */,
                  height: canvasSize.height
                },
                isOversized: isResizableFrameOversized,
                setIsOversized: setIsResizableFrameOversized,
                innerContentStyle: {
                  background: gradientValue !== null && gradientValue !== void 0 ? gradientValue : backgroundColor
                },
                children: areas.preview
              })
            })
          })]
        })]
      })
    })]
  });
}
function LayoutWithGlobalStylesProvider(props) {
  const {
    createErrorNotice
  } = (0, _data.useDispatch)(_notices.store);
  function onPluginAreaError(name) {
    createErrorNotice((0, _i18n.sprintf)(/* translators: %s: plugin name */
    (0, _i18n.__)('The "%s" plugin has encountered an error and cannot be rendered.'), name));
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.SlotFillProvider, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(GlobalStylesProvider, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_plugins.PluginArea, {
        onError: onPluginAreaError
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(Layout, {
        ...props
      })]
    })
  });
}
//# sourceMappingURL=index.js.map