/*!
Copyright (c) 2026 Lenovo SSG BASD. All rights reserved.
@license Proprietary

This software is the confidential and proprietary property of Lenovo SSG BASD.
You shall not disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement.

--- Build Info ---
Version: 1.1.1
Build Time: 2026/2/26 17:21:00
Build Commit: 81e556ed827e2f043668506dae6fae8dbb9ef395
*/
import{Extension as t,Node as e,callOrReturn as o,getExtensionField as n,mergeAttributes as l,findParentNodeClosestToPos as s}from"../../core/dist/index.js";import{TextSelection as a}from"../../../prosemirror-state/dist/index.js";import{columnResizing as r,tableEditing as i,CellSelection as c,fixTables as d,goToNextCell as h,setCellAttr as p,mergeCells as u,splitCell as b,toggleHeaderCell as m,toggleHeader as f,deleteTable as g,deleteRow as w,addRowAfter as y,addRowBefore as M,deleteColumn as C,addColumnAfter as T,addColumnBefore as H}from"../../../prosemirror-tables/dist/index.js";var A=e.create({name:"tableCell",addOptions:()=>({HTMLAttributes:{}}),content:"block+",addAttributes:()=>({colspan:{default:1},rowspan:{default:1},colwidth:{default:null,parseHTML:t=>{var e,o;const n=t.getAttribute("colwidth"),l=n?n.split(",").map(t=>parseInt(t,10)):null;if(!l){const n=null==(e=t.closest("table"))?void 0:e.querySelectorAll("colgroup > col"),l=Array.from((null==(o=t.parentElement)?void 0:o.children)||[]).indexOf(t);if(l&&l>-1&&n&&n[l]){const t=n[l].getAttribute("width");return t?[parseInt(t,10)]:null}}return l}}}),tableRole:"cell",isolating:!0,parseHTML:()=>[{tag:"td"}],renderHTML({HTMLAttributes:t}){return["td",l(this.options.HTMLAttributes,t),0]}}),x=e.create({name:"tableHeader",addOptions:()=>({HTMLAttributes:{}}),content:"block+",addAttributes:()=>({colspan:{default:1},rowspan:{default:1},colwidth:{default:null,parseHTML:t=>{const e=t.getAttribute("colwidth");return e?e.split(",").map(t=>parseInt(t,10)):null}}}),tableRole:"header_cell",isolating:!0,parseHTML:()=>[{tag:"th"}],renderHTML({HTMLAttributes:t}){return["th",l(this.options.HTMLAttributes,t),0]}}),L=e.create({name:"tableRow",addOptions:()=>({HTMLAttributes:{}}),content:"(tableCell | tableHeader)*",tableRole:"row",parseHTML:()=>[{tag:"tr"}],renderHTML({HTMLAttributes:t}){return["tr",l(this.options.HTMLAttributes,t),0]}});function R(t,e){return e?["width",`${Math.max(e,t)}px`]:["min-width",`${t}px`]}function W(t,e,o,n,l,s){var a;let r=0,i=!0,c=e.firstChild;const d=t.firstChild;if(null!==d)for(let p=0,u=0;p<d.childCount;p+=1){const{colspan:t,colwidth:o}=d.child(p).attrs;for(let a=0;a<t;a+=1,u+=1){const t=l===u?s:o&&o[a],d=t?`${t}px`:"";if(r+=t||n,t||(i=!1),c){if(c.style.width!==d){const[e,o]=R(n,t);c.style.setProperty(e,o)}c=c.nextSibling}else{const o=document.createElement("col"),[l,s]=R(n,t);o.style.setProperty(l,s),e.appendChild(o)}}}for(;c;){const t=c.nextSibling;null==(a=c.parentNode)||a.removeChild(c),c=t}const h=t.attrs.style&&"string"==typeof t.attrs.style&&/\bwidth\s*:/i.test(t.attrs.style);i&&!h?(o.style.width=`${r}px`,o.style.minWidth=""):(o.style.width="",o.style.minWidth=`${r}px`)}var v=class{constructor(t,e){this.node=t,this.cellMinWidth=e,this.dom=document.createElement("div"),this.dom.className="tableWrapper",this.table=this.dom.appendChild(document.createElement("table")),t.attrs.style&&(this.table.style.cssText=t.attrs.style),this.colgroup=this.table.appendChild(document.createElement("colgroup")),W(t,this.colgroup,this.table,e),this.contentDOM=this.table.appendChild(document.createElement("tbody"))}update(t){return t.type===this.node.type&&(this.node=t,W(t,this.colgroup,this.table,this.cellMinWidth),!0)}ignoreMutation(t){const e=t.target,o=this.dom.contains(e),n=this.contentDOM.contains(e);return!(!o||n||"attributes"!==t.type&&"childList"!==t.type&&"characterData"!==t.type)}};function N(t,e,o,n){let l=0,s=!0;const a=[],r=t.firstChild;if(!r)return{};for(let d=0,h=0;d<r.childCount;d+=1){const{colspan:t,colwidth:i}=r.child(d).attrs;for(let r=0;r<t;r+=1,h+=1){const t=o===h?n:i&&i[r];l+=t||e,t||(s=!1);const[c,d]=R(e,t);a.push(["col",{style:`${c}: ${d}`}])}}const i=s?`${l}px`:"",c=s?"":`${l}px`;return{colgroup:["colgroup",{},...a],tableWidth:i,tableMinWidth:c}}function E(t,e){return t.createAndFill()}function S(t,e,o,n,l){const s=function(t){if(t.cached.tableNodeTypes)return t.cached.tableNodeTypes;const e={};return Object.keys(t.nodes).forEach(o=>{const n=t.nodes[o];n.spec.tableRole&&(e[n.spec.tableRole]=n)}),t.cached.tableNodeTypes=e,e}(t),a=[],r=[];for(let c=0;c<o;c+=1){const t=E(s.cell);if(t&&r.push(t),n){const t=E(s.header_cell);t&&a.push(t)}}const i=[];for(let c=0;c<e;c+=1)i.push(s.row.createChecked(null,n&&0===c?a:r));return s.table.createChecked(null,i)}var $=({editor:t})=>{const{selection:e}=t.state;if(!(e instanceof c))return!1;let o=0;const n=s(e.ranges[0].$from,t=>"table"===t.type.name);null==n||n.node.descendants(t=>{if("table"===t.type.name)return!1;["tableCell","tableHeader"].includes(t.type.name)&&(o+=1)});return o===e.ranges.length&&(t.commands.deleteTable(),!0)},k="";function O(t,e,o={}){var n;const l=null!=(n=o.cellLineSeparator)?n:"";if(!t||!t.content||0===t.content.length)return"";const s=[];t.content.forEach(t=>{const o=[];t.content&&t.content.forEach(t=>{let n="";if(t.content&&Array.isArray(t.content)&&t.content.length>1){n=t.content.map(t=>e.renderChildren(t)).join(l)}else n=t.content?e.renderChildren(t.content):"";const s=(n||"").replace(/\s+/g," ").trim();const a="tableHeader"===t.type;o.push({text:s,isHeader:a})}),s.push(o)});const a=s.reduce((t,e)=>Math.max(t,e.length),0);if(0===a)return"";const r=new Array(a).fill(0);s.forEach(t=>{var e;for(let o=0;o<a;o+=1){const n=((null==(e=t[o])?void 0:e.text)||"").length;n>r[o]&&(r[o]=n),r[o]<3&&(r[o]=3)}});const i=(t,e)=>t+" ".repeat(Math.max(0,e-t.length)),c=s[0],d=c.some(t=>t.isHeader);let h="\n";const p=new Array(a).fill(0).map((t,e)=>d&&c[e]&&c[e].text||"");h+=`| ${p.map((t,e)=>i(t,r[e])).join(" | ")} |\n`,h+=`| ${r.map(t=>"-".repeat(Math.max(3,t))).join(" | ")} |\n`;return(d?s.slice(1):s).forEach(t=>{h+=`| ${new Array(a).fill(0).map((e,o)=>i(t[o]&&t[o].text||"",r[o])).join(" | ")} |\n`}),h}var j=O,I=e.create({name:"table",addOptions:()=>({HTMLAttributes:{},resizable:!1,renderWrapper:!1,handleWidth:5,cellMinWidth:25,View:v,lastColumnResizable:!0,allowTableNodeSelection:!1}),content:"tableRow+",tableRole:"table",isolating:!0,group:"block",parseHTML:()=>[{tag:"table"}],renderHTML({node:t,HTMLAttributes:e}){const{colgroup:o,tableWidth:n,tableMinWidth:s}=N(t,this.options.cellMinWidth),a=e.style;const r=["table",l(this.options.HTMLAttributes,e,{style:a||(n?`width: ${n}`:`min-width: ${s}`)}),o,["tbody",0]];return this.options.renderWrapper?["div",{class:"tableWrapper"},r]:r},parseMarkdown:(t,e)=>{const o=[];if(t.header){const n=[];t.header.forEach(t=>{n.push(e.createNode("tableHeader",{},[{type:"paragraph",content:e.parseInline(t.tokens)}]))}),o.push(e.createNode("tableRow",{},n))}return t.rows&&t.rows.forEach(t=>{const n=[];t.forEach(t=>{n.push(e.createNode("tableCell",{},[{type:"paragraph",content:e.parseInline(t.tokens)}]))}),o.push(e.createNode("tableRow",{},n))}),e.createNode("table",void 0,o)},renderMarkdown:(t,e)=>j(t,e),addCommands:()=>({insertTable:({rows:t=3,cols:e=3,withHeaderRow:o=!0}={})=>({tr:n,dispatch:l,editor:s})=>{const r=S(s.schema,t,e,o);if(l){const t=n.selection.from+1;n.replaceSelectionWith(r).scrollIntoView().setSelection(a.near(n.doc.resolve(t)))}return!0},addColumnBefore:()=>({state:t,dispatch:e})=>H(t,e),addColumnAfter:()=>({state:t,dispatch:e})=>T(t,e),deleteColumn:()=>({state:t,dispatch:e})=>C(t,e),addRowBefore:()=>({state:t,dispatch:e})=>M(t,e),addRowAfter:()=>({state:t,dispatch:e})=>y(t,e),deleteRow:()=>({state:t,dispatch:e})=>w(t,e),deleteTable:()=>({state:t,dispatch:e})=>g(t,e),mergeCells:()=>({state:t,dispatch:e})=>u(t,e),splitCell:()=>({state:t,dispatch:e})=>b(t,e),toggleHeaderColumn:()=>({state:t,dispatch:e})=>f("column")(t,e),toggleHeaderRow:()=>({state:t,dispatch:e})=>f("row")(t,e),toggleHeaderCell:()=>({state:t,dispatch:e})=>m(t,e),mergeOrSplit:()=>({state:t,dispatch:e})=>!!u(t,e)||b(t,e),setCellAttribute:(t,e)=>({state:o,dispatch:n})=>p(t,e)(o,n),goToNextCell:()=>({state:t,dispatch:e})=>h(1)(t,e),goToPreviousCell:()=>({state:t,dispatch:e})=>h(-1)(t,e),fixTables:()=>({state:t,dispatch:e})=>(e&&d(t),!0),setCellSelection:t=>({tr:e,dispatch:o})=>{if(o){const o=c.create(e.doc,t.anchorCell,t.headCell);e.setSelection(o)}return!0}}),addKeyboardShortcuts(){return{Tab:()=>!!this.editor.commands.goToNextCell()||!!this.editor.can().addRowAfter()&&this.editor.chain().addRowAfter().goToNextCell().run(),"Shift-Tab":()=>this.editor.commands.goToPreviousCell(),Backspace:$,"Mod-Backspace":$,Delete:$,"Mod-Delete":$}},addProseMirrorPlugins(){return[...this.options.resizable&&this.editor.isEditable?[r({handleWidth:this.options.handleWidth,cellMinWidth:this.options.cellMinWidth,defaultCellMinWidth:this.options.cellMinWidth,View:this.options.View,lastColumnResizable:this.options.lastColumnResizable})]:[],i({allowTableNodeSelection:this.options.allowTableNodeSelection})]},extendNodeSchema(t){const e={name:t.name,options:t.options,storage:t.storage};return{tableRole:o(n(t,"tableRole",e))}}}),P=t.create({name:"tableKit",addExtensions(){const t=[];return!1!==this.options.table&&t.push(I.configure(this.options.table)),!1!==this.options.tableCell&&t.push(A.configure(this.options.tableCell)),!1!==this.options.tableHeader&&t.push(x.configure(this.options.tableHeader)),!1!==this.options.tableRow&&t.push(L.configure(this.options.tableRow)),t}});export{k as DEFAULT_CELL_LINE_SEPARATOR,I as Table,A as TableCell,x as TableHeader,P as TableKit,L as TableRow,v as TableView,N as createColGroup,S as createTable,O as renderTableToMarkdown,W as updateColumns};