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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = SaveKeyboardShortcut;
var _element = require("@wordpress/element");
var _keyboardShortcuts = require("@wordpress/keyboard-shortcuts");
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _coreData = require("@wordpress/core-data");
var _editor = require("@wordpress/editor");
var _store = require("../../store");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const shortcutName = 'core/edit-site/save';

/**
 * Register the save keyboard shortcut in view mode.
 *
 * @return {null} Returns null.
 */
function SaveKeyboardShortcut() {
  const {
    __experimentalGetDirtyEntityRecords,
    isSavingEntityRecord
  } = (0, _data.useSelect)(_coreData.store);
  const {
    hasNonPostEntityChanges,
    isPostSavingLocked
  } = (0, _data.useSelect)(_editor.store);
  const {
    savePost
  } = (0, _data.useDispatch)(_editor.store);
  const {
    setIsSaveViewOpened
  } = (0, _data.useDispatch)(_store.store);
  const {
    registerShortcut,
    unregisterShortcut
  } = (0, _data.useDispatch)(_keyboardShortcuts.store);
  (0, _element.useEffect)(() => {
    registerShortcut({
      name: shortcutName,
      category: 'global',
      description: (0, _i18n.__)('Save your changes.'),
      keyCombination: {
        modifier: 'primary',
        character: 's'
      }
    });
    return () => {
      unregisterShortcut(shortcutName);
    };
  }, [registerShortcut, unregisterShortcut]);
  (0, _keyboardShortcuts.useShortcut)('core/edit-site/save', event => {
    event.preventDefault();
    const dirtyEntityRecords = __experimentalGetDirtyEntityRecords();
    const hasDirtyEntities = !!dirtyEntityRecords.length;
    const isSaving = dirtyEntityRecords.some(record => isSavingEntityRecord(record.kind, record.name, record.key));
    if (!hasDirtyEntities || isSaving) {
      return;
    }
    if (hasNonPostEntityChanges()) {
      setIsSaveViewOpened(true);
    } else if (!isPostSavingLocked()) {
      savePost();
    }
  });
  return null;
}
//# sourceMappingURL=index.js.map