import{o as e}from"./rolldown-runtime-FhOqtrmT.js";import{x as t}from"./vendor-markdown-0Mxgxy0L.js";import{Y as n}from"./vendor-ui-UXCWAcmi.js";import{B as r,G as i,R as a,c as o,o as s,s as c}from"./data-grid-types-DcEKQO3y.js";import{t as l}from"./dist-JYGF-20y.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,validateCell:w,getCellRenderer:T,provideEditor:E,isOutsideClick:D}=e,[O,k]=d.useState(f?n:void 0),A=d.useRef(O??n);A.current=O??n;let[j,M]=d.useState(()=>w===void 0?!0:!(s(n)&&w?.(S,n,A.current)===!1)),N=d.useCallback((e,t)=>{l(j?e:void 0,t)},[j,l]),P=d.useCallback(e=>{if(w!==void 0&&e!==void 0&&s(e)){let t=w(S,e,A.current);t===!1?M(!1):(typeof t==`object`&&(e=t),M(!0))}k(e)},[S,w]),F=d.useRef(!1),I=d.useRef(void 0),L=d.useCallback(()=>{N(O,[0,0]),F.current=!0},[O,N]),R=d.useCallback((e,t)=>{N(e,t??I.current??[0,0]),F.current=!0},[N]),z=d.useCallback(async e=>{let t=!1;e.key===`Escape`?(e.stopPropagation(),e.preventDefault(),I.current=[0,0]):e.key===`Enter`&&!e.shiftKey?(e.stopPropagation(),e.preventDefault(),I.current=[0,1],t=!0):e.key===`Tab`&&(e.stopPropagation(),e.preventDefault(),I.current=[e.shiftKey?-1:1,0],t=!0),window.setTimeout(()=>{!F.current&&I.current!==void 0&&(N(t?O:void 0,I.current),F.current=!0)},0)},[N,O]),B=O??n,[V,H]=d.useMemo(()=>{if(c(n))return[];let e=E?.(n);return e===void 0?[T(n)?.provideEditor?.(n),!1]:[e,!1]},[n,T,E]),{ref:U,style:W}=b(),G=!0,K,q=!0,J;if(V!==void 0){G=V.disablePadding!==!0,q=V.disableStyling!==!0;let e=o(V);e&&(J=V.styleOverride);let n=e?V.editor:V;K=d.createElement(n,{isHighlighted:g,onChange:P,value:B,initialValue:p,onFinishedEditing:R,validatedSelection:s(B)?B.selectionRange:void 0,forceEditMode:f,target:t,imageEditorOverride:m,markdownDivCreateNode:h,isValid:j,theme:y})}J={...J,...W};let Y=document.getElementById(`portal`);if(Y===null)return console.error('Cannot open Data Grid overlay editor, because portal not found.  Please add `<div id="portal" />` as the last child of your `<body>`.'),null;let X=q?`gdg-style`:`gdg-unstyle`;j||(X+=` gdg-invalid`),G&&(X+=` gdg-pad`);let Z=C?.[0]??1,Q=C?.[1]??1;return(0,u.createPortal)(d.createElement(a.Provider,{value:y},d.createElement(i,{style:r(y),className:_,onClickOutside:L,isOutsideClick:D},d.createElement(v,{ref:U,id:x,className:X,style:J,as:H===!0?`label`:void 0,targetX:t.x-Z,targetY:t.y-Q,targetWidth:t.width+Z*2,targetHeight:t.height+Q*2},d.createElement(`div`,{className:`gdg-clip-region`,onKeyDown:z},K)))),Y)};export{x as default};