"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 offset_normalizing_exports = {};
__export(offset_normalizing_exports, {
  createOffsetNormalizingTransactionPlugin: () => createOffsetNormalizingTransactionPlugin
});
module.exports = __toCommonJS(offset_normalizing_exports);
var import_v1 = require("./../../ricos-schema/legacy/wix/rich_content/v1/index.js");
var import_model = require("./../../tiptap/pm/model.js");
var import_state = require("./../../tiptap/pm/state.js");
var import_wix_rich_content_common = require("./../../wix-rich-content-common/index.js");
var import_commons = require("../list-item/commands/commons.js");
const createOffsetNormalizingTransactionPlugin = () => {
  return new import_state.Plugin({
    key: new import_state.PluginKey("normalize-offsets"),
    appendTransaction: (_, _oldState, newState) => {
      if (newState.doc === _oldState.doc && newState.selection === _oldState.selection) {
        return;
      }
      const tr = newState.tr;
      const listItemsWithVisualIndent = (0, import_commons.getListItemsWithVisibleIndentation)(tr.doc);
      const descendants = [];
      let start = null;
      let end = null;
      listItemsWithVisualIndent.forEach(({ node, pos, visibleIndentation }) => {
        if (visibleIndentation < 0 && node.attrs.offset < 0) {
          tr.setNodeAttribute(
            pos,
            "offset",
            (0, import_wix_rich_content_common.normalizeOffset)(node.attrs.offset - visibleIndentation)
          );
        }
        if (visibleIndentation > import_wix_rich_content_common.MAX_LIST_VISIBLE_INDENTATION) {
          if (node.attrs.offset > 0 && visibleIndentation - node.attrs.offset <= import_wix_rich_content_common.MAX_LIST_VISIBLE_INDENTATION) {
            tr.setNodeAttribute(
              pos,
              "offset",
              (0, import_wix_rich_content_common.normalizeOffset)(node.attrs.offset - visibleIndentation + import_wix_rich_content_common.MAX_LIST_VISIBLE_INDENTATION)
            );
          } else {
            const listItemType = node.firstChild?.type;
            node.forEach((childNode) => {
              childNode.forEach((grandChildNode) => {
                if ((grandChildNode.type.name === import_v1.Node_Type.PARAGRAPH || grandChildNode.type.name === import_v1.Node_Type.HEADING) && listItemType) {
                  descendants.push(listItemType.create(null, grandChildNode));
                }
              });
            });
            if (!start) {
              start = pos;
              end = pos + node.nodeSize;
            }
          }
        }
      });
      if (start && end) {
        const fragment = import_model.Fragment.from(descendants);
        const prevSelection = tr.selection;
        tr.delete(start, end).insert(start + 1, fragment).setSelection(
          import_state.TextSelection.between(
            tr.doc.resolve(prevSelection.from),
            tr.doc.resolve(prevSelection.to)
          )
        );
      }
      return tr;
    }
  });
};
//# sourceMappingURL=offset-normalizing.js.map
