"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setEditorCanvasContainerView = exports.setCanvasMode = void 0;
var _blockEditor = require("@wordpress/block-editor");
var _preferences = require("@wordpress/preferences");
var _editor = require("@wordpress/editor");
/**
 * WordPress dependencies
 */

/**
 * Action that switches the canvas mode.
 *
 * @param {?string} mode Canvas mode.
 */
const setCanvasMode = mode => ({
  registry,
  dispatch
}) => {
  const isMediumOrBigger = window.matchMedia('(min-width: 782px)').matches;
  const switchCanvasMode = () => {
    registry.batch(() => {
      registry.dispatch(_blockEditor.store).clearSelectedBlock();
      registry.dispatch(_editor.store).setDeviceType('Desktop');
      registry.dispatch(_blockEditor.store).__unstableSetEditorMode('edit');
      const isPublishSidebarOpened = registry.select(_editor.store).isPublishSidebarOpened();
      dispatch({
        type: 'SET_CANVAS_MODE',
        mode
      });
      const isEditMode = mode === 'edit';
      if (isPublishSidebarOpened && !isEditMode) {
        registry.dispatch(_editor.store).closePublishSidebar();
      }

      // Check if the block list view should be open by default.
      // If `distractionFree` mode is enabled, the block list view should not be open.
      // This behavior is disabled for small viewports.
      if (isMediumOrBigger && isEditMode && registry.select(_preferences.store).get('core', 'showListViewByDefault') && !registry.select(_preferences.store).get('core', 'distractionFree')) {
        registry.dispatch(_editor.store).setIsListViewOpened(true);
      } else {
        registry.dispatch(_editor.store).setIsListViewOpened(false);
      }
      registry.dispatch(_editor.store).setIsInserterOpened(false);
    });
  };

  /*
   * Skip transition in mobile, otherwise it crashes the browser.
   * See: https://github.com/WordPress/gutenberg/pull/63002.
   */
  if (!isMediumOrBigger || !document.startViewTransition) {
    switchCanvasMode();
  } else {
    document.documentElement.classList.add(`canvas-mode-${mode}-transition`);
    const transition = document.startViewTransition(() => switchCanvasMode());
    transition.finished.finally(() => {
      document.documentElement.classList.remove(`canvas-mode-${mode}-transition`);
    });
  }
};

/**
 * Action that switches the editor canvas container view.
 *
 * @param {?string} view Editor canvas container view.
 */
exports.setCanvasMode = setCanvasMode;
const setEditorCanvasContainerView = view => ({
  dispatch
}) => {
  dispatch({
    type: 'SET_EDITOR_CANVAS_CONTAINER_VIEW',
    view
  });
};
exports.setEditorCanvasContainerView = setEditorCanvasContainerView;
//# sourceMappingURL=private-actions.js.map