(function(w,y){typeof exports=="object"&&typeof module<"u"?y(exports,require("../../core/plugin/base-plugin"),require("../../core/plugin/expander-column")):typeof define=="function"&&define.amd?define(["exports","../../core/plugin/base-plugin","../../core/plugin/expander-column"],y):(w=typeof globalThis<"u"?globalThis:w||self,y(w.TbwGridPlugin_clipboard={},w.TbwGrid,w.TbwGrid))})(this,(function(w,y,S){"use strict";async function R(C){try{return await navigator.clipboard.writeText(C),!0}catch{const e=document.createElement("textarea");e.value=C,e.style.position="fixed",e.style.opacity="0",e.style.pointerEvents="none",document.body.appendChild(e),e.select();const n=document.execCommand("copy");return document.body.removeChild(e),n}}function b(C,e){const n=e.delimiter??"	",o=e.newline??`
`,a=C.replace(/\r\n/g,`
`).replace(/\r/g,`
`),s=[];let t=[],i="",l=!1;for(let r=0;r<a.length;r++){const c=a[r];c==='"'&&!l?l=!0:c==='"'&&l?a[r+1]==='"'?(i+='"',r++):l=!1:c===n&&!l?(t.push(i),i=""):c===o&&!l?(t.push(i),i="",(t.length>1||t.some(u=>u.trim()!==""))&&s.push(t),t=[]):i+=c}return t.push(i),(t.length>1||t.some(r=>r.trim()!==""))&&s.push(t),s}async function T(){try{return await navigator.clipboard.readText()}catch{return""}}function P(C,e){const{rows:n,target:o,fields:a}=C;if(!o)return;const s=e.rows,t=e.effectiveConfig.columns??[],i=t.map(u=>u.field),l=new Map;t.forEach(u=>{l.set(u.field,u.editable===!0)});const r=[...s],c=o.bounds?o.bounds.endRow:1/0;n.forEach((u,g)=>{const f=o.row+g;if(!(f>c)){if(o.bounds){if(f>=r.length)return}else for(;f>=r.length;){const d={};i.forEach(h=>d[h]=""),r.push(d)}r[f]={...r[f]},u.forEach((d,h)=>{const p=a[h];p&&l.get(p)&&(r[f][p]=d)})}}),e.rows=r}class v extends y.BaseGridPlugin{static dependencies=[{name:"selection",required:!1,reason:"Enables copy/paste of selected cells instead of entire grid"}];name="clipboard";get defaultConfig(){return{includeHeaders:!1,delimiter:"	",newline:`
`,quoteStrings:!1}}lastCopied=null;attach(e){super.attach(e),e.addEventListener("paste",n=>this.#o(n),{signal:this.disconnectSignal})}detach(){this.lastCopied=null}onKeyDown(e){return(e.ctrlKey||e.metaKey)&&e.key==="c"?(this.#n(e.target),!0):!1}#n(e){const n=this.#t(),o=n?.getSelection(),a=this.columns.length-1,s=this.rows.length-1;let t;if(o&&o.ranges.length>0){const{mode:l,ranges:r}=o,c=r[r.length-1];l==="row"?t={startRow:c.from.row,startCol:0,endRow:c.to.row,endCol:a}:t={startRow:c.from.row,startCol:c.from.col,endRow:c.to.row,endCol:c.to.col}}else if(!n)t={startRow:0,startCol:0,endRow:s,endCol:a};else{const l=this.#l(e);if(!l)return;t={startRow:l.row,startCol:l.col,endRow:l.row,endCol:l.col}}const i=this.#e(t);R(i.text).then(()=>{this.lastCopied={text:i.text,timestamp:Date.now()},this.emit("copy",{text:i.text,rowCount:i.rowCount,columnCount:i.columnCount})})}#o(e){const n=e.clipboardData?.getData("text/plain");if(!n)return;e.preventDefault();const o=b(n,this.config),s=this.#t()?.getSelection(),t=s?.ranges?.[0],i=t?.from.row??0,l=t?.from.col??0,c=t&&(s?.mode==="range"||s?.mode==="row")&&(t.from.row!==t.to.row||t.from.col!==t.to.col)?{endRow:t.to.row,endCol:t.to.col}:null,u=c?.endCol??this.columns.length-1,g=this.columns[l],f=g?{row:i,col:l,field:g.field,bounds:c}:null,d=[],h=o[0]?.length??0;for(let x=0;x<h&&l+x<=u;x++){const m=this.columns[l+x];m&&!m.hidden&&d.push(m.field)}const p={rows:o,text:n,target:f,fields:d};this.emit("paste",p),this.#s(p)}#s(e){if(!this.grid)return;const{pasteHandler:n}=this.config;if(n===null)return;(n??P)(e,this.grid)}#t(){try{const e=this.grid?.getPluginByName("selection");if(e)return e}catch{}}#e(e){const{startRow:n,startCol:o,endRow:a,endCol:s}=e,t=Math.min(n,a),i=Math.max(n,a),l=Math.min(o,s),r=Math.max(o,s),c=this.config.delimiter??"	",u=this.config.newline??`
`,g=[],f=this.columns.slice(l,r+1).filter(d=>!S.isUtilityColumn(d));if(this.config.includeHeaders){const d=f.map(h=>h.header||h.field);g.push(d.join(c))}for(let d=t;d<=i;d++){const h=this.rows[d];if(!h)continue;const p=f.map(x=>{const m=h[x.field];return m==null?"":m instanceof Date?m.toISOString():String(m)});g.push(p.join(c))}return{text:g.join(u),rowCount:i-t+1,columnCount:r-l+1}}#l(e){const n=e.closest("[data-field-cache]");if(!n)return null;const o=n.dataset.fieldCache,a=n.dataset.row;if(!o||!a)return null;const s=parseInt(a,10);if(isNaN(s))return null;const t=this.columns.findIndex(i=>i.field===o);return t===-1?null:{row:s,col:t}}async copy(){const n=this.#t()?.getSelection(),o=this.columns.length-1;let a={startRow:0,startCol:0,endRow:this.rows.length-1,endCol:o};if(n&&n.ranges.length>0){const t=n.ranges[n.ranges.length-1];n.mode==="row"?a={startRow:t.from.row,startCol:0,endRow:t.to.row,endCol:o}:a={startRow:t.from.row,startCol:t.from.col,endRow:t.to.row,endCol:t.to.col}}const s=this.#e(a);return await R(s.text),this.lastCopied={text:s.text,timestamp:Date.now()},s.text}async copyRows(e){if(e.length===0)return"";const n=[...e].sort((t,i)=>t-i),o=this.columns.length-1,a={startRow:n[0],startCol:0,endRow:n[n.length-1],endCol:o},s=this.#e(a);return await R(s.text),this.lastCopied={text:s.text,timestamp:Date.now()},s.text}async paste(){const e=await T();return e?b(e,this.config):null}getLastCopied(){return this.lastCopied}}w.ClipboardPlugin=v,w.defaultPasteHandler=P,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=clipboard.umd.js.map
