"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.query.focusMode || location?.params?.postId && _constants.FOCUSABLE_ENTITIES.includes(location?.params?.postType);
    const didComeFromEditorCanvas = previousLocation?.query.canvas === 'edit';
    const showBackButton = isFocusMode && didComeFromEditorCanvas;
    return showBackButton ? () => history.back() : undefined;
    // `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.
  }, [location, history]);
  return goBack;
}
function useSpecificEditorSettings() {
  const {
    query
  } = useLocation();
  const {
    canvas = 'view'
  } = query;
  const onNavigateToEntityRecord = (0, _useNavigateToEntityRecord.default)();
  const {
    settings
  } = (0, _data.useSelect)(select => {
    const {
      getSettings
    } = select(_store.store);
    return {
      settings: getSettings()
    };
  }, []);
  const onNavigateToPreviousEntityRecord = useNavigateToPreviousEntityRecord();
  const defaultEditorSettings = (0, _element.useMemo)(() => {
    return {
      ...settings,
      richEditingEnabled: true,
      supportsTemplateMode: true,
      focusMode: canvas !== 'view',
      onNavigateToEntityRecord,
      onNavigateToPreviousEntityRecord,
      isPreviewMode: canvas === 'view'
    };
  }, [settings, canvas, onNavigateToEntityRecord, onNavigateToPreviousEntityRecord]);
  return defaultEditorSettings;
}
//# sourceMappingURL=use-site-editor-settings.js.map