"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 fix_exports = {};
__export(fix_exports, {
  fixTable: () => fixTable,
  fixTables: () => fixTables,
  fixTablesKey: () => fixTablesKey
});
module.exports = __toCommonJS(fix_exports);
var import_prosemirror_state = require("prosemirror-state");
var import_tablemap = require("../tablemap.js");
var import_mutations = require("./mutations.js");
var import_queries = require("./queries.js");
const fixTablesKey = new import_prosemirror_state.PluginKey("fix-tables");
function changedDescendants(old, cur, offset, f) {
  const oldSize = old.childCount, curSize = cur.childCount;
  outer: for (let i = 0, j = 0; i < curSize; i++) {
    const child = cur.child(i);
    for (let scan = j, e = Math.min(oldSize, i + 3); scan < e; scan++) {
      if (old.child(scan) === child) {
        j = scan + 1;
        offset += child.nodeSize;
        continue outer;
      }
    }
    f(child, offset);
    if (j < oldSize && old.child(j).sameMarkup(child)) {
      changedDescendants(old.child(j), child, offset + 1, f);
    } else {
      child.nodesBetween(0, child.content.size, f, offset + 1);
    }
    offset += child.nodeSize;
  }
}
function fixTables(state, oldState) {
  let tr;
  const check = (node, pos) => {
    if (node.type.spec.tableRole === "table") {
      tr = fixTable(state, node, pos, tr);
    }
  };
  if (!oldState) {
    state.doc.descendants(check);
  } else if (oldState.doc !== state.doc) {
    changedDescendants(oldState.doc, state.doc, 0, check);
  }
  return tr;
}
function fixTable(state, table, tablePos, tr) {
  const map = import_tablemap.TableMap.get(table);
  if (!map.problems) {
    return tr;
  }
  if (!tr) {
    tr = state.tr;
  }
  const mustAdd = [];
  for (let i = 0; i < map.height; i++) {
    mustAdd.push(0);
  }
  for (let i = 0; i < map.problems.length; i++) {
    const prob = map.problems[i];
    if (prob.type === "collision") {
      const cell = table.nodeAt(prob.pos);
      if (!cell) {
        continue;
      }
      const attrs = cell.attrs;
      for (let j = 0; j < attrs.rowspan; j++) {
        mustAdd[prob.row + j] += prob.n;
      }
      tr.setNodeMarkup(
        tr.mapping.map(tablePos + 1 + prob.pos),
        null,
        (0, import_mutations.removeColSpan)(attrs, attrs.colspan - prob.n, prob.n)
      );
    } else if (prob.type === "missing") {
      mustAdd[prob.row] += prob.n;
    } else if (prob.type === "overlong_rowspan") {
      const cell = table.nodeAt(prob.pos);
      if (!cell) {
        continue;
      }
      tr.setNodeMarkup(tr.mapping.map(tablePos + 1 + prob.pos), null, {
        ...cell.attrs,
        rowspan: cell.attrs.rowspan - prob.n
      });
    } else if (prob.type === "colwidth mismatch") {
      const cell = table.nodeAt(prob.pos);
      if (!cell) {
        continue;
      }
      tr.setNodeMarkup(tr.mapping.map(tablePos + 1 + prob.pos), null, {
        ...cell.attrs,
        colwidth: prob.colwidth
      });
    } else if (prob.type === "zero_sized") {
      const pos = tr.mapping.map(tablePos);
      tr.delete(pos, pos + table.nodeSize);
    }
  }
  let first, last;
  for (let i = 0; i < mustAdd.length; i++) {
    if (mustAdd[i]) {
      if (first === null) {
        first = i;
      }
      last = i;
    }
  }
  for (let i = 0, pos = tablePos + 1; i < map.height; i++) {
    const row = table.child(i);
    const end = pos + row.nodeSize;
    const add = mustAdd[i];
    if (add > 0) {
      let role = "cell";
      if (row.firstChild) {
        role = row.firstChild.type.spec.tableRole;
      }
      const nodes = [];
      for (let j = 0; j < add; j++) {
        const node = (0, import_queries.tableNodeTypes)(state.schema)[role].createAndFill();
        if (node) {
          nodes.push(node);
        }
      }
      const side = (i === 0 || first === i - 1) && last === i ? pos + 1 : end - 1;
      tr.insert(tr.mapping.map(side), nodes);
    }
    pos = end;
  }
  return tr.setMeta(fixTablesKey, { fixTables: true });
}
//# sourceMappingURL=fix.js.map
