"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.registerEntityAction = registerEntityAction;
exports.registerPostTypeActions = void 0;
exports.setIsReady = setIsReady;
exports.unregisterEntityAction = unregisterEntityAction;
var _coreData = require("@wordpress/core-data");
var _hooks = require("@wordpress/hooks");
var _duplicateTemplatePart = _interopRequireDefault(require("../actions/duplicate-template-part"));
var _resetPost = _interopRequireDefault(require("../actions/reset-post"));
var _trashPost = _interopRequireDefault(require("../actions/trash-post"));
var _renamePost = _interopRequireDefault(require("../actions/rename-post"));
var _restorePost = _interopRequireDefault(require("../actions/restore-post"));
var _store = require("../../store");
var _lockUnlock = require("../../lock-unlock");
var _fields = require("@wordpress/fields");
var _deletePost = _interopRequireDefault(require("../actions/delete-post"));
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function registerEntityAction(kind, name, config) {
  return {
    type: 'REGISTER_ENTITY_ACTION',
    kind,
    name,
    config
  };
}
function unregisterEntityAction(kind, name, actionId) {
  return {
    type: 'UNREGISTER_ENTITY_ACTION',
    kind,
    name,
    actionId
  };
}
function setIsReady(kind, name) {
  return {
    type: 'SET_IS_READY',
    kind,
    name
  };
}
const registerPostTypeActions = postType => async ({
  registry
}) => {
  const isReady = (0, _lockUnlock.unlock)(registry.select(_store.store)).isEntityReady('postType', postType);
  if (isReady) {
    return;
  }
  (0, _lockUnlock.unlock)(registry.dispatch(_store.store)).setIsReady('postType', postType);
  const postTypeConfig = await registry.resolveSelect(_coreData.store).getPostType(postType);
  const canCreate = await registry.resolveSelect(_coreData.store).canUser('create', {
    kind: 'postType',
    name: postType
  });
  const currentTheme = await registry.resolveSelect(_coreData.store).getCurrentTheme();
  const actions = [postTypeConfig.viewable ? _fields.viewPost : undefined, !!postTypeConfig?.supports?.revisions ? _fields.viewPostRevisions : undefined,
  // @ts-ignore
  globalThis.IS_GUTENBERG_PLUGIN ? !['wp_template', 'wp_block', 'wp_template_part'].includes(postTypeConfig.slug) && canCreate && _fields.duplicatePost : undefined, postTypeConfig.slug === 'wp_template_part' && canCreate && currentTheme?.is_block_theme ? _duplicateTemplatePart.default : undefined, canCreate && postTypeConfig.slug === 'wp_block' ? _fields.duplicatePattern : undefined, postTypeConfig.supports?.title ? _renamePost.default : undefined, postTypeConfig?.supports?.['page-attributes'] ? _fields.reorderPage : undefined, postTypeConfig.slug === 'wp_block' ? _fields.exportPattern : undefined, _resetPost.default, _restorePost.default, _deletePost.default, _trashPost.default, _fields.permanentlyDeletePost];
  registry.batch(() => {
    actions.forEach(action => {
      if (!action) {
        return;
      }
      (0, _lockUnlock.unlock)(registry.dispatch(_store.store)).registerEntityAction('postType', postType, action);
    });
  });
  (0, _hooks.doAction)('core.registerPostTypeActions', postType);
};
exports.registerPostTypeActions = registerPostTypeActions;
//# sourceMappingURL=private-actions.js.map