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