var e=[`DRAFT`,`REVIEW`,`LOCKED`,`ARCHIVED`],t={DRAFT:[`REVIEW`,`LOCKED`],REVIEW:[`DRAFT`,`LOCKED`],LOCKED:[`ARCHIVED`],ARCHIVED:[]};function n(e,n){return t[e].includes(n)}function r(e,n){let r=t[e];return e===n?{valid:!1,reason:`Document is already in ${e} state`,allowedTargets:r}:r.includes(n)?{valid:!0,allowedTargets:r}:{valid:!1,reason:`Cannot transition from ${e} to ${n}. Allowed: ${r.join(`, `)||`none (terminal state)`}`,allowedTargets:r}}function i(e){return e===`DRAFT`||e===`REVIEW`}function a(e){return t[e].length===0}var o={requiredCount:1,requireUnanimous:!1,allowedReviewerIds:[],timeoutMs:0,requiredPercentage:0};function s(e,t,n,r){let i=r??Date.now(),a=[],o=[],s=[],c=[],l=t.allowedReviewerIds.length>0?t.allowedReviewerIds:[...new Set(e.map(e=>e.reviewerId))],u=new Map;for(let t of e){let e=u.get(t.reviewerId);(!e||t.timestamp>e.timestamp)&&u.set(t.reviewerId,t)}for(let e of l){let r=u.get(e);if(!r){s.push(e);continue}if(r.atVersion<n){c.push(e);continue}if(t.timeoutMs>0&&i-r.timestamp>t.timeoutMs){c.push(e);continue}r.status===`APPROVED`?a.push(e):r.status===`REJECTED`?o.push(e):r.status===`STALE`?c.push(e):s.push(e)}let d,f;if(o.length>0)d=!1,f=`Rejected by ${o.join(`, `)}`;else if(t.requireUnanimous)d=a.length===l.length&&s.length===0&&c.length===0,f=d?`Unanimous approval (${a.length}/${l.length})`:`Awaiting unanimous approval (${a.length}/${l.length})`;else{let e=t.requiredPercentage&&t.requiredPercentage>0?Math.ceil(Math.min(t.requiredPercentage,100)/100*l.length):t.requiredCount;d=a.length>=e;let n=t.requiredPercentage&&t.requiredPercentage>0?`${t.requiredPercentage}% = ${e}`:`${e}`;f=d?`Approved (${a.length}/${n} required)`:`Needs ${e-a.length} more approval(s) (${a.length}/${n} required)`}return{approved:d,approvedBy:a,rejectedBy:o,pendingFrom:s,staleFrom:c,reason:f}}function c(e,t){return e.map(e=>e.atVersion<t&&e.status!==`STALE`?{...e,status:`STALE`}:e)}export{i as a,r as c,e as i,s as n,a as o,c as r,n as s,o as t};