(function(p,g){typeof exports=="object"&&typeof module<"u"?g(exports,require("../../core/plugin/base-plugin")):typeof define=="function"&&define.amd?define(["exports","../../core/plugin/base-plugin"],g):(p=typeof globalThis<"u"?globalThis:p||self,g(p.TbwGridPlugin_clipboard={},p.TbwGrid))})(this,(function(p,g){"use strict";function R(f,e,t,n){if(n.processCell)return n.processCell(f,e,t);if(f==null)return"";if(f instanceof Date)return f.toISOString();if(typeof f=="object")return JSON.stringify(f);const s=String(f),r=n.delimiter??"	",o=n.newline??`
`;return n.quoteStrings||s.includes(r)||s.includes(o)||s.includes('"')?`"${s.replace(/"/g,'""')}"`:s}function y(f){const{rows:e,columns:t,selectedIndices:n,config:s}=f,r=s.delimiter??"	",o=s.newline??`
`,l=t.filter(i=>!i.hidden&&!i.field.startsWith("__")),c=[];if(s.includeHeaders){const i=l.map(d=>{const h=d.header||d.field;return h.includes(r)||h.includes(o)||h.includes('"')?`"${h.replace(/"/g,'""')}"`:h});c.push(i.join(r))}const u=[...n instanceof Set?[...n]:n].sort((i,d)=>i-d);for(const i of u){const d=e[i];if(!d)continue;const h=l.map(m=>R(d[m.field],m.field,d,s));c.push(h.join(r))}return c.join(o)}async function x(f){try{return await navigator.clipboard.writeText(f),!0}catch{const e=document.createElement("textarea");e.value=f,e.style.position="fixed",e.style.opacity="0",e.style.pointerEvents="none",document.body.appendChild(e),e.select();const t=document.execCommand("copy");return document.body.removeChild(e),t}}function S(f,e){const t=e.delimiter??"	",n=e.newline??`
`,s=f.replace(/\r\n/g,`
`).replace(/\r/g,`
`),r=[];let o=[],l="",c=!1;for(let a=0;a<s.length;a++){const u=s[a];u==='"'&&!c?c=!0:u==='"'&&c?s[a+1]==='"'?(l+='"',a++):c=!1:u===t&&!c?(o.push(l),l=""):u===n&&!c?(o.push(l),l="",(o.length>1||o.some(i=>i.trim()!==""))&&r.push(o),o=[]):l+=u}return o.push(l),(o.length>1||o.some(a=>a.trim()!==""))&&r.push(o),r}async function b(){try{return await navigator.clipboard.readText()}catch{return""}}class T extends g.BaseGridPlugin{name="clipboard";version="1.0.0";get defaultConfig(){return{includeHeaders:!1,delimiter:"	",newline:`
`,quoteStrings:!1}}lastCopied=null;detach(){this.lastCopied=null}onKeyDown(e){const t=(e.ctrlKey||e.metaKey)&&e.key==="c",n=(e.ctrlKey||e.metaKey)&&e.key==="v";return t?(this.#t(e.target),!0):n?(this.#n(),!0):!1}#t(e){const t=this.#e(),n=t?.getSelectedRows()??[],s=n.length>0,r=t?.getRanges()??[],o=r.length>0,l=t?.getSelectedCell()!=null;let c,a,u;if(s&&t)c=y({rows:this.rows,columns:[...this.columns],selectedIndices:n,config:this.config}),a=n.length,u=this.columns.filter(i=>!i.hidden&&!i.field.startsWith("__")).length;else if(o&&t){const i=r[r.length-1],d=this.#s({startRow:i.from.row,startCol:i.from.col,endRow:i.to.row,endCol:i.to.col});c=d.text,a=d.rowCount,u=d.columnCount}else if(l&&t){const i=t.getSelectedCell(),d=this.#i(i.row,i.col);if(!d)return;c=d.text,a=1,u=1}else{const i=this.#o(e);if(!i)return;c=i.text,a=1,u=1}x(c).then(()=>{this.lastCopied={text:c,timestamp:Date.now()},this.emit("copy",{text:c,rowCount:a,columnCount:u})})}#n(){b().then(e=>{if(!e)return;const t=S(e,this.config);this.emit("paste",{rows:t,text:e})})}#e(){try{const e=this.grid;if(e?._plugins){for(const t of e._plugins)if(t.name==="selection")return t}}catch{}}#i(e,t){const n=this.rows[e];if(!n)return null;const s=this.columns[t];if(!s)return null;const r=n[s.field],o=s.header||s.field;let l;if(this.config.includeHeaders){const c=r==null?"":String(r);l=`${o}: ${c}`}else l=r==null?"":String(r);return{text:l}}#s(e){const{startRow:t,startCol:n,endRow:s,endCol:r}=e,o=Math.min(t,s),l=Math.max(t,s),c=Math.min(n,r),a=Math.max(n,r),u=this.config.delimiter??"	",i=this.config.newline??`
`,d=[],h=this.columns.slice(c,a+1);if(this.config.includeHeaders){const m=h.map(w=>w.header||w.field);d.push(m.join(u))}for(let m=o;m<=l;m++){const w=this.rows[m];if(!w)continue;const P=h.map(I=>{const C=w[I.field];return C==null?"":C instanceof Date?C.toISOString():String(C)});d.push(P.join(u))}return{text:d.join(i),rowCount:l-o+1,columnCount:a-c+1}}#o(e){const t=e.closest("[data-field-cache]");if(!t)return null;const n=t.dataset.fieldCache;if(!n)return null;const s=t.dataset.row;if(!s)return null;const r=parseInt(s,10);if(isNaN(r))return null;const o=this.rows[r];if(!o)return null;const l=o[n],a=this.columns.find(i=>i.field===n)?.header||n;let u;if(this.config.includeHeaders){const i=l==null?"":String(l);u=`${a}: ${i}`}else u=l==null?"":String(l);return{text:u,field:n,value:l}}async copy(){const t=this.#e()?.getSelectedRows()??[],n=y({rows:this.rows,columns:[...this.columns],selectedIndices:t,config:this.config});return await x(n),this.lastCopied={text:n,timestamp:Date.now()},n}async copyRows(e){const t=y({rows:this.rows,columns:[...this.columns],selectedIndices:e,config:this.config});return await x(t),this.lastCopied={text:t,timestamp:Date.now()},t}async paste(){const e=await b();return e?S(e,this.config):null}getLastCopied(){return this.lastCopied}}p.ClipboardPlugin=T,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));
//# sourceMappingURL=clipboard.umd.js.map
