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