import c from "../../../../quill-delta@5.1.0/node_modules/quill-delta/dist/Delta.mjs";
import l from "../core/quill.mjs";
import h from "../core/module.mjs";
import { TableCell as u, TableRow as f, TableBody as b, TableContainer as a, tableId as d } from "../formats/table.mjs";
class w extends h {
  static register() {
    l.register(u), l.register(f), l.register(b), l.register(a);
  }
  constructor() {
    super(...arguments), this.listenBalanceCells();
  }
  balanceTables() {
    this.quill.scroll.descendants(a).forEach((t) => {
      t.balanceCells();
    });
  }
  deleteColumn() {
    const [t, , e] = this.getTable();
    e != null && (t.deleteColumn(e.cellOffset()), this.quill.update(l.sources.USER));
  }
  deleteRow() {
    const [, t] = this.getTable();
    t != null && (t.remove(), this.quill.update(l.sources.USER));
  }
  deleteTable() {
    const [t] = this.getTable();
    if (t == null)
      return;
    const e = t.offset();
    t.remove(), this.quill.update(l.sources.USER), this.quill.setSelection(e, l.sources.SILENT);
  }
  getTable() {
    let t = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : this.quill.getSelection();
    if (t == null)
      return [null, null, null, -1];
    const [e, s] = this.quill.getLine(t.index);
    if (e == null || e.statics.blotName !== u.blotName)
      return [null, null, null, -1];
    const n = e.parent;
    return [n.parent.parent, n, e, s];
  }
  insertColumn(t) {
    const e = this.quill.getSelection();
    if (!e)
      return;
    const [s, n, i] = this.getTable(e);
    if (i == null)
      return;
    const r = i.cellOffset();
    s.insertColumn(r + t), this.quill.update(l.sources.USER);
    let o = n.rowOffset();
    t === 0 && (o += 1), this.quill.setSelection(e.index + o, e.length, l.sources.SILENT);
  }
  insertColumnLeft() {
    this.insertColumn(0);
  }
  insertColumnRight() {
    this.insertColumn(1);
  }
  insertRow(t) {
    const e = this.quill.getSelection();
    if (!e)
      return;
    const [s, n, i] = this.getTable(e);
    if (i == null)
      return;
    const r = n.rowOffset();
    s.insertRow(r + t), this.quill.update(l.sources.USER), t > 0 ? this.quill.setSelection(e, l.sources.SILENT) : this.quill.setSelection(e.index + n.children.length, e.length, l.sources.SILENT);
  }
  insertRowAbove() {
    this.insertRow(0);
  }
  insertRowBelow() {
    this.insertRow(1);
  }
  insertTable(t, e) {
    const s = this.quill.getSelection();
    if (s == null)
      return;
    const n = new Array(t).fill(0).reduce((i) => {
      const r = new Array(e).fill(`
`).join("");
      return i.insert(r, {
        table: d()
      });
    }, new c().retain(s.index));
    this.quill.updateContents(n, l.sources.USER), this.quill.setSelection(s.index, l.sources.SILENT), this.balanceTables();
  }
  listenBalanceCells() {
    this.quill.on(l.events.SCROLL_OPTIMIZE, (t) => {
      t.some((e) => ["TD", "TR", "TBODY", "TABLE"].includes(e.target.tagName) ? (this.quill.once(l.events.TEXT_CHANGE, (s, n, i) => {
        i === l.sources.USER && this.balanceTables();
      }), !0) : !1);
    });
  }
}
export {
  w as default
};
