"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-library/src/list-item/hooks/use-enter.js
var use_enter_exports = {};
__export(use_enter_exports, {
  default: () => useEnter
});
module.exports = __toCommonJS(use_enter_exports);
var import_blocks = require("@wordpress/blocks");
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_keycodes = require("@wordpress/keycodes");
var import_data = require("@wordpress/data");
var import_block_editor = require("@wordpress/block-editor");
var import_use_outdent_list_item = __toESM(require("./use-outdent-list-item.cjs"));
var import_lock_unlock = require("../../lock-unlock.cjs");
var { subscribeDelegatedListener } = (0, import_lock_unlock.unlock)(import_compose.privateApis);
function useEnter(props) {
  const { replaceBlocks, selectionChange } = (0, import_data.useDispatch)(import_block_editor.store);
  const { getBlock, getBlockRootClientId, getBlockIndex, getBlockName } = (0, import_data.useSelect)(import_block_editor.store);
  const propsRef = (0, import_element.useRef)(props);
  propsRef.current = props;
  const outdentListItem = (0, import_use_outdent_list_item.default)();
  return (0, import_compose.useRefEffect)((element) => {
    function onKeyDown(event) {
      if (event.defaultPrevented || event.keyCode !== import_keycodes.ENTER) {
        return;
      }
      const { content, clientId } = propsRef.current;
      if (content.length) {
        return;
      }
      event.preventDefault();
      const canOutdent = getBlockName(
        getBlockRootClientId(
          getBlockRootClientId(propsRef.current.clientId)
        )
      ) === "core/list-item";
      if (canOutdent) {
        outdentListItem();
        return;
      }
      const topParentListBlock = getBlock(
        getBlockRootClientId(clientId)
      );
      const blockIndex = getBlockIndex(clientId);
      const head = (0, import_blocks.cloneBlock)({
        ...topParentListBlock,
        innerBlocks: topParentListBlock.innerBlocks.slice(
          0,
          blockIndex
        )
      });
      const middle = (0, import_blocks.createBlock)((0, import_blocks.getDefaultBlockName)());
      const after = [
        ...topParentListBlock.innerBlocks[blockIndex].innerBlocks[0]?.innerBlocks || [],
        ...topParentListBlock.innerBlocks.slice(blockIndex + 1)
      ];
      const tail = after.length ? [
        (0, import_blocks.cloneBlock)({
          ...topParentListBlock,
          innerBlocks: after
        })
      ] : [];
      replaceBlocks(
        topParentListBlock.clientId,
        [head, middle, ...tail],
        1
      );
      selectionChange(middle.clientId);
    }
    return subscribeDelegatedListener(
      element,
      "keydown",
      onKeyDown,
      true
    );
  }, []);
}
//# sourceMappingURL=use-enter.cjs.map
