import { Q as I } from "./index-U54PFR25.js";
import { TableSelection as X } from "./index-C_1M7Ag6.js";
import { d as Y, r as w, b as F, e as G, f as O } from "./TableUpLogical-B6KpNIZF.js";
function C(s) {
  return F(s);
}
function q(s, t) {
  const e = s.getBoundingClientRect(), o = t.right - t.left, l = t.bottom - t.top, n = t.left - e.left + s.scrollLeft, i = t.top - e.top + s.scrollTop;
  return {
    height: l,
    width: o,
    x: n,
    x1: n + o,
    y: i,
    y1: i + l
  };
}
function m(s, t) {
  const e = t ?? C(s);
  if (!e) return null;
  const o = G(e);
  return o ? {
    geometry: o,
    grid: e
  } : null;
}
function S(s, t) {
  const e = t.tableRect;
  return e.right <= e.left || e.bottom <= e.top ? null : {
    x: Math.min(Math.max(s.x, e.left + 0.5), e.right - 0.5),
    y: Math.min(Math.max(s.y, e.top + 0.5), e.bottom - 0.5)
  };
}
function T(s, t, e) {
  const o = S(s, e), l = S(t, e);
  return !o || !l ? null : {
    x: Math.min(o.x, l.x),
    x1: Math.max(o.x, l.x),
    y: Math.min(o.y, l.y),
    y1: Math.max(o.y, l.y)
  };
}
function z(s, t, e) {
  if (e) {
    const n = s.x1 < t.left || s.x > t.right, i = s.y1 < t.top || s.y > t.bottom;
    return !(n || i);
  }
  const o = s.x1 <= t.left || s.x >= t.right, l = s.y1 <= t.top || s.y >= t.bottom;
  return !(o || l);
}
function A(s, t, e) {
  const o = [], l = Array.from(t.placements.values()).map((r, c) => ({
    cell: r.cell,
    index: c
  }));
  let n = s, i = !0;
  for (; i; ) {
    i = !1;
    for (let r = 0; r < l.length; r += 1) {
      const c = l[r], a = e.cellRects.get(c.cell);
      if (!a) continue;
      const u = {
        bottom: Math.floor(a.bottom),
        left: Math.floor(a.left),
        right: Math.floor(a.right),
        top: Math.floor(a.top)
      }, d = {
        x: Math.floor(n.x),
        x1: Math.floor(n.x1),
        y: Math.floor(n.y),
        y1: Math.floor(n.y1)
      };
      if (z(d, u, o.length === 0)) {
        o.push(c), l.splice(r, 1), n = {
          x: Math.min(n.x, u.left),
          x1: Math.max(n.x1, u.right),
          y: Math.min(n.y, u.top),
          y1: Math.max(n.y1, u.bottom)
        }, i = !0;
        break;
      }
    }
  }
  return o.toSorted((r, c) => r.index - c.index).map(({
    cell: r
  }) => r);
}
function B(s, t, e) {
  const o = t.map((n) => e.cellRects.get(n)).filter((n) => !!n), l = O(o);
  return l ? q(s, l) : null;
}
class U extends X {
  resolveSelectedCellsFromBoundary(t, e, o, l) {
    const n = l ? {
      geometry: l,
      grid: o ?? C(t)
    } : m(t, o);
    if (!(n != null && n.grid)) return [];
    const {
      geometry: i,
      grid: r
    } = n, c = Y(r, A(e, r, i));
    if (!c) return [];
    const a = c.cells.reduce((d, f) => {
      const h = I.find(f);
      return h && d.push(h.getCellInner()), d;
    }, []), u = B(this.quill.root, c.cells, i);
    return !u || a.length === 0 ? [] : (this.boundary = u, a);
  }
  computeSelectedTds(t, e) {
    if (!this.table) return [];
    const o = this.table, l = m(o);
    if (!l) return [];
    const {
      geometry: n,
      grid: i
    } = l, r = T(t, e, n);
    return r ? this.resolveSelectedCellsFromBoundary(o, r, i, n) : [];
  }
  tableSelectHandler(t) {
    const {
      button: e,
      target: o,
      clientX: l,
      clientY: n
    } = t, i = o instanceof HTMLElement ? o : null, r = i == null ? void 0 : i.closest("table"), c = i == null ? void 0 : i.closest("caption");
    if (e !== 0 || !r || c) return;
    const a = {
      x: l,
      y: n
    };
    this.setSelectionTable(r);
    const u = r.dataset.tableId;
    this.recordScrollPosition(), this.setSelectedTds(this.computeSelectedTds(a, a)), this.show();
    const d = (P) => {
      this.dragging = !0;
      const {
        button: R,
        target: y,
        clientX: b,
        clientY: g
      } = P, p = y instanceof HTMLElement ? y : null, E = p == null ? void 0 : p.closest("caption");
      if (R !== 0 || E || r.dataset.tableId !== u)
        return;
      const L = {
        x: b,
        y: g
      }, x = m(r);
      if (!x) return;
      const {
        geometry: v,
        grid: H
      } = x, M = T(a, L, v);
      M && (this.setSelectedTds(this.resolveSelectedCellsFromBoundary(r, M, H, v)), this.selectedTds.length > 1 && this.quill.blur(), this.update(), this.autoScroller.updateMousePosition(b, g));
    }, f = () => {
      document.body.removeEventListener("mousemove", d, !1), document.body.removeEventListener("mouseup", f, !1), this.autoScroller.stop(), this.dragging = !1, this.clearRecordScrollPosition();
    };
    document.body.addEventListener("mousemove", d, !1), document.body.addEventListener("mouseup", f, !1);
    const h = r.parentElement;
    h && (this.autoScroller.updateMousePosition(l, n), this.autoScroller.start(h));
  }
  updateWithSelectedTds() {
    if (this.selectedTds.length <= 0) {
      this.hide();
      return;
    }
    if (!this.table) {
      this.hide();
      return;
    }
    const t = m(this.table);
    if (!t) {
      this.hide();
      return;
    }
    const e = this.selectedTds.reduce((l, n) => {
      const i = w(n);
      return i && l.push(i), l;
    }, []), o = B(this.quill.root, e, t.geometry);
    if (!o) {
      this.hide();
      return;
    }
    this.boundary = o, this.update();
  }
}
export {
  U as default
};
