/* esm.sh - @assistant-ui/tap@0.9.24/dist/react-hooks/useReducer */
import{getCurrentResourceFiber as f,peekResourceFiber as p}from"../core/helpers/execution-context.mjs";import{isDevelopment as g}from"../core/helpers/env.mjs";import{addCommit as h,applyChangelogRecord as w,markReducerDirty as S}from"../core/helpers/root.mjs";import{throwHookOrderChanged as k,throwRenderedMoreHooks as m}from"./utils/hookErrors.mjs";var I=(r,t,u)=>{if(r.isNeverMounted)throw new Error("Resource updated before mount");let l=!1,s=!0;r.root.unsettledCount++,r.root.dispatchUpdate(()=>(l||(l=!0,u&&r.root.changelog.length===0&&!t.cell.isDirty&&!t.hasEagerState&&(t.prevState=t.cell.workInProgress,t.eagerState=u(t.cell.workInProgress,t.action),t.hasEagerState=!0,s=!Object.is(t.cell.current,t.eagerState),!s&&!t.settled&&(t.settled=!0,r.root.unsettledCount--))),s),()=>(l=!0,s=!0,w(t),t.logged||(t.logged=!0,r.root.changelog.push(t)),!0))},P=(r,t,u,l,s)=>{let c=l?l(u):u;g&&r.devStrictMode&&l&&l(u);let a={type:"reducer",workInProgress:c,current:c,isDirty:!1,queue:null,renderQueue:null,reducer:t,dispatch:e=>{let i=p();if(i!==null){if(i!==r)throw new Error("Cannot update a resource while rendering a different resource.");(r.renderPendingCells??=new Set).add(a),(a.renderQueue??=[]).push(e)}else{let o={fiber:r,cell:a,action:e,hasEagerState:!1,eagerState:void 0,prevState:a.current,settled:!1,queued:!1,logged:!1};I(r,o,s?t:void 0)}}};return a};function v(r,t,u,l){let s=f(),c=s.currentIndex++,a=s.cells[c],e=(()=>{if(a!==void 0)return a.type==="reducer"?a:k();s.isFirstRender||m();let o=P(s,r,t,u,l);return s.cells[c]=o,o})(),i=e.queue;if(i!==null){let o=r===e.reducer;for(let d=0;d<i.length;d++){let n=i[d];!n.hasEagerState||!o||!Object.is(n.prevState,e.workInProgress)?(n.prevState=e.workInProgress,n.eagerState=r(e.workInProgress,n.action),n.hasEagerState=!0,g&&s.devStrictMode&&(n.eagerState=r(e.workInProgress,n.action))):g&&s.devStrictMode&&r(e.workInProgress,n.action),n.queued=!1,e.workInProgress=n.eagerState}e.queue=null}if(e.reducer=r,e.renderQueue!==null){let o=e.workInProgress;for(let d of e.renderQueue)o=r(o,d);e.renderQueue=null,s.renderPendingCells?.delete(e),Object.is(o,e.workInProgress)||(S(s,e),e.workInProgress=o)}return e.isDirty&&h(s,()=>{e.current=e.workInProgress,e.isDirty=!1}),[e.workInProgress,e.dispatch]}function q(r,t,u){return v(r,t,u,!1)}export{q as useReducer,v as useReducerImpl};
//# sourceMappingURL=useReducer.mjs.map