"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_chunks-cjs/SharedStateContext.cjs"),t=require("./_chunks-cjs/enableVisualEditing.cjs"),r=require("./_chunks-cjs/mutations.cjs"),s=require("react"),u=require("use-effect-event"),n=require("./_chunks-cjs/create-data-attribute.cjs");exports.createOverlayController=e.c,exports.enableVisualEditing=t.e,exports.getArrayItemKeyAndParentPath=r.b,exports.useDocuments=r.a,exports.createDataAttribute=n.c,exports.useOptimistic=function(e,t){const[n,i]=s.useState(!0),[a,c]=s.useState(e),[o,d]=s.useState(null),[l,f]=s.useState(e),S=r.u(),b=u.useEffectEvent(((e,s)=>(Array.isArray(t)?t:[t]).reduce(((t,s)=>s(t,{document:e.document,id:r.g(e.id),originalId:e.id,type:e.type})),s))),h=u.useEffectEvent((()=>f(e)));return s.useEffect((()=>{if(r.i(S))return;let e;const t=S.on("rebased.local",(t=>{const s={document:t.document,id:t.id,originalId:r.g(t.id),type:"mutate"};c((e=>b(s,e))),d(s),h(),i(!1),clearTimeout(e)})),u=S.on("pristine",(()=>{e=setTimeout((()=>{s.startTransition((()=>i(!0)))}),15e3)}));return()=>{t.unsubscribe(),u.unsubscribe()}}),[S,b,h]),s.useEffect((()=>{if(!n){if(!o)throw new Error("No last event found when syncing passthrough");l!==e&&s.startTransition((()=>{c(b(o,e)),f(e)}))}}),[o,l,e,n,b]),n?e:a},exports.useSharedState=function(t){const r=s.useContext(e.S);if(!r)throw new Error("useSharedState must be used within a SharedStateProvider");const{store:u}=r;return s.useSyncExternalStore(u.subscribe,s.useCallback((()=>u.getState()[t]),[t,u]))};//# sourceMappingURL=index.cjs.map