"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("./index-BQRd8ZzE.cjs"),O=require("./index-BkMSvjha.cjs"),f=require("./TableUpLogical-CuBhx8pi.cjs");function P(s){return f.buildDomTableGrid(s)}function X(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 p(s,t){const e=t??P(s);if(!e)return null;const o=f.buildDomTableGeometry(e);return o?{geometry:o,grid:e}:null}function T(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+.5),e.right-.5),y:Math.min(Math.max(s.y,e.top+.5),e.bottom-.5)}}function B(s,t,e){const o=T(s,e),l=T(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 Y(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 w(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(Y(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 C(s,t,e){const o=t.map(n=>e.cellRects.get(n)).filter(n=>!!n),l=f.resolveViewportRectBounds(o);return l?X(s,l):null}class F extends O.TableSelection{resolveSelectedCellsFromBoundary(t,e,o,l){const n=l?{geometry:l,grid:o??P(t)}:p(t,o);if(!(n!=null&&n.grid))return[];const{geometry:i,grid:r}=n,c=f.resolveRectangularSelection(r,w(e,r,i));if(!c)return[];const a=c.cells.reduce((d,m)=>{const h=I.Quill.find(m);return h&&d.push(h.getCellInner()),d},[]),u=C(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=p(o);if(!l)return[];const{geometry:n,grid:i}=l,r=B(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=R=>{this.dragging=!0;const{button:L,target:b,clientX:g,clientY:x}=R,y=b instanceof HTMLElement?b:null,E=y==null?void 0:y.closest("caption");if(L!==0||E||r.dataset.tableId!==u)return;const q={x:g,y:x},v=p(r);if(!v)return;const{geometry:S,grid:H}=v,M=B(a,q,S);M&&(this.setSelectedTds(this.resolveSelectedCellsFromBoundary(r,M,H,S)),this.selectedTds.length>1&&this.quill.blur(),this.update(),this.autoScroller.updateMousePosition(g,x))},m=()=>{document.body.removeEventListener("mousemove",d,!1),document.body.removeEventListener("mouseup",m,!1),this.autoScroller.stop(),this.dragging=!1,this.clearRecordScrollPosition()};document.body.addEventListener("mousemove",d,!1),document.body.addEventListener("mouseup",m,!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=p(this.table);if(!t){this.hide();return}const e=this.selectedTds.reduce((l,n)=>{const i=f.resolveTableCellNode(n);return i&&l.push(i),l},[]),o=C(this.quill.root,e,t.geometry);if(!o){this.hide();return}this.boundary=o,this.update()}}exports.default=F;
