"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.openGeneralSidebar = exports.closeGeneralSidebar = exports.addTemplate = exports.__experimentalSetPreviewDeviceType = void 0;
exports.openNavigationPanelToMenu = openNavigationPanelToMenu;
exports.revertTemplate = exports.removeTemplate = void 0;
exports.setEditedEntity = setEditedEntity;
exports.setEditedPostContext = setEditedPostContext;
exports.setHasPageContentFocus = void 0;
exports.setHomeTemplateId = setHomeTemplateId;
exports.setIsListViewOpened = exports.setIsInserterOpened = void 0;
exports.setIsNavigationPanelOpened = setIsNavigationPanelOpened;
exports.setIsSaveViewOpened = setIsSaveViewOpened;
exports.setNavigationMenu = setNavigationMenu;
exports.setNavigationPanelActiveMenu = setNavigationPanelActiveMenu;
exports.setPage = setPage;
exports.setTemplate = setTemplate;
exports.setTemplatePart = setTemplatePart;
exports.toggleDistractionFree = exports.switchEditorMode = void 0;
exports.toggleFeature = toggleFeature;
exports.updateSettings = updateSettings;
var _blocks = require("@wordpress/blocks");
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
var _coreData = require("@wordpress/core-data");
var _blockEditor = require("@wordpress/block-editor");
var _editor = require("@wordpress/editor");
var _preferences = require("@wordpress/preferences");
var _constants = require("../utils/constants");
var _lockUnlock = require("../lock-unlock");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  interfaceStore
} = (0, _lockUnlock.unlock)(_editor.privateApis);

/**
 * Dispatches an action that toggles a feature flag.
 *
 * @param {string} featureName Feature name.
 */
function toggleFeature(featureName) {
  return function ({
    registry
  }) {
    (0, _deprecated.default)("dispatch( 'core/edit-site' ).toggleFeature( featureName )", {
      since: '6.0',
      alternative: "dispatch( 'core/preferences').toggle( 'core/edit-site', featureName )"
    });
    registry.dispatch(_preferences.store).toggle('core/edit-site', featureName);
  };
}

/**
 * Action that changes the width of the editing canvas.
 *
 * @deprecated
 *
 * @param {string} deviceType
 *
 * @return {Object} Action object.
 */
const __experimentalSetPreviewDeviceType = deviceType => ({
  registry
}) => {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).__experimentalSetPreviewDeviceType", {
    since: '6.5',
    version: '6.7',
    hint: 'registry.dispatch( editorStore ).setDeviceType'
  });
  registry.dispatch(_editor.store).setDeviceType(deviceType);
};

/**
 * Action that sets a template, optionally fetching it from REST API.
 *
 * @return {Object} Action object.
 */
exports.__experimentalSetPreviewDeviceType = __experimentalSetPreviewDeviceType;
function setTemplate() {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setTemplate", {
    since: '6.5',
    version: '6.8',
    hint: 'The setTemplate is not needed anymore, the correct entity is resolved from the URL automatically.'
  });
  return {
    type: 'NOTHING'
  };
}

/**
 * Action that adds a new template and sets it as the current template.
 *
 * @param {Object} template The template.
 *
 * @deprecated
 *
 * @return {Object} Action object used to set the current template.
 */
const addTemplate = template => async ({
  dispatch,
  registry
}) => {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).addTemplate", {
    since: '6.5',
    version: '6.8',
    hint: 'use saveEntityRecord directly'
  });
  const newTemplate = await registry.dispatch(_coreData.store).saveEntityRecord('postType', _constants.TEMPLATE_POST_TYPE, template);
  if (template.content) {
    registry.dispatch(_coreData.store).editEntityRecord('postType', _constants.TEMPLATE_POST_TYPE, newTemplate.id, {
      blocks: (0, _blocks.parse)(template.content)
    }, {
      undoIgnore: true
    });
  }
  dispatch({
    type: 'SET_EDITED_POST',
    postType: _constants.TEMPLATE_POST_TYPE,
    id: newTemplate.id
  });
};

/**
 * Action that removes a template.
 *
 * @param {Object} template The template object.
 */
exports.addTemplate = addTemplate;
const removeTemplate = template => ({
  registry
}) => {
  return (0, _lockUnlock.unlock)(registry.dispatch(_editor.store)).removeTemplates([template]);
};

/**
 * Action that sets a template part.
 *
 * @deprecated
 * @param {string} templatePartId The template part ID.
 *
 * @return {Object} Action object.
 */
exports.removeTemplate = removeTemplate;
function setTemplatePart(templatePartId) {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setTemplatePart", {
    since: '6.8'
  });
  return {
    type: 'SET_EDITED_POST',
    postType: _constants.TEMPLATE_PART_POST_TYPE,
    id: templatePartId
  };
}

/**
 * Action that sets a navigation menu.
 *
 * @deprecated
 * @param {string} navigationMenuId The Navigation Menu Post ID.
 *
 * @return {Object} Action object.
 */
function setNavigationMenu(navigationMenuId) {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setNavigationMenu", {
    since: '6.8'
  });
  return {
    type: 'SET_EDITED_POST',
    postType: _constants.NAVIGATION_POST_TYPE,
    id: navigationMenuId
  };
}

/**
 * Action that sets an edited entity.
 *
 * @deprecated
 * @param {string} postType The entity's post type.
 * @param {string} postId   The entity's ID.
 * @param {Object} context  The entity's context.
 *
 * @return {Object} Action object.
 */
function setEditedEntity(postType, postId, context) {
  return {
    type: 'SET_EDITED_POST',
    postType,
    id: postId,
    context
  };
}

/**
 * @deprecated
 */
function setHomeTemplateId() {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setHomeTemplateId", {
    since: '6.2',
    version: '6.4'
  });
  return {
    type: 'NOTHING'
  };
}

/**
 * Set's the current block editor context.
 *
 * @deprecated
 * @param {Object} context The context object.
 *
 * @return {Object} Action object.
 */
function setEditedPostContext(context) {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setEditedPostContext", {
    since: '6.8'
  });
  return {
    type: 'SET_EDITED_POST_CONTEXT',
    context
  };
}

/**
 * Resolves the template for a page and displays both. If no path is given, attempts
 * to use the postId to generate a path like `?p=${ postId }`.
 *
 * @deprecated
 *
 * @return {Object} Action object.
 */
function setPage() {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setPage", {
    since: '6.5',
    version: '6.8',
    hint: 'The setPage is not needed anymore, the correct entity is resolved from the URL automatically.'
  });
  return {
    type: 'NOTHING'
  };
}

/**
 * Action that sets the active navigation panel menu.
 *
 * @deprecated
 *
 * @return {Object} Action object.
 */
function setNavigationPanelActiveMenu() {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setNavigationPanelActiveMenu", {
    since: '6.2',
    version: '6.4'
  });
  return {
    type: 'NOTHING'
  };
}

/**
 * Opens the navigation panel and sets its active menu at the same time.
 *
 * @deprecated
 */
function openNavigationPanelToMenu() {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).openNavigationPanelToMenu", {
    since: '6.2',
    version: '6.4'
  });
  return {
    type: 'NOTHING'
  };
}

/**
 * Sets whether the navigation panel should be open.
 *
 * @deprecated
 */
function setIsNavigationPanelOpened() {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setIsNavigationPanelOpened", {
    since: '6.2',
    version: '6.4'
  });
  return {
    type: 'NOTHING'
  };
}

/**
 * Returns an action object used to open/close the inserter.
 *
 * @deprecated
 *
 * @param {boolean|Object} value Whether the inserter should be opened (true) or closed (false).
 */
const setIsInserterOpened = value => ({
  registry
}) => {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setIsInserterOpened", {
    since: '6.5',
    alternative: "dispatch( 'core/editor').setIsInserterOpened"
  });
  registry.dispatch(_editor.store).setIsInserterOpened(value);
};

/**
 * Returns an action object used to open/close the list view.
 *
 * @deprecated
 *
 * @param {boolean} isOpen A boolean representing whether the list view should be opened or closed.
 */
exports.setIsInserterOpened = setIsInserterOpened;
const setIsListViewOpened = isOpen => ({
  registry
}) => {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).setIsListViewOpened", {
    since: '6.5',
    alternative: "dispatch( 'core/editor').setIsListViewOpened"
  });
  registry.dispatch(_editor.store).setIsListViewOpened(isOpen);
};

/**
 * Returns an action object used to update the settings.
 *
 * @param {Object} settings New settings.
 *
 * @return {Object} Action object.
 */
exports.setIsListViewOpened = setIsListViewOpened;
function updateSettings(settings) {
  return {
    type: 'UPDATE_SETTINGS',
    settings
  };
}

/**
 * Sets whether the save view panel should be open.
 *
 * @param {boolean} isOpen If true, opens the save view. If false, closes it.
 *                         It does not toggle the state, but sets it directly.
 */
function setIsSaveViewOpened(isOpen) {
  return {
    type: 'SET_IS_SAVE_VIEW_OPENED',
    isOpen
  };
}

/**
 * Reverts a template to its original theme-provided file.
 *
 * @param {Object}  template            The template to revert.
 * @param {Object}  [options]
 * @param {boolean} [options.allowUndo] Whether to allow the user to undo
 *                                      reverting the template. Default true.
 */
const revertTemplate = (template, options) => ({
  registry
}) => {
  return (0, _lockUnlock.unlock)(registry.dispatch(_editor.store)).revertTemplate(template, options);
};

/**
 * Action that opens an editor sidebar.
 *
 * @param {?string} name Sidebar name to be opened.
 */
exports.revertTemplate = revertTemplate;
const openGeneralSidebar = name => ({
  registry
}) => {
  registry.dispatch(interfaceStore).enableComplementaryArea('core', name);
};

/**
 * Action that closes the sidebar.
 */
exports.openGeneralSidebar = openGeneralSidebar;
const closeGeneralSidebar = () => ({
  registry
}) => {
  registry.dispatch(interfaceStore).disableComplementaryArea('core');
};

/**
 * Triggers an action used to switch editor mode.
 *
 * @deprecated
 *
 * @param {string} mode The editor mode.
 */
exports.closeGeneralSidebar = closeGeneralSidebar;
const switchEditorMode = mode => ({
  registry
}) => {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).switchEditorMode", {
    since: '6.6',
    alternative: "dispatch( 'core/editor').switchEditorMode"
  });
  registry.dispatch(_editor.store).switchEditorMode(mode);
};

/**
 * Sets whether or not the editor allows only page content to be edited.
 *
 * @param {boolean} hasPageContentFocus True to allow only page content to be
 *                                      edited, false to allow template to be
 *                                      edited.
 */
exports.switchEditorMode = switchEditorMode;
const setHasPageContentFocus = hasPageContentFocus => ({
  dispatch,
  registry
}) => {
  (0, _deprecated.default)(`dispatch( 'core/edit-site' ).setHasPageContentFocus`, {
    since: '6.5'
  });
  if (hasPageContentFocus) {
    registry.dispatch(_blockEditor.store).clearSelectedBlock();
  }
  dispatch({
    type: 'SET_HAS_PAGE_CONTENT_FOCUS',
    hasPageContentFocus
  });
};

/**
 * Action that toggles Distraction free mode.
 * Distraction free mode expects there are no sidebars, as due to the
 * z-index values set, you can't close sidebars.
 *
 * @deprecated
 */
exports.setHasPageContentFocus = setHasPageContentFocus;
const toggleDistractionFree = () => ({
  registry
}) => {
  (0, _deprecated.default)("dispatch( 'core/edit-site' ).toggleDistractionFree", {
    since: '6.6',
    alternative: "dispatch( 'core/editor').toggleDistractionFree"
  });
  registry.dispatch(_editor.store).toggleDistractionFree();
};
exports.toggleDistractionFree = toggleDistractionFree;
//# sourceMappingURL=actions.js.map