'use strict'; const contextKey=Symbol('order-rules-read-context'); // This facade lives for one synchronous read only. Cached rows are internal, // read-only values; public query boundaries copy their result before returning. function readStore(store){ if(store[contextKey])return store; const rows=new Map(),values=new Map(),indexes=new Map(),memo=new Map(),view=Object.create(store); const state={memo,index(name,build){if(!indexes.has(name))indexes.set(name,build());return indexes.get(name);}}; Object.defineProperty(view,contextKey,{value:state}); view.all=function(kind){ if(!rows.has(kind)){ const result=store.all(kind);rows.set(kind,result); let found=values.get(kind);if(!found)values.set(kind,found=new Map());for(const row of result)found.set(row.id,row); } // snapshot callers may reverse/sort the array, never the cached ordering. return rows.get(kind).slice(); }; view.get=function(kind,id){ let found=values.get(kind);if(!found)values.set(kind,found=new Map()); if(!found.has(id))found.set(id,rows.has(kind)?null:store.get(kind,id)); return found.get(id); }; for(const method of ['put','audit','transaction'])view[method]=()=>{throw new Error('Read context cannot write.');}; return view; } function state(store){return store?.[contextKey];} function byOrder(store,kind,id){ const context=state(store);if(!context)return store.all(kind).filter(row=>row.orderId===id); const index=context.index('order:'+kind,()=>{const map=new Map();for(const row of store.all(kind)){if(!map.has(row.orderId))map.set(row.orderId,[]);map.get(row.orderId).push(row);}return map;}); return index.get(id)||[]; } module.exports={readStore,state,byOrder};