"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-library/src/list-item/hooks/use-outdent-list-item.js
var use_outdent_list_item_exports = {};
__export(use_outdent_list_item_exports, {
  default: () => useOutdentListItem
});
module.exports = __toCommonJS(use_outdent_list_item_exports);
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_block_editor = require("@wordpress/block-editor");
var import_blocks = require("@wordpress/blocks");
function useOutdentListItem() {
  const registry = (0, import_data.useRegistry)();
  const {
    moveBlocksToPosition,
    removeBlock,
    insertBlock,
    updateBlockListSettings
  } = (0, import_data.useDispatch)(import_block_editor.store);
  const {
    getBlockRootClientId,
    getBlockName,
    getBlockOrder,
    getBlockIndex,
    getSelectedBlockClientIds,
    getBlock,
    getBlockListSettings
  } = (0, import_data.useSelect)(import_block_editor.store);
  function getParentListItemId(id) {
    const listId = getBlockRootClientId(id);
    const parentListItemId = getBlockRootClientId(listId);
    if (!parentListItemId) {
      return;
    }
    if (getBlockName(parentListItemId) !== "core/list-item") {
      return;
    }
    return parentListItemId;
  }
  return (0, import_element.useCallback)((clientIds = getSelectedBlockClientIds()) => {
    if (!Array.isArray(clientIds)) {
      clientIds = [clientIds];
    }
    if (!clientIds.length) {
      return;
    }
    const firstClientId = clientIds[0];
    if (getBlockName(firstClientId) !== "core/list-item") {
      return;
    }
    const parentListItemId = getParentListItemId(firstClientId);
    if (!parentListItemId) {
      return;
    }
    const parentListId = getBlockRootClientId(firstClientId);
    const lastClientId = clientIds[clientIds.length - 1];
    const order = getBlockOrder(parentListId);
    const followingListItems = order.slice(
      getBlockIndex(lastClientId) + 1
    );
    registry.batch(() => {
      if (followingListItems.length) {
        let nestedListId = getBlockOrder(firstClientId)[0];
        if (!nestedListId) {
          const nestedListBlock = (0, import_blocks.cloneBlock)(
            getBlock(parentListId),
            {},
            []
          );
          nestedListId = nestedListBlock.clientId;
          insertBlock(nestedListBlock, 0, firstClientId, false);
          updateBlockListSettings(
            nestedListId,
            getBlockListSettings(parentListId)
          );
        }
        moveBlocksToPosition(
          followingListItems,
          parentListId,
          nestedListId
        );
      }
      moveBlocksToPosition(
        clientIds,
        parentListId,
        getBlockRootClientId(parentListItemId),
        getBlockIndex(parentListItemId) + 1
      );
      if (!getBlockOrder(parentListId).length) {
        const shouldSelectParent = false;
        removeBlock(parentListId, shouldSelectParent);
      }
    });
    return true;
  }, []);
}
//# sourceMappingURL=use-outdent-list-item.js.map
