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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = BlockRemovalWarnings;
var _i18n = require("@wordpress/i18n");
var _blockEditor = require("@wordpress/block-editor");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _lockUnlock = require("../../lock-unlock");
var _store = require("../../store");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  BlockRemovalWarningModal
} = (0, _lockUnlock.unlock)(_blockEditor.privateApis);

// Prevent accidental removal of certain blocks, asking the user for confirmation first.
const TEMPLATE_BLOCKS = ['core/post-content', 'core/post-template', 'core/query'];
const BLOCK_REMOVAL_RULES = [{
  // Template blocks.
  // The warning is only shown when a user manipulates templates or template parts.
  postTypes: ['wp_template', 'wp_template_part'],
  callback(removedBlocks) {
    const removedTemplateBlocks = removedBlocks.filter(({
      name
    }) => TEMPLATE_BLOCKS.includes(name));
    if (removedTemplateBlocks.length) {
      return (0, _i18n._n)('Deleting this block will stop your post or page content from displaying on this template. It is not recommended.', 'Some of the deleted blocks will stop your post or page content from displaying on this template. It is not recommended.', removedBlocks.length);
    }
  }
}, {
  // Pattern overrides.
  // The warning is only shown when the user edits a pattern.
  postTypes: ['wp_block'],
  callback(removedBlocks) {
    const removedBlocksWithOverrides = removedBlocks.filter(({
      attributes
    }) => attributes?.metadata?.bindings && Object.values(attributes.metadata.bindings).some(binding => binding.source === 'core/pattern-overrides'));
    if (removedBlocksWithOverrides.length) {
      return (0, _i18n._n)('The deleted block allows instance overrides. Removing it may result in content not displaying where this pattern is used. Are you sure you want to proceed?', 'Some of the deleted blocks allow instance overrides. Removing them may result in content not displaying where this pattern is used. Are you sure you want to proceed?', removedBlocks.length);
    }
  }
}];
function BlockRemovalWarnings() {
  const currentPostType = (0, _data.useSelect)(select => select(_store.store).getCurrentPostType(), []);
  const removalRulesForPostType = (0, _element.useMemo)(() => BLOCK_REMOVAL_RULES.filter(rule => rule.postTypes.includes(currentPostType)), [currentPostType]);

  // `BlockRemovalWarnings` is rendered in the editor provider, a shared component
  // across react native and web. However, `BlockRemovalWarningModal` is web only.
  // Check it exists before trying to render it.
  if (!BlockRemovalWarningModal) {
    return null;
  }
  if (!removalRulesForPostType) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(BlockRemovalWarningModal, {
    rules: removalRulesForPostType
  });
}
//# sourceMappingURL=index.js.map