"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useSpecificEditorSettings = useSpecificEditorSettings;
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _router = require("@wordpress/router");
var _compose = require("@wordpress/compose");
var _store = require("../../store");
var _lockUnlock = require("../../lock-unlock");
var _useNavigateToEntityRecord = _interopRequireDefault(require("./use-navigate-to-entity-record"));
var _constants = require("../../utils/constants");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useLocation,
  useHistory
} = (0, _lockUnlock.unlock)(_router.privateApis);
function useNavigateToPreviousEntityRecord() {
  const location = useLocation();
  const previousLocation = (0, _compose.usePrevious)(location);
  const history = useHistory();
  const goBack = (0, _element.useMemo)(() => {
    const isFocusMode = location.params.focusMode || location.params.postId && _constants.FOCUSABLE_ENTITIES.includes(location.params.postType);
    const didComeFromEditorCanvas = previousLocation?.params.canvas === 'edit';
    const showBackButton = isFocusMode && didComeFromEditorCanvas;
    return showBackButton ? () => history.back() : undefined;
    // Disable reason: previousLocation changes when the component updates for any reason, not
    // just when location changes. Until this is fixed we can't add it to deps. See
    // https://github.com/WordPress/gutenberg/pull/58710#discussion_r1479219465.
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [location, history]);
  return goBack;
}
function useSpecificEditorSettings() {
  const onNavigateToEntityRecord = (0, _useNavigateToEntityRecord.default)();
  const {
    canvasMode,
    settings,
    shouldUseTemplateAsDefaultRenderingMode
  } = (0, _data.useSelect)(select => {
    const {
      getEditedPostContext,
      getCanvasMode,
      getSettings
    } = (0, _lockUnlock.unlock)(select(_store.store));
    const _context = getEditedPostContext();
    return {
      canvasMode: getCanvasMode(),
      settings: getSettings(),
      // TODO: The `postType` check should be removed when the default rendering mode per post type is merged.
      // @see https://github.com/WordPress/gutenberg/pull/62304/
      shouldUseTemplateAsDefaultRenderingMode: _context?.postId && _context?.postType !== 'post'
    };
  }, []);
  const defaultRenderingMode = shouldUseTemplateAsDefaultRenderingMode ? 'template-locked' : 'post-only';
  const onNavigateToPreviousEntityRecord = useNavigateToPreviousEntityRecord();
  const defaultEditorSettings = (0, _element.useMemo)(() => {
    return {
      ...settings,
      richEditingEnabled: true,
      supportsTemplateMode: true,
      focusMode: canvasMode !== 'view',
      defaultRenderingMode,
      onNavigateToEntityRecord,
      onNavigateToPreviousEntityRecord,
      __unstableIsPreviewMode: canvasMode === 'view'
    };
  }, [settings, canvasMode, defaultRenderingMode, onNavigateToEntityRecord, onNavigateToPreviousEntityRecord]);
  return defaultEditorSettings;
}
//# sourceMappingURL=use-site-editor-settings.js.map