import { TIPTAP_LAYOUT_CELL_TYPE, TIPTAP_LAYOUT_TYPE, generateId } from "./../../ricos-content/index.js";
import { Plugin, PluginKey, TextSelection } from "./../../tiptap/pm/state.js";
import { findWrapping } from "./../../tiptap/pm/transform.js";
import { MAX_COLUMNS, MIN_COL_SPAN } from "./shared-with-viewer.js";
import { balanceLayoutCells } from "./shared.js";
const createCellNormalizingTransactionPlugin = () => {
  return new Plugin({
    key: new PluginKey("normalize-cell"),
    appendTransaction: (_, oldState, newState) => {
      if (newState.doc.eq(oldState.doc) && newState.selection.eq(oldState.selection)) {
        return;
      }
      const tr = newState.tr;
      tr.doc.forEach((node, pos) => {
        if (node.type.name === TIPTAP_LAYOUT_TYPE && node.childCount === 1 && node.firstChild?.childCount === 0) {
          tr.deleteRange(pos, pos + node.nodeSize);
        }
      });
      tr.doc.descendants((node, pos, parent) => {
        if (node.type.name === TIPTAP_LAYOUT_CELL_TYPE && parent?.type.name !== TIPTAP_LAYOUT_TYPE) {
          const $from = tr.doc.resolve(pos);
          const $to = tr.doc.resolve(pos + node.nodeSize);
          const nodeRange = $from.blockRange($to);
          const wrapping = nodeRange && findWrapping(nodeRange, newState.schema.nodes[TIPTAP_LAYOUT_TYPE], {
            id: generateId()
          });
          if (!wrapping) {
            return;
          }
          tr.wrap(nodeRange, wrapping);
        }
        if (node.type.name === TIPTAP_LAYOUT_CELL_TYPE && parent?.type.name === TIPTAP_LAYOUT_TYPE && node.attrs.colSpan === null) {
          const colSpans = [];
          parent.content.forEach((child) => colSpans.push(child.attrs.colSpan ?? 0));
          const totalColSpan = colSpans.reduce((acc, curr) => acc + curr, 0);
          const remainingColumns = MAX_COLUMNS - totalColSpan;
          tr.setNodeAttribute(pos, "colSpan", remainingColumns);
        }
        if (node.type.name === TIPTAP_LAYOUT_CELL_TYPE && parent?.type.name === TIPTAP_LAYOUT_TYPE && node.childCount === 0) {
          const paragraphNode = newState.schema.nodes.PARAGRAPH.create({
            id: generateId()
          });
          const insertPos = pos + 1;
          tr.insert(insertPos, paragraphNode);
          const textPos = insertPos + 1;
          tr.setSelection(TextSelection.create(tr.doc, textPos));
        }
      });
      tr.doc.forEach((node, pos) => {
        if (node.type.name === TIPTAP_LAYOUT_TYPE) {
          const colSpans = [];
          node.content.forEach((child) => colSpans.push(child.attrs.colSpan ?? 0));
          const allColSpansValid = colSpans.every((colSpan) => colSpan >= MIN_COL_SPAN);
          const colSpansSum = colSpans.reduce((acc, curr) => acc + curr, 0);
          if (colSpansSum !== MAX_COLUMNS || !allColSpansValid) {
            balanceLayoutCells(tr, node, pos);
          }
        }
      });
      return tr;
    }
  });
};
export {
  createCellNormalizingTransactionPlugin
};
//# sourceMappingURL=cell-normalizing.js.map
