import { Q as f } from "./index-U54PFR25.js";
import x, { TableSelection as v, findParentBlot as p, blotName as m, randomId as b, TableCellInnerFormat as y, findParentBlots as N } from "./index-C_1M7Ag6.js";
import { b as C, r as S, a as H } from "./TableUpLogical-B6KpNIZF.js";
class V extends x {
  getTableSelectionModule() {
    return this.getModule(v.moduleName);
  }
  resolveSelectedCells(t) {
    var e;
    return t.length > 0 ? t : ((e = this.getTableSelectionModule()) == null ? void 0 : e.selectedTds) || [];
  }
  moveSelectionToCell(t) {
    const e = this.quill.getIndex(t);
    this.quill.focus(), this.quill.setSelection({
      index: e,
      length: 0
    }, f.sources.SILENT);
  }
  clearDisplayedSelection() {
    var t;
    (t = this.getTableSelectionModule()) == null || t.hide();
  }
  resolveSplitTargetCell(t) {
    if (t.length === 1) return t[0];
    const e = this.getTableSelectionModule();
    if ((e == null ? void 0 : e.selectedTds.length) === 1) return e.selectedTds[0];
    const l = this.quill.getSelection();
    if (!l) return null;
    const [s] = this.quill.getLine(l.index);
    if (!s) return null;
    try {
      return p(s, m.tableCellInner);
    } catch {
      return null;
    }
  }
  resolveCellInnerFromNode(t) {
    const e = f.find(t);
    return (e == null ? void 0 : e.getCellInner()) ?? null;
  }
  readElementHeight(t) {
    if (!(t instanceof HTMLElement)) return null;
    const e = t.getBoundingClientRect().height;
    return Number.isFinite(e) && e > 0 ? Math.round(e * 100) / 100 : null;
  }
  applyCellHeight(t, e) {
    e === null || e <= 0 || t.setFormatValue("height", `${Math.round(e * 100) / 100}px`, !0);
  }
  resolveUniqueId(t, e) {
    const l = t == null ? void 0 : t.trim();
    if (l && !e.has(l))
      return e.add(l), l;
    let s = b();
    for (; e.has(s); ) s = b();
    return e.add(s), s;
  }
  normalizeTableStructure(t) {
    const e = t.domNode;
    if (!(e instanceof HTMLTableElement)) return;
    const l = C(e);
    if (!l) return;
    const s = e.dataset.tableId || t.tableId || b();
    e.dataset.tableId = s;
    const u = t.getRows(), a = t.getCols();
    if (u.length < l.rows.length || a.length < l.colIds.length) return;
    const c = /* @__PURE__ */ new Set(), d = l.rows.map((n, i) => {
      var I;
      const o = u[i], h = n.dataset.wrapTag || ((I = n.parentElement) == null ? void 0 : I.tagName.toLowerCase()) || "tbody", w = this.resolveUniqueId(n.dataset.rowId, c);
      return o.setFormatValue("table-id", s), o.setFormatValue("row-id", w), o.setFormatValue("wrap-tag", h), {
        rowId: w,
        wrapTag: h
      };
    }), g = /* @__PURE__ */ new Set(), r = Array.from({
      length: l.colIds.length
    }, (n, i) => {
      const o = a[i], h = this.resolveUniqueId(o == null ? void 0 : o.domNode.dataset.colId, g);
      return o.domNode.dataset.tableId = s, o.domNode.dataset.colId = h, h;
    });
    t.getBodys().forEach((n) => {
      n.domNode.dataset.tableId = s;
    }), t.descendants(y).forEach((n) => {
      n.emptyRow = [];
    }), Array.from(l.placements.values()).forEach((n) => {
      const i = f.find(n.cell), o = i == null ? void 0 : i.getCellInner();
      if (!o) return;
      const h = d[n.rowIndex], w = (h == null ? void 0 : h.rowId) || b(), I = (h == null ? void 0 : h.wrapTag) || "tbody", T = r[n.colIndex] || b();
      o.setFormatValue("table-id", s), o.setFormatValue("row-id", w), o.setFormatValue("col-id", T), o.setFormatValue("rowspan", n.rowspan), o.setFormatValue("colspan", n.colspan), o.setFormatValue("wrap-tag", I), o.setFormatValue("tag", n.cell.tagName.toLowerCase()), o.emptyRow = [];
    }), t.checkEmptyRow(!1), this.quill.update(f.sources.SILENT);
  }
  normalizeTableForCell(t) {
    if (t)
      try {
        const e = p(t, m.tableMain);
        this.normalizeTableStructure(e);
      } catch {
      }
  }
  resolveStableMergeSelection(t) {
    if (t.length <= 1) return null;
    const e = p(t[0], m.tableMain);
    if (!e) return null;
    this.normalizeTableStructure(e);
    const l = e.domNode;
    if (!(l instanceof HTMLTableElement)) return null;
    const s = C(l);
    if (!s) return null;
    const u = t.reduce((r, n) => {
      const i = S(n);
      return i && r.push(i), r;
    }, []), a = H(s, u);
    if (!a) return null;
    const c = a.cells.reduce((r, n) => {
      const i = this.resolveCellInnerFromNode(n);
      return i && r.push(i), r;
    }, []), d = this.resolveCellInnerFromNode(a.baseCell);
    if (!d || c.length !== a.cells.length) return null;
    const g = s.rows.slice(a.rowStartIndex, a.rowEndIndex + 1).reduce((r, n) => {
      const i = this.readElementHeight(n);
      return i === null ? r : (r ?? 0) + i;
    }, null);
    return {
      baseCell: d,
      cells: c,
      colCount: a.colEndIndex - a.colStartIndex + 1,
      rowCount: a.rowEndIndex - a.rowStartIndex + 1,
      totalHeight: g
    };
  }
  mergeStableSelection(t) {
    const {
      baseCell: e,
      cells: l,
      colCount: s,
      rowCount: u
    } = t, a = e.getTableBody();
    let c = e.getTableRow();
    if (!a || !c) return;
    for (let r = 1; r < l.length; r += 1) {
      const n = l[r], i = n.getTableBody();
      if (i && i !== a) {
        const o = n.getTableRow();
        o && (c.parent.insertBefore(o, c.next), c = o);
      }
    }
    a.convertBody(e.wrapTag);
    for (let r = 1; r < l.length; r += 1) {
      const n = l[r];
      n.length() > 1 && n.moveChildren(e), n.parent.remove();
    }
    e.colspan = s, e.rowspan = u;
    const d = this.quill.getIndex(e);
    this.quill.setSelection({
      index: d,
      length: 0
    }, f.sources.SILENT);
    const g = p(e, m.tableMain);
    this.fixTableByRemove(g);
  }
  resolveSplitCellHeights(t) {
    const [e, l] = N(t, [m.tableMain, m.tableRow]), s = e.getRows(), u = s.indexOf(l);
    if (u < 0) return null;
    const a = Array.from({
      length: t.rowspan
    }, (g, r) => {
      const n = s[u + r], i = this.readElementHeight((n == null ? void 0 : n.domNode) ?? null);
      if (i !== null) return i;
      const o = this.readElementHeight(S(t));
      return o !== null && t.rowspan > 0 ? o / t.rowspan : null;
    }), c = t.getColumnIndex(), d = e.getColIds().slice(c, c + t.colspan);
    return d.length === 0 ? null : {
      colIds: d,
      rowHeights: a,
      startRowIndex: u,
      tableBlot: e
    };
  }
  syncSplitCellHeights(t, e, l, s) {
    const u = t.getRows();
    s.forEach((a, c) => {
      if (a === null) return;
      const d = u[e + c];
      d && l.forEach((g) => {
        const r = d.getCellByColId(g, "next"), n = r == null ? void 0 : r.getCellInner();
        (n == null ? void 0 : n.rowId) !== d.rowId || n.colId !== g || this.applyCellHeight(n, a);
      });
    });
  }
  mergeCells(t) {
    const e = this.resolveSelectedCells(t), l = this.resolveStableMergeSelection(e);
    l && (this.clearDisplayedSelection(), this.mergeStableSelection(l), this.applyCellHeight(l.baseCell, l.totalHeight), this.moveSelectionToCell(l.baseCell));
  }
  splitCell(t) {
    const e = this.resolveSplitTargetCell(t);
    if (!e) return;
    this.normalizeTableForCell(e);
    const l = this.resolveSplitCellHeights(e);
    this.clearDisplayedSelection(), super.splitCell([e]), l && this.syncSplitCellHeights(l.tableBlot, l.startRowIndex, l.colIds, l.rowHeights), this.moveSelectionToCell(e);
  }
}
export {
  V as default
};
