"use strict";var t=require("react"),e=require("../../../@codemirror/state/dist/index.js"),r=require("../../../@codemirror/view/dist/index.js"),i=require("./getDefaultExtensions.js"),n=require("./utils.js"),o=require("./timeoutLatch.js"),a=e.Annotation.define(),s=[];exports.ExternalChange=a,exports.useCodeMirror=function(u){var{value:c,selection:d,onChange:l,onStatistics:f,onCreateEditor:h,onUpdate:g,extensions:m=s,autoFocus:S,theme:E="light",height:p=null,minHeight:v=null,maxHeight:x=null,width:w=null,minWidth:b=null,maxWidth:j=null,placeholder:q="",editable:y=!0,readOnly:C=!1,indentWithTab:W=!0,basicSetup:L=!0,root:V,initialState:H}=u,[D,O]=t.useState(),[T,A]=t.useState(),[F,J]=t.useState(),M=t.useState(()=>({current:null}))[0],N=t.useState(()=>({current:null}))[0],U=r.EditorView.theme({"&":{height:p,minHeight:v,maxHeight:x,width:w,minWidth:b,maxWidth:j},"& .cm-scroller":{height:"100% !important"}}),k=[r.EditorView.updateListener.of(t=>{if(t.docChanged&&"function"==typeof l&&!t.transactions.some(t=>t.annotation(a))){M.current?M.current.reset():(M.current=new o.TimeoutLatch(()=>{if(N.current){var t=N.current;N.current=null,t()}M.current=null},200),o.getScheduler().add(M.current));var e=t.state.doc.toString();l(e,t)}f&&f(n.getStatistics(t))}),U,...i.getDefaultExtensions({theme:E,editable:y,readOnly:C,placeholder:q,indentWithTab:W,basicSetup:L})];return g&&"function"==typeof g&&k.push(r.EditorView.updateListener.of(g)),k=k.concat(m),t.useLayoutEffect(()=>{if(D&&!F){var t={doc:c,selection:d,extensions:k},i=H?e.EditorState.fromJSON(H.json,t,H.fields):e.EditorState.create(t);if(J(i),!T){var n=new r.EditorView({state:i,parent:D,root:V});A(n),h&&h(n,i)}}return()=>{T&&(J(void 0),A(void 0))}},[D,F]),t.useEffect(()=>{u.container&&O(u.container)},[u.container]),t.useEffect(()=>()=>{T&&(T.destroy(),A(void 0)),M.current&&(M.current.cancel(),M.current=null)},[T]),t.useEffect(()=>{S&&T&&T.focus()},[S,T]),t.useEffect(()=>{T&&T.dispatch({effects:e.StateEffect.reconfigure.of(k)})},[E,m,p,v,x,w,b,j,q,y,C,W,L,l,g]),t.useEffect(()=>{if(void 0!==c){var t=T?T.state.doc.toString():"";if(T&&c!==t){var e=()=>{T&&c!==T.state.doc.toString()&&T.dispatch({changes:{from:0,to:T.state.doc.toString().length,insert:c||""},annotations:[a.of(!0)]})};M.current&&!M.current.isDone?N.current=e:e()}}},[c,T]),{state:F,setState:J,view:T,setView:A,container:D,setContainer:O}};
//# sourceMappingURL=useCodeMirror.js.map
