"use strict";const C=require("./jsx-runtime-nZSsnGb7.cjs"),t=require("@synnaxlabs/x"),T=require("react"),p=require("react-dom"),R=require("./css-CloSmhZB.cjs"),u=require("./Animated-B_N04fe6.cjs"),b=require("./Box-CNUz5BsV.cjs");require("./Copy-BgRUUzWz.cjs");const q=require("./Text-XHBXXDRS.cjs"),M=require("./Editable-HUPqTaui.cjs");require("./types-DI2vtOGs.cjs");require("./Provider-cSwm-2e_.cjs");const m={visible:!1,position:t.xy.ZERO,cursor:t.xy.ZERO,keys:[]},O=R.CSS.BE("menu-context","container"),S=o=>{let e=o;for(;!e.classList.contains(u.CONTEXT_TARGET);){if(e.classList.contains(O)||e.parentElement==null)return o;e=e.parentElement}return e},_=o=>{const e=S(o);if(e==null)return[];const s=Array.from(e.parentElement?.querySelectorAll(`.${u.CONTEXT_SELECTED}`)??[]);return s.includes(e)?s:[e]},N=[{targetCorner:t.location.BOTTOM_RIGHT,dialogCorner:t.location.TOP_LEFT},{targetCorner:t.location.BOTTOM_LEFT,dialogCorner:t.location.TOP_RIGHT},{targetCorner:t.location.TOP_RIGHT,dialogCorner:t.location.BOTTOM_LEFT},{targetCorner:t.location.TOP_LEFT,dialogCorner:t.location.BOTTOM_RIGHT}],j=()=>{const o=T.useRef(null),[e,s]=T.useState(m),E=T.useCallback(n=>{const r=t.xy.construct(n);let x=[];if(typeof n=="object"&&"preventDefault"in n){n.preventDefault(),n.stopPropagation();const d=_(n.target);x=t.unique.unique(d.map(f=>f.id).filter(f=>f.length>0))}s({visible:!0,keys:x,position:r,cursor:r})},[]),i=T.useCallback(()=>{const n=o.current;n!=null&&s(r=>{if(!r.visible)return r;const{adjustedDialog:x}=u.position({container:t.box.construct(0,0,window.innerWidth,window.innerHeight),dialog:t.box.construct(n),target:t.box.construct(r.cursor,0,0),prefer:N}),d=t.box.topLeft(x);return t.xy.equals(r.position,d)?r:{...r,position:d}})},[]),c=u.useResize(i,{enabled:e.visible}),l=M.useCombinedRefs(o,c);u.useWindowResize(i,{enabled:e.visible});const a=()=>s(m);return u.useClickOutside({ref:o,onClickOutside:a}),{...e,close:a,open:E,ref:l,className:O}},y=({ref:o,menu:e,visible:s,open:E,close:i,position:c,keys:l,className:a,cursor:n,style:r,onClick:x,...d})=>s?p.createPortal(C.jsxRuntimeExports.jsx(b.Box$1,{className:R.CSS(u.CONTEXT_MENU_CLASS,R.CSS.bordered(),a),ref:o,style:{...t.xy.css(c),...r},onClick:f=>{i(),x?.(f)},gap:"tiny",...d,children:e?.({keys:l,visible:s,position:c,cursor:n})}),document.body):null,g=({menu:o,children:e,...s})=>C.jsxRuntimeExports.jsxs(C.jsxRuntimeExports.Fragment,{children:[C.jsxRuntimeExports.jsx(y,{menu:o,...s}),e]}),[L,I]=q.create({defaultValue:{onClick:()=>{},selected:""},displayName:"Menu.Context"}),P=({children:o,onChange:e,level:s,gap:E,background:i,value:c=""})=>{const l=T.useCallback(n=>{typeof e=="function"?e(n):e&&n in e&&e[n](n)},[e]),a=T.useMemo(()=>({onClick:l,selected:c,level:s,gap:E,background:i}),[c,l,s,E,i]);return C.jsxRuntimeExports.jsx(L,{value:a,children:o})};exports.ContextMenu=g;exports.Menu=P;exports.useContext=I;exports.useContextMenu=j;
