"use strict";var E=Object.defineProperty;var I=(r,e,t)=>e in r?E(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var o=(r,e,t)=>(I(r,typeof e!="symbol"?e+"":e,t),t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});class w{constructor(e,t){o(this,"x");o(this,"y");o(this,"width");o(this,"height");this.x=this.getXCoord(t.column,e),this.y=this.getYCoord(t.row,e),this.width=e.columns[t.column].width,this.height=e.rows[t.row].height}getXCoord(e,t){let s=0;for(let l=0;l{const{key:t}=e;switch(t){case"Escape":{this.hide();break}case"Enter":this.root.changeCellValues(this.root.selection.selectedCell,{value:this.element.value,displayValue:this.element.value}),this.hide()}});o(this,"handleClickOutside",e=>{const t=e.target;this.element.contains(t)||this.hide()});this.root=e;const t=document.createElement("input");t.classList.add("editor"),this.element=t,this.hide()}hide(){this.element.style.display="none",this.element.classList.add("hide"),this.element.blur(),window.removeEventListener("click",this.handleClickOutside),this.element.removeEventListener("keydown",this.handleKeydown),this.root.focusTable()}show(e){const{height:t,width:s,x:l,y:n}=new w(this.root.config,e),i=this.root.getCell(e);this.element.classList.remove("hide"),this.element.style.top=n-this.root.viewport.top+"px",this.element.style.left=l-this.root.viewport.left+"px",this.element.style.width=s+"px",this.element.style.height=t+"px",this.element.style.display="block",window.addEventListener("click",this.handleClickOutside),this.element.addEventListener("keydown",this.handleKeydown),this.element.value=i.value,this.element.focus(),this.element.select()}}class A{constructor(e){o(this,"element");o(this,"root");this.root=e;const t=document.createElement("header");t.classList.add(),this.element=t}}class B{constructor(e){o(this,"element");o(this,"verticalScroller");o(this,"horizontalScroller");o(this,"root");o(this,"isSelecting",!1);o(this,"handleMouseMove",e=>{if(!this.isSelecting)return;const{offsetX:t,offsetY:s}=e,l=this.root.getCellByCoords(t,s);this.root.selection.selectedRange&&(this.root.selection.selectedRange.to=l),this.root.renderSheet()});o(this,"handleMouseUp",()=>{this.isSelecting=!1,this.root.selection.selectedRange&&this.root.selection.selectedRange.from.row===this.root.selection.selectedRange.to.row&&this.root.selection.selectedRange.from.column===this.root.selection.selectedRange.to.column&&(this.root.selection.selectedRange=null),this.root.renderSheet()});o(this,"handleDoubleClick",e=>{e.preventDefault();const t=this.root.getCellByCoords(e.offsetX,e.offsetY);this.root.showEditor(t)});o(this,"handleKeydown",e=>{if(console.log(e),["ArrowLeft","ArrowRight","ArrowUp","ArrowDown"].includes(e.key))switch(e.preventDefault(),this.root.selection.selectedRange=null,e.key){case"ArrowLeft":{this.root.selection.selectedCell&&this.root.selection.selectedCell.column>0&&(console.log("tick"),this.root.selection.selectedCell.column-=1,this.root.renderSheet());break}case"ArrowRight":{this.root.selection.selectedCell&&this.root.selection.selectedCell.column0&&(this.root.selection.selectedCell.row-=1,this.root.renderSheet());break}case"ArrowDown":{this.root.selection.selectedCell&&this.root.selection.selectedCell.row{if(e.button!==0)return;const{offsetX:t,offsetY:s}=e,l=this.root.getCellByCoords(t,s);this.isSelecting=!0,this.root.selection.selectedRange={from:l,to:l},this.root.selection.selectedCell=l,this.root.renderSheet()});o(this,"handleScroll",()=>{const e=this.getViewportBoundlingRect();this.root.viewport.updateValues(e),this.root.renderSheet()});this.root=e;const{horizontalScroller:t,scroller:s,verticalScroller:l}=this.buildComponent();this.element=s,this.verticalScroller=l,this.horizontalScroller=t,this.element.style.height=this.root.config.view.height+"px",this.element.style.width=this.root.config.view.width+"px",this.element.tabIndex=-1,this.updateScrollerSize(),this.element.addEventListener("scroll",this.handleScroll),this.element.addEventListener("mousedown",this.handleClick),this.element.addEventListener("mousemove",this.handleMouseMove),this.element.addEventListener("mouseup",this.handleMouseUp),this.element.addEventListener("dblclick",this.handleDoubleClick),this.element.addEventListener("keydown",this.handleKeydown)}getViewportBoundlingRect(){const{scrollTop:e,scrollLeft:t}=this.element,{height:s,width:l}=this.element.getBoundingClientRect(),n=e+s,i=t+l;return{top:e,left:t,bottom:n,right:i}}buildComponent(){const e=document.createElement("div"),t=document.createElement("div"),s=document.createElement("div"),l=document.createElement("div"),n=document.createElement("div");return t.style.width="0px",t.style.pointerEvents="none",s.style.pointerEvents="none",l.style.display="flex",n.appendChild(t),n.appendChild(s),l.appendChild(n),this.verticalScroller=t,this.horizontalScroller=s,e.appendChild(l),e.classList.add("scroller"),{scroller:e,verticalScroller:t,horizontalScroller:s}}getActualHeight(){return this.root.config.rows.reduce((e,t)=>(e+=t.height,e),0)}getActualWidth(){return this.root.config.columns.reduce((e,t)=>(e+=t.width,e),0)}updateScrollerSize(){const e=this.getActualHeight(),t=this.getActualWidth();this.setScrollerHeight(e),this.setScrollerWidth(t)}setScrollerHeight(e){this.verticalScroller.style.height=e+"px"}setScrollerWidth(e){this.horizontalScroller.style.width=e+"px"}}class p{constructor(e){o(this,"fontSize",16);o(this,"fontColor","black");o(this,"background","white");o(this,"borderColor","black");o(this,"selectedBackground","#4287f5");o(this,"selectedFontColor","#ffffff");e&&Object.assign(this,e)}}class y{constructor(e,t){o(this,"row");o(this,"column");this.row=e,this.column=t}}class S{constructor(e){o(this,"value");o(this,"displayValue");o(this,"resultValue");o(this,"position");o(this,"style");this.value=e.value,this.displayValue=e.displayValue,this.resultValue=e.resultValue,this.position=e.position,this.style=e.style}}class g{constructor(e){o(this,"value");o(this,"displayValue");o(this,"resultValue");o(this,"position");o(this,"style",new p);this.value=e.value,this.displayValue=e.displayValue,this.resultValue=e.resultValue,this.position=e.position}getSerializableCell(){return new S({displayValue:this.displayValue,position:this.position,resultValue:this.resultValue,style:this.style,value:this.value})}changeValues(e){Object.assign(this,e)}isCellInRange(e){const{column:t,row:s}=this.position,{selectedRange:l}=e.selection;if(!l)return!1;const n=s>=Math.min(l.from.row,l.to.row)&&s<=Math.max(l.to.row,l.from.row);return t>=Math.min(l.from.column,l.to.column)&&t<=Math.max(l.to.column,l.from.column)&&n}render(e){var a;let{height:t,width:s,x:l,y:n}=new w(e.config,this.position);const{ctx:i}=e,c=((a=e.selection.selectedCell)==null?void 0:a.row)===this.position.row&&e.selection.selectedCell.column===this.position.column,h=this.isCellInRange(e);n-=e.viewport.top,l-=e.viewport.left,i.clearRect(l,n,s,t),i.fillStyle=c||h?this.style.selectedBackground:this.style.background,i.strokeStyle="black",i.fillRect(l,n,s-1,t-1),i.strokeRect(l,n,s,t),i.fillStyle=c||h?this.style.selectedFontColor:this.style.fontColor,i.textAlign="left",i.font=`${this.style.fontSize}px Arial`,i.textBaseline="middle",i.fillText(this.displayValue,l+2,n+t/2)}}class D{constructor(e){o(this,"element");o(this,"ctx");o(this,"root");this.root=e;const t=document.createElement("canvas");t.classList.add("sheet"),t.height=this.root.config.view.height,t.width=this.root.config.view.width,t.style.width=this.root.config.view.width+"px",t.style.height=this.root.config.view.height+"px",this.element=t;const s=this.element.getContext("2d");if(!s)throw new Error("Enable hardware acceleration");this.ctx=s}getCellByCoords(e,t){let s=0,l=0;for(;l<=t&&(l+=this.root.config.rows[s].height,!(l>=t));)s++;let n=0,i=0;for(;i<=e&&(i+=this.root.config.columns[n].width,!(i>=e));)n++;return new y(s,n)}renderCell(e){const{column:t,row:s}=e;this.root.data[s][t].render(this.root)}renderSheet(){const e=this.root.viewport.firstRow,t=this.root.viewport.lastCol+3,s=this.root.viewport.lastRow+3,l=this.root.viewport.firstCol;for(let n=e;n<=s;n++)for(let i=l;i<=t&&!(!this.root.config.columns[i]||!this.root.config.rows[n]);i++)this.renderCell({column:i,row:n})}}class z{constructor(e){o(this,"element");o(this,"root");this.root=e;const t=document.createElement("div");t.classList.add("spreadsheet_container"),this.element=t,this.changeElementSizes(this.root.viewProps)}changeElementSizes(e){const{height:t,width:s}=e;this.element.style.width=s+"px",this.element.style.height=t+"px"}}class M{constructor(e){o(this,"element");o(this,"root");this.root=e;const t=document.createElement("div");t.classList.add("toolbar"),this.element=t}}class d{constructor(e){o(this,"rows");o(this,"columns");o(this,"view",{width:800,height:600});this.columns=e.columns,this.rows=e.rows,this.view=e.view}}class v{constructor(){o(this,"selectedCell",null);o(this,"selectedRange",null)}}class x{}class u{constructor(e,t){o(this,"root");o(this,"top");o(this,"left");o(this,"right");o(this,"bottom");o(this,"firstRow");o(this,"lastRow");o(this,"firstCol");o(this,"lastCol");this.root=e,this.top=t.top,this.left=t.left,this.right=t.right,this.bottom=t.bottom,this.firstRow=this.getFirstRow(),this.lastCol=this.getFirstRow();//!Temp this.firstCol=this.getFirstRow();//!Temp this.lastRow=this.getLastRow(),this.updateValues({top:0,left:0,right:this.root.viewProps.width,bottom:this.root.viewProps.height})}updateValues(e){this.top=e.top,this.left=e.left,this.right=e.right,this.bottom=e.bottom,this.firstRow=this.getFirstRow(),this.lastRow=this.getLastRow(),this.firstCol=this.getFirstCol(),this.lastCol=this.getLastCol()}getFirstRow(){return this.root.cache.getRowByYCoord(this.top)}getLastRow(){return this.root.cache.getRowByYCoord(this.bottom)}getFirstCol(){return this.root.cache.getColumnByXCoord(this.left)}getLastCol(){return this.root.cache.getColumnByXCoord(this.right)}}class m{constructor(e){o(this,"width");o(this,"title");this.width=e.width,this.title=e.title}}class f{constructor(e){o(this,"height");o(this,"title");this.height=e.height,this.title=e.title}}function C(r,e,t=!1){const s=[];for(let l=0;l<=r;l++){const n=[];for(let i=0;i<=e;i++){const c=t?`${l}:${i}`:"",h=new g({displayValue:c,resultValue:c,value:c,position:{column:i,row:l}});n.push(h)}s.push(n)}return s}function R(r,e){const t=[];for(let n=0;n<=r;n++){const i=new f({height:40,title:String(n)});t.push(i)}const s=[];for(let n=0;n<=e;n++){const i=new m({title:String(n),width:150});s.push(i)}return new d({columns:s,rows:t,view:{height:600,width:800}})}function T(r,e){const t=C(r,e),s=R(r,e);return{data:t,config:s}}class b{constructor(e){o(this,"xPos");o(this,"colIdx");this.xPos=e.xPos,this.colIdx=e.colIdx}}class k{constructor(e){o(this,"yPos");o(this,"rowIdx");this.yPos=e.yPos,this.rowIdx=e.rowIdx}}class V{constructor(e){o(this,"columns");o(this,"rows");this.columns=e.columns,this.rows=e.rows}getRowByYCoord(e){let t=0;for(let s=0;s{this.changeCellValues(e.position,{displayValue:"",resultValue:"",value:""})});else{if(!this.selection.selectedCell)return;this.changeCellValues(this.selection.selectedCell,{displayValue:"",resultValue:"",value:""})}}showEditor(e){this.editor.show(e)}renderSheet(){this.sheet.renderSheet()}renderCell(e,t){this.data[e][t].render(this)}loadData(e){const t=e.length,s=e[0]?this.data[0].length:0;this.data=[];const l=[];for(let n=0;n