"use strict";const a=require("@synnaxlabs/x"),d=require("react"),v=require("./Editable-HUPqTaui.cjs"),g=require("./Text-XHBXXDRS.cjs"),x=({triggers:n,callback:u,region:i,loose:r,double:t,regionMustBeElement:l})=>{const{listen:e}=g.useContext();let f;n!=null&&n?.length>0&&typeof n[0]=="string"?f=[n]:f=n;const c=g.useMemoCompare(()=>f,([s],[o])=>s==null&&o==null?!0:s==null||o==null?!1:a.compare.primitiveArrays(s.flat(),o.flat())===a.compare.EQUAL,[f]);d.useEffect(()=>{if(!(c==null||c.length===0))return e(s=>{const o=g.filter(c,s.prev,{loose:r,double:t}),q=g.filter(c,s.next,{loose:r,double:t}),b=g.diff(q,o);let h=b[0];const p=b[1];if(h.length===0&&p.length===0)return;h=R(s.target,s.cursor,h,i,l);const m={target:s.target,cursor:s.cursor};h.length>0&&u?.({...m,stage:"start",triggers:h,prevTriggers:s.prev}),p.length>0&&u?.({...m,stage:"end",triggers:p,prevTriggers:s.prev})})},[u,c,e,r,i,t,l])},R=(n,u,i,r,t)=>{if(r==null)return i;if(r.current==null)return[];const l=a.box.construct(r.current);return i.filter(e=>t??e.some(c=>c.includes("Mouse"))?a.box.contains(l,u)&&n===r.current:a.box.contains(l,u))},k=({triggers:n,loose:u})=>{const[i,r]=v.useStateRef({triggers:[],held:!1});return x({triggers:n,callback:d.useCallback(t=>{r(l=>{let e;return t.stage==="start"?e=a.unique.unique([...l.triggers,...t.triggers]):e=g.purge(l.triggers,t.triggers),{triggers:e,held:e.length>0}})},[]),loose:u}),i},H=({triggers:n,loose:u})=>{const[i,r]=d.useState({triggers:[],held:!1});return x({triggers:n,callback:d.useCallback(t=>{r(l=>{let e;return t.stage==="start"?e=a.unique.unique([...l.triggers,...t.triggers]):e=g.purge(l.triggers,t.triggers),{triggers:e,held:e.length>0}})},[]),loose:u}),i};exports.use=x;exports.useHeld=H;exports.useHeldRef=k;
