import{o as e}from"./chunk.DuxOD-Sk.js";import{r as t}from"./emotion-is-prop-valid.esm.DV_OErv-.js";import{a as n}from"./isLength.Dot76Eow.js";import{b as r,i,n as a,t as o,v as s,y as c}from"./click-outside-container.D4IIXuyx.js";import{t as l}from"./dist.CLbdoH5b.js";var u=e(n(),1),d=e(t(),1),f=()=>e=>e.targetX,p=()=>e=>e.targetY,m=()=>e=>e.targetWidth,h=()=>e=>e.targetHeight,g=()=>e=>e.targetY+10,_=()=>e=>Math.max(0,(e.targetHeight-28)/2),v=l(`div`)({name:`DataGridOverlayEditorStyle`,class:`gdg-d19meir1`,propsAsIs:!1,vars:{"d19meir1-0":[p(),`px`],"d19meir1-1":[f(),`px`],"d19meir1-2":[m(),`px`],"d19meir1-3":[h(),`px`],"d19meir1-4":[g(),`px`],"d19meir1-5":[_(),`px`]}});function y(){let[e,t]=d.useState();return[e??void 0,t]}function b(){let[e,t]=y(),[n,r]=d.useState(0),[i,a]=d.useState(!0);return d.useLayoutEffect(()=>{if(e===void 0||!(`IntersectionObserver`in window))return;let t=new IntersectionObserver(e=>{e.length!==0&&a(e[0].isIntersecting)},{threshold:1});return t.observe(e),()=>t.disconnect()},[e]),d.useEffect(()=>{if(i||e===void 0)return;let t,n=()=>{let{right:i}=e.getBoundingClientRect();r(e=>Math.min(e+window.innerWidth-i-10,0)),t=requestAnimationFrame(n)};return t=requestAnimationFrame(n),()=>{t!==void 0&&cancelAnimationFrame(t)}},[e,i]),{ref:t,style:d.useMemo(()=>({transform:`translateX(${n}px)`}),[n])}}var x=e=>{let{target:t,content:n,onFinishEditing:l,forceEditMode:f,initialValue:p,imageEditorOverride:m,markdownDivCreateNode:h,highlight:g,className:_,theme:y,id:x,cell:S,bloom:C,portalElementRef:w,validateCell:T,getCellRenderer:E,provideEditor:D,isOutsideClick:ee,customEventTarget:O,activation:k}=e,[A,j]=d.useState(f?n:void 0),M=d.useRef(A??n);M.current=A??n;let[N,P]=d.useState(()=>T===void 0?!0:!(s(n)&&T?.(S,n,M.current)===!1)),F=d.useCallback((e,t)=>{l(N?e:void 0,t)},[N,l]),I=d.useCallback(e=>{if(T!==void 0&&e!==void 0&&s(e)){let t=T(S,e,M.current);t===!1?P(!1):(typeof t==`object`&&(e=t),P(!0))}j(e)},[S,T]),L=d.useRef(!1),R=d.useRef(void 0),te=d.useCallback(()=>{F(A,[0,0]),L.current=!0},[A,F]),z=d.useCallback((e,t)=>{F(e,t??R.current??[0,0]),L.current=!0},[F]),B=d.useCallback(async e=>{let t=!1;e.key===`Escape`?(e.stopPropagation(),e.preventDefault(),R.current=[0,0]):e.key===`Enter`&&!e.shiftKey?(e.stopPropagation(),e.preventDefault(),R.current=[0,1],t=!0):e.key===`Tab`&&(e.stopPropagation(),e.preventDefault(),R.current=[e.shiftKey?-1:1,0],t=!0),window.setTimeout(()=>{!L.current&&R.current!==void 0&&(F(t?A:void 0,R.current),L.current=!0)},0)},[F,A]),V=A??n,[H,U]=d.useMemo(()=>{if(c(n))return[];let e={...n,location:S,activation:k},t=D?.(e);return t===void 0?[E(n)?.provideEditor?.(e),!1]:[t,!1]},[S,n,E,D,k]),{ref:W,style:G}=b(),K=!0,q,J=!0,Y;if(H!==void 0){K=H.disablePadding!==!0,J=H.disableStyling!==!0;let e=r(H);e&&(Y=H.styleOverride);let n=e?H.editor:H;q=d.createElement(n,{portalElementRef:w,isHighlighted:g,activation:k,onChange:I,value:V,initialValue:p,onFinishedEditing:z,validatedSelection:s(V)?V.selectionRange:void 0,forceEditMode:f,target:t,imageEditorOverride:m,markdownDivCreateNode:h,isValid:N,theme:y})}Y={...Y,...G};let X=w?.current??document.getElementById(`portal`);if(X===null)return console.error('Cannot open Data Grid overlay editor, because portal not found. Please, either provide a portalElementRef or add `<div id="portal" />` as the last child of your `<body>`.'),null;let Z=J?`gdg-style`:`gdg-unstyle`;N||(Z+=` gdg-invalid`),K&&(Z+=` gdg-pad`);let Q=C?.[0]??1,$=C?.[1]??1;return(0,u.createPortal)(d.createElement(a.Provider,{value:y},d.createElement(o,{style:i(y),className:_,onClickOutside:te,isOutsideClick:ee,customEventTarget:O},d.createElement(v,{ref:W,id:x,className:Z,style:Y,as:U===!0?`label`:void 0,targetX:t.x-Q,targetY:t.y-$,targetWidth:t.width+Q*2,targetHeight:t.height+$*2},d.createElement(`div`,{className:`gdg-clip-region`,onKeyDown:B},q)))),X)};export{x as default};