"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/hooks/block-hooks.js
var block_hooks_exports = {};
__export(block_hooks_exports, {
  default: () => block_hooks_default
});
module.exports = __toCommonJS(block_hooks_exports);
var import_i18n = require("@wordpress/i18n");
var import_element = require("@wordpress/element");
var import_components = require("@wordpress/components");
var import_blocks = require("@wordpress/blocks");
var import_data = require("@wordpress/data");
var import_components2 = require("../components");
var import_store = require("../store");
var import_jsx_runtime = require("react/jsx-runtime");
var EMPTY_OBJECT = {};
function BlockHooksControlPure({
  name,
  clientId,
  metadata: { ignoredHookedBlocks = [] } = {}
}) {
  const blockTypes = (0, import_data.useSelect)(
    (select) => select(import_blocks.store).getBlockTypes(),
    []
  );
  const hookedBlocksForCurrentBlock = (0, import_element.useMemo)(
    () => blockTypes?.filter(
      ({ name: blockName, blockHooks }) => blockHooks && name in blockHooks || ignoredHookedBlocks.includes(blockName)
    ),
    [blockTypes, name, ignoredHookedBlocks]
  );
  const hookedBlockClientIds = (0, import_data.useSelect)(
    (select) => {
      const { getBlocks, getBlockRootClientId: getBlockRootClientId2, getGlobalBlockCount } = select(import_store.store);
      const rootClientId = getBlockRootClientId2(clientId);
      const _hookedBlockClientIds = hookedBlocksForCurrentBlock.reduce(
        (clientIds, block) => {
          if (getGlobalBlockCount(block.name) === 0) {
            return clientIds;
          }
          const relativePosition = block?.blockHooks?.[name];
          let candidates;
          switch (relativePosition) {
            case "before":
            case "after":
              candidates = getBlocks(rootClientId);
              break;
            case "first_child":
            case "last_child":
              candidates = getBlocks(clientId);
              break;
            case void 0:
              candidates = [
                ...getBlocks(rootClientId),
                ...getBlocks(clientId)
              ];
              break;
          }
          const hookedBlock = candidates?.find(
            (candidate) => candidate.name === block.name
          );
          if (hookedBlock) {
            return {
              ...clientIds,
              [block.name]: hookedBlock.clientId
            };
          }
          return clientIds;
        },
        {}
      );
      if (Object.values(_hookedBlockClientIds).length > 0) {
        return _hookedBlockClientIds;
      }
      return EMPTY_OBJECT;
    },
    [hookedBlocksForCurrentBlock, name, clientId]
  );
  const { getBlockIndex, getBlockCount, getBlockRootClientId } = (0, import_data.useSelect)(import_store.store);
  const { insertBlock, removeBlock } = (0, import_data.useDispatch)(import_store.store);
  if (!hookedBlocksForCurrentBlock.length) {
    return null;
  }
  const groupedHookedBlocks = hookedBlocksForCurrentBlock.reduce(
    (groups, block) => {
      const [namespace] = block.name.split("/");
      if (!groups[namespace]) {
        groups[namespace] = [];
      }
      groups[namespace].push(block);
      return groups;
    },
    {}
  );
  const insertBlockIntoDesignatedLocation = (block, relativePosition) => {
    const blockIndex = getBlockIndex(clientId);
    const innerBlocksLength = getBlockCount(clientId);
    const rootClientId = getBlockRootClientId(clientId);
    switch (relativePosition) {
      case "before":
      case "after":
        insertBlock(
          block,
          relativePosition === "after" ? blockIndex + 1 : blockIndex,
          rootClientId,
          // Insert as a child of the current block's parent
          false
        );
        break;
      case "first_child":
      case "last_child":
        insertBlock(
          block,
          // TODO: It'd be great if insertBlock() would accept negative indices for insertion.
          relativePosition === "first_child" ? 0 : innerBlocksLength,
          clientId,
          // Insert as a child of the current block.
          false
        );
        break;
      case void 0:
        insertBlock(
          block,
          blockIndex + 1,
          rootClientId,
          // Insert as a child of the current block's parent
          false
        );
        break;
    }
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components2.InspectorControls, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
    import_components.PanelBody,
    {
      className: "block-editor-hooks__block-hooks",
      title: (0, import_i18n.__)("Plugins"),
      initialOpen: true,
      children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "block-editor-hooks__block-hooks-helptext", children: (0, import_i18n.__)(
          "Manage the inclusion of blocks added automatically by plugins."
        ) }),
        Object.keys(groupedHookedBlocks).map((vendor) => {
          return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_element.Fragment, { children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h3", { children: vendor }),
            groupedHookedBlocks[vendor].map((block) => {
              const checked = block.name in hookedBlockClientIds;
              return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
                import_components.ToggleControl,
                {
                  __nextHasNoMarginBottom: true,
                  checked,
                  label: block.title,
                  onChange: () => {
                    if (!checked) {
                      const relativePosition = block.blockHooks[name];
                      insertBlockIntoDesignatedLocation(
                        (0, import_blocks.createBlock)(block.name),
                        relativePosition
                      );
                      return;
                    }
                    removeBlock(
                      hookedBlockClientIds[block.name],
                      false
                    );
                  }
                },
                block.title
              );
            })
          ] }, vendor);
        })
      ]
    }
  ) });
}
var block_hooks_default = {
  edit: BlockHooksControlPure,
  attributeKeys: ["metadata"],
  hasSupport() {
    return true;
  }
};
//# sourceMappingURL=block-hooks.js.map
