"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);
var sibling_list_node_merging_exports = {};
__export(sibling_list_node_merging_exports, {
  createMergingSiblingListNodesTransactionPlugin: () => createMergingSiblingListNodesTransactionPlugin
});
module.exports = __toCommonJS(sibling_list_node_merging_exports);
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_state = require("./../../tiptap/pm/state.js");
var import_transform = require("./../../tiptap/pm/transform.js");
const createMergingSiblingListNodesTransactionPlugin = () => {
  return new import_state.Plugin({
    key: new import_state.PluginKey("merge-sibling-list-nodes"),
    appendTransaction: (_, _oldState, newState) => {
      if (newState.doc === _oldState.doc) {
        return;
      }
      let tr = newState.tr;
      tr = mergeListNodes(tr);
      tr.doc.descendants((node, pos) => {
        if (node.type.name !== import_v1.Node_Type.LIST_ITEM || node.childCount < 2) {
          return;
        }
        mergeListNodes(tr, node, pos + 1);
      });
      return tr;
    }
  });
};
const mergeListNodes = (tr, node = tr.doc, pos = 0) => {
  const validNodeTypes = [import_v1.Node_Type.BULLETED_LIST, import_v1.Node_Type.CHECKBOX_LIST, import_v1.Node_Type.ORDERED_LIST];
  let prevOffset = null;
  let joinedCount = 0;
  node.forEach((childNode, childPos) => {
    if (!validNodeTypes.includes(childNode.type.name)) {
      prevOffset = null;
      return;
    }
    const shouldBeMerged = prevOffset === childNode.attrs.offset;
    prevOffset = childNode.attrs.offset;
    const newPos = childPos + pos - joinedCount * 2;
    if (shouldBeMerged && (0, import_transform.canJoin)(tr.doc, newPos)) {
      tr.join(newPos);
      joinedCount++;
    }
  });
  return tr;
};
//# sourceMappingURL=sibling-list-node-merging.js.map
