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