"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useEditModeCommands = useEditModeCommands;
var _data = require("@wordpress/data");
var _i18n = require("@wordpress/i18n");
var _icons = require("@wordpress/icons");
var _commands = require("@wordpress/commands");
var _htmlEntities = require("@wordpress/html-entities");
var _router = require("@wordpress/router");
var _editor = require("@wordpress/editor");
var _store = require("../../store");
var _useEditedEntityRecord = _interopRequireDefault(require("../../components/use-edited-entity-record"));
var _isTemplateRemovable = _interopRequireDefault(require("../../utils/is-template-removable"));
var _isTemplateRevertable = _interopRequireDefault(require("../../utils/is-template-revertable"));
var _lockUnlock = require("../../lock-unlock");
var _constants = require("../../utils/constants");
var _link = require("../../components/routes/link");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useHistory
} = (0, _lockUnlock.unlock)(_router.privateApis);
function usePageContentFocusCommands() {
  const {
    record: template
  } = (0, _useEditedEntityRecord.default)();
  const {
    isPage,
    canvasMode,
    templateId,
    currentPostType
  } = (0, _data.useSelect)(select => {
    const {
      isPage: _isPage,
      getCanvasMode
    } = (0, _lockUnlock.unlock)(select(_store.store));
    const {
      getCurrentPostType,
      getCurrentTemplateId
    } = select(_editor.store);
    return {
      isPage: _isPage(),
      canvasMode: getCanvasMode(),
      templateId: getCurrentTemplateId(),
      currentPostType: getCurrentPostType()
    };
  }, []);
  const {
    onClick: editTemplate
  } = (0, _link.useLink)({
    postType: 'wp_template',
    postId: templateId
  });
  const {
    setRenderingMode
  } = (0, _data.useDispatch)(_editor.store);
  if (!isPage || canvasMode !== 'edit') {
    return {
      isLoading: false,
      commands: []
    };
  }
  const commands = [];
  if (currentPostType !== 'wp_template') {
    commands.push({
      name: 'core/switch-to-template-focus',
      label: (0, _i18n.sprintf)(/* translators: %s: template title */
      (0, _i18n.__)('Edit template: %s'), (0, _htmlEntities.decodeEntities)(template.title)),
      icon: _icons.layout,
      callback: ({
        close
      }) => {
        editTemplate();
        close();
      }
    });
  } else {
    commands.push({
      name: 'core/switch-to-page-focus',
      label: (0, _i18n.__)('Back to page'),
      icon: _icons.page,
      callback: ({
        close
      }) => {
        setRenderingMode('template-locked');
        close();
      }
    });
  }
  return {
    isLoading: false,
    commands
  };
}
function useManipulateDocumentCommands() {
  const {
    isLoaded,
    record: template
  } = (0, _useEditedEntityRecord.default)();
  const {
    removeTemplate,
    revertTemplate
  } = (0, _data.useDispatch)(_store.store);
  const history = useHistory();
  const isEditingPage = (0, _data.useSelect)(select => select(_store.store).isPage() && select(_editor.store).getCurrentPostType() !== 'wp_template', []);
  if (!isLoaded) {
    return {
      isLoading: true,
      commands: []
    };
  }
  const commands = [];
  if ((0, _isTemplateRevertable.default)(template) && !isEditingPage) {
    const label = template.type === _constants.TEMPLATE_POST_TYPE ? (0, _i18n.sprintf)(/* translators: %s: template title */
    (0, _i18n.__)('Reset template: %s'), (0, _htmlEntities.decodeEntities)(template.title)) : (0, _i18n.sprintf)(/* translators: %s: template part title */
    (0, _i18n.__)('Reset template part: %s'), (0, _htmlEntities.decodeEntities)(template.title));
    commands.push({
      name: 'core/reset-template',
      label,
      icon: (0, _i18n.isRTL)() ? _icons.rotateRight : _icons.rotateLeft,
      callback: ({
        close
      }) => {
        revertTemplate(template);
        close();
      }
    });
  }
  if ((0, _isTemplateRemovable.default)(template) && !isEditingPage) {
    const label = template.type === _constants.TEMPLATE_POST_TYPE ? (0, _i18n.sprintf)(/* translators: %s: template title */
    (0, _i18n.__)('Delete template: %s'), (0, _htmlEntities.decodeEntities)(template.title)) : (0, _i18n.sprintf)(/* translators: %s: template part title */
    (0, _i18n.__)('Delete template part: %s'), (0, _htmlEntities.decodeEntities)(template.title));
    commands.push({
      name: 'core/remove-template',
      label,
      icon: _icons.trash,
      callback: ({
        close
      }) => {
        removeTemplate(template);
        // Navigate to the template list
        history.push({
          postType: template.type
        });
        close();
      }
    });
  }
  return {
    isLoading: !isLoaded,
    commands
  };
}
function useEditModeCommands() {
  (0, _commands.useCommandLoader)({
    name: 'core/edit-site/page-content-focus',
    hook: usePageContentFocusCommands,
    context: 'entity-edit'
  });
  (0, _commands.useCommandLoader)({
    name: 'core/edit-site/manipulate-document',
    hook: useManipulateDocumentCommands
  });
}
//# sourceMappingURL=use-edit-mode-commands.js.map