// @bun
var{defineProperty:Z,getOwnPropertyNames:L,getOwnPropertyDescriptor:O}=Object,W=Object.prototype.hasOwnProperty;function k(z){return this[z]}var w=(z)=>{var B=(H??=new WeakMap).get(z),J;if(B)return B;if(B=Z({},"__esModule",{value:!0}),z&&typeof z==="object"||typeof z==="function"){for(var G of L(z))if(!W.call(B,G))Z(B,G,{get:k.bind(z,G),enumerable:!(J=O(z,G))||J.enumerable})}return H.set(z,B),B},H;var R=(z)=>z;function C(z,B){this[z]=R.bind(null,B)}var S=(z,B)=>{for(var J in B)Z(z,J,{get:B[J],enumerable:!0,configurable:!0,set:C.bind(B,J)})};var h=(z,B)=>()=>(z&&(B=z(z=0)),B);var d=import.meta.require;function _(z={}){let{onSelect:B,onClear:J}=z,G=new Set,D={text:"",rects:[],ranges:[],isCollapsed:!0},Q=null;function I(){G.forEach((E)=>E(D))}function K(E){if(!E||E.rangeCount===0)return[];let N=[];for(let U=0;U<E.rangeCount;U++)N.push(E.getRangeAt(U).cloneRange());return N}function V(E){let N=[];for(let U of E){let X=U.getClientRects();for(let Y=0;Y<X.length;Y++)N.push(X[Y])}return N}function $(){let E=window.getSelection();if(!E)return;let N=E.toString(),U=E.isCollapsed,X=K(E),Y=V(X),q=D.text;if(D={text:N,rects:Y,ranges:X,isCollapsed:U},I(),N&&N!==q)B?.(D);else if(!N&&q)J?.()}function v(){if(typeof document>"u"||Q)return;document.addEventListener("selectionchange",$),Q=()=>{document.removeEventListener("selectionchange",$)}}function F(){Q?.(),Q=null}function M(){return D.text}function P(){let E=window.getSelection();if(E)E.removeAllRanges()}function j(E){let N=window.getSelection();if(!N)return;let U=document.createRange();if(E)U.selectNodeContents(E);else U.selectNodeContents(document.body);N.removeAllRanges(),N.addRange(U)}function A(){return D.rects}if(typeof document<"u")v();return{get:()=>D,subscribe:(E)=>{return G.add(E),E(D),()=>{if(G.delete(E),G.size===0)F()}},getText:M,clear:P,selectAll:j,getRects:A}}function y(z){let B=_(),J=new Set,G="";return B.subscribe((D)=>{if(!z||!D.text){if(G)G="",J.forEach((K)=>K(""));return}let Q=window.getSelection();if(!Q||Q.rangeCount===0)return;let I=Q.getRangeAt(0);if(z.contains(I.commonAncestorContainer))G=D.text,J.forEach((K)=>K(D.text));else if(G)G="",J.forEach((K)=>K(""))}),{get:()=>G,subscribe:(D)=>{return J.add(D),D(G),()=>{J.delete(D)}},clear:B.clear,selectAll:()=>z&&B.selectAll(z)}}function u(z){let{onShow:B,onHide:J,minLength:G=1}=z,D=!1,Q=_({onSelect:(I)=>{if(I.text.length>=G&&I.rects.length>0){let K=I.rects[I.rects.length-1],V={x:K.left+K.width/2,y:K.top-10};D=!0,B?.(I.text,V)}},onClear:()=>{if(D)D=!1,J?.()}});return{get:Q.get,subscribe:Q.subscribe,isShowing:()=>D}}function b(){let z=_();async function B(I){try{return await navigator.clipboard.writeText(I),!0}catch{return!1}}async function J(){let I=z.getText();if(!I)return!1;return B(I)}async function G(){let I=z.getText();if(!I)return!1;let K=I.split(`
`).map((V)=>`> ${V}`).join(`
`);return B(K)}async function D(I){let K=z.getText();if(!K)return!1;return B(I+K)}async function Q(I,K){let V=z.getText();if(!V)return!1;return B(I+V+K)}return{get:z.get,subscribe:z.subscribe,copy:J,copyAsMarkdown:G,copyWithPrefix:D,copyWithWrapper:Q,getText:z.getText,clear:z.clear}}export{_ as useTextSelection,u as useSelectionPopup,y as useElementTextSelection,b as useCopySelection};
