/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@genie-design/tinybase@5.1.6/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const getTypeOf=e=>typeof e,EMPTY_STRING="",STRING=getTypeOf(EMPTY_STRING),BOOLEAN=getTypeOf(!0),NUMBER=getTypeOf(0),FUNCTION=getTypeOf(getTypeOf),TYPE="type",DEFAULT="default",SUM="sum",AVG="avg",MIN="min",MAX="max",LISTENER="Listener",RESULT="Result",GET="get",ADD="add",HAS="Has",IDS="Ids",TABLE="Table",TABLES="Tables",TABLE_IDS=TABLE+IDS,ROW="Row",ROW_COUNT="RowCount",ROW_IDS=ROW+IDS,SORTED_ROW_IDS="SortedRowIds",CELL="Cell",CELL_IDS=CELL+IDS,VALUE="Value",VALUES="Values",VALUE_IDS=VALUE+IDS,id=e=>EMPTY_STRING+e,strStartsWith=(e,a)=>e.startsWith(a),strEndsWith=(e,a)=>e.endsWith(a),promise=Promise,GLOBAL=globalThis,math=Math,mathMax=math.max,mathMin=math.min,mathFloor=math.floor,isFiniteNumber=isFinite,isInstanceOf=(e,a)=>e instanceof a,isUndefined=e=>null==e,ifNotUndefined=(e,a,t)=>isUndefined(e)?t?.():a(e),isTypeStringOrBoolean=e=>e==STRING||e==BOOLEAN,isString=e=>getTypeOf(e)==STRING,isFunction=e=>getTypeOf(e)==FUNCTION,isArray=e=>Array.isArray(e),slice=(e,a,t)=>e.slice(a,t),size=e=>e.length,test=(e,a)=>e.test(a),getUndefined=()=>{},promiseNew=e=>new promise(e),errorNew=e=>{throw new Error(e)},arrayHas=(e,a)=>e.includes(a),arrayEvery=(e,a)=>e.every(a),arrayIsEqual=(e,a)=>size(e)===size(a)&&arrayEvery(e,((e,t)=>a[t]===e)),arrayIsSorted=(e,a)=>arrayEvery(e,((t,o)=>0==o||a(e[o-1],t)<=0)),arraySort=(e,a)=>e.sort(a),arrayForEach=(e,a)=>e.forEach(a),arrayMap=(e,a)=>e.map(a),arraySum=e=>arrayReduce(e,((e,a)=>e+a),0),arrayIsEmpty=e=>0==size(e),arrayReduce=(e,a,t)=>e.reduce(a,t),arrayClear=(e,a)=>e.splice(0,a),arrayPush=(e,...a)=>e.push(...a),arrayPop=e=>e.pop(),arrayUnshift=(e,...a)=>e.unshift(...a),arrayShift=e=>e.shift(),object=Object,getPrototypeOf=e=>object.getPrototypeOf(e),objEntries=object.entries,objFrozen=object.isFrozen,isObject=e=>!isUndefined(e)&&ifNotUndefined(getPrototypeOf(e),(e=>e==object.prototype||isUndefined(getPrototypeOf(e))),(()=>!0)),objIds=object.keys,objFreeze=object.freeze,objNew=(e=[])=>object.fromEntries(e),objHas=(e,a)=>a in e,objDel=(e,a)=>(delete e[a],e),objForEach=(e,a)=>arrayForEach(objEntries(e),(([e,t])=>a(t,e))),objToArray=(e,a)=>arrayMap(objEntries(e),(([e,t])=>a(t,e))),objSize=e=>size(objIds(e)),objIsEmpty=e=>isObject(e)&&0==objSize(e),objEnsure=(e,a,t)=>(objHas(e,a)||(e[a]=t()),e[a]),objValidate=(e,a,t,o=0)=>isUndefined(e)||!isObject(e)||!o&&objIsEmpty(e)||objFrozen(e)?(t?.(),!1):(objToArray(e,((t,o)=>{a(t,o)||objDel(e,o)})),!!o||!objIsEmpty(e)),collSizeN=e=>a=>arrayReduce(collValues(a),((a,t)=>a+e(t)),0),collSize=e=>e?.size??0,collSize2=collSizeN(collSize),collSize3=collSizeN(collSize2),collSize4=collSizeN(collSize3),collHas=(e,a)=>e?.has(a)??!1,collIsEmpty=e=>isUndefined(e)||0==collSize(e),collValues=e=>[...e?.values()??[]],collClear=e=>e.clear(),collForEach=(e,a)=>e?.forEach(a),collDel=(e,a)=>e?.delete(a),mapNew=e=>new Map(e),mapKeys=e=>[...e?.keys()??[]],mapGet=(e,a)=>e?.get(a),mapForEach=(e,a)=>collForEach(e,((e,t)=>a(t,e))),mapMap=(e,a)=>arrayMap([...e?.entries()??[]],(([e,t])=>a(t,e))),mapSet=(e,a,t)=>isUndefined(t)?(collDel(e,a),e):e?.set(a,t),mapEnsure=(e,a,t,o)=>(collHas(e,a)?o?.(mapGet(e,a)):mapSet(e,a,t()),mapGet(e,a)),mapMatch=(e,a,t,o=mapSet)=>(objToArray(a,((a,o)=>t(e,o,a))),mapForEach(e,(t=>objHas(a,t)?0:o(e,t))),e),mapToObj=(e,a,t,o)=>{const s={};return collForEach(e,((e,l)=>{if(!t?.(e,l)){const t=a?a(e,l):e;o?.(t)||(s[l]=t)}})),s},mapToObj2=(e,a,t)=>mapToObj(e,(e=>mapToObj(e,a,t)),collIsEmpty,objIsEmpty),mapToObj3=(e,a,t)=>mapToObj(e,(e=>mapToObj2(e,a,t)),collIsEmpty,objIsEmpty),mapClone=(e,a)=>{const t=mapNew();return collForEach(e,((e,o)=>t.set(o,a?.(e)??e))),t},mapClone2=e=>mapClone(e,mapClone),mapClone3=e=>mapClone(e,mapClone2),visitTree=(e,a,t,o,s=0)=>ifNotUndefined((t?mapEnsure:mapGet)(e,a[s],s>size(a)-2?t:mapNew),(l=>{if(s>size(a)-2)return o?.(l)&&mapSet(e,a[s]),l;const r=visitTree(l,a,t,o,s+1);return collIsEmpty(l)&&mapSet(e,a[s]),r})),getCellOrValueType=e=>{const a=getTypeOf(e);return isTypeStringOrBoolean(a)||a==NUMBER&&isFiniteNumber(e)?a:void 0},isCellOrValueOrNullOrUndefined=e=>isUndefined(e)||!isUndefined(getCellOrValueType(e)),setOrDelCell=(e,a,t,o,s)=>isUndefined(s)?e.delCell(a,t,o,!0):e.setCell(a,t,o,s),setOrDelValue=(e,a,t)=>isUndefined(t)?e.delValue(a):e.setValue(a,t),setNew=e=>new Set(isArray(e)||isUndefined(e)?e:[e]),setAdd=(e,a)=>e?.add(a),getDefinableFunctions=(e,a,t,o,s)=>{const l=e.hasRow,r=mapNew(),n=mapNew(),i=mapNew(),p=mapNew(),c=mapNew(),d=mapNew(),m=(a,t,...o)=>{const s=mapEnsure(d,a,setNew);return arrayForEach(o,(a=>setAdd(s,a)&&t&&e.callListener(a))),o},h=(a,...t)=>ifNotUndefined(mapGet(d,a),(o=>{arrayForEach(arrayIsEmpty(t)?collValues(o):t,(a=>{e.delListener(a),collDel(o,a)})),collIsEmpty(o)&&mapSet(d,a)})),E=(e,t)=>{mapSet(r,e,t),collHas(n,e)||(mapSet(n,e,a()),mapSet(p,e,mapNew()),mapSet(c,e,mapNew()),s(i))},u=e=>{mapSet(r,e),mapSet(n,e),mapSet(p,e),mapSet(c,e),h(e),s(i)};return[()=>e,()=>mapKeys(r),e=>mapForEach(n,e),e=>collHas(n,e),e=>mapGet(r,e),e=>mapGet(n,e),(e,a)=>mapSet(n,e,a),E,(a,o,s,r,n)=>{E(a,o);const i=mapNew(),d=mapNew(),u=mapGet(p,a),S=mapGet(c,a),g=a=>{const s=t=>e.getCell(o,a,t),p=mapGet(u,a),c=l(o,a)?t(r(s,a)):void 0;if(p===c||isArray(p)&&isArray(c)&&arrayIsEqual(p,c)||mapSet(i,a,[p,c]),!isUndefined(n)){const e=mapGet(S,a),t=l(o,a)?n(s,a):void 0;e!=t&&mapSet(d,a,t)}},y=e=>{s((()=>{collForEach(i,(([,e],a)=>mapSet(u,a,e))),collForEach(d,((e,a)=>mapSet(S,a,e)))}),i,d,u,S,e),collClear(i),collClear(d)};mapForEach(u,g),e.hasTable(o)&&arrayForEach(e.getRowIds(o),(e=>{collHas(u,e)||g(e)})),y(!0),h(a),m(a,0,e.addRowListener(o,null,((e,a,t)=>g(t))),e.addTableListener(o,(()=>y())))},u,e=>o(e,i),()=>mapForEach(d,u),m,h]},getRowCellFunction=(e,a)=>isString(e)?a=>a(e):e??(()=>a??EMPTY_STRING),getCreateFunction=(e,a)=>{const t=new WeakMap;return o=>{t.has(o)||t.set(o,e(o));const s=t.get(o);return a?.(s),s}},INTEGER=/^\d+$/,getPoolFunctions=()=>{const e=[];let a=0;return[t=>(t?arrayShift(e):null)??EMPTY_STRING+a++,a=>{test(INTEGER,a)&&size(e)<1e3&&arrayPush(e,a)}]},getWildcardedLeaves=(e,a=[EMPTY_STRING])=>{const t=[],o=(e,s)=>s==size(a)?arrayPush(t,e):null===a[s]?collForEach(e,(e=>o(e,s+1))):arrayForEach([a[s],null],(a=>o(mapGet(e,a),s+1)));return o(e,0),t},getListenerFunctions=e=>{let a;const[t,o]=getPoolFunctions(),s=mapNew();return[(o,l,r,n=[],i=()=>[])=>{a??=e();const p=t(1);return mapSet(s,p,[o,l,r,n,i]),setAdd(visitTree(l,r??[EMPTY_STRING],setNew),p),p},(e,t,...o)=>arrayForEach(getWildcardedLeaves(e,t),(e=>collForEach(e,(e=>mapGet(s,e)[0](a,...t??[],...o))))),e=>ifNotUndefined(mapGet(s,e),(([,a,t])=>(visitTree(a,t??[EMPTY_STRING],void 0,(a=>(collDel(a,e),collIsEmpty(a)?1:0))),mapSet(s,e),o(e),t))),e=>ifNotUndefined(mapGet(s,e),(([e,,t=[],o,s])=>{const l=(...r)=>{const n=size(r);n==size(t)?e(a,...r,...s(r)):isUndefined(t[n])?arrayForEach(o[n]?.(...r)??[],(e=>l(...r,e))):l(...r,t[n])};l()}))]},createCheckpoints=getCreateFunction((e=>{let a,t,o,s=100,l=mapNew(),r=mapNew(),n=1;const i=mapNew(),p=mapNew(),[c,d,m]=getListenerFunctions((()=>R)),h=mapNew(),E=mapNew(),u=[],S=[],g=(a,t)=>{n=0,e.transaction((()=>{const[o,s]=mapGet(h,t);collForEach(o,((t,o)=>collForEach(t,((t,s)=>collForEach(t,((t,l)=>setOrDelCell(e,o,s,l,t[a]))))))),collForEach(s,((t,o)=>setOrDelValue(e,o,t[a])))})),n=1},y=e=>{mapSet(h,e),mapSet(E,e),d(p,[e])},N=(e,a)=>arrayForEach(arrayClear(e,a??size(e)),y),f=()=>N(u,size(u)-s),w=()=>ifNotUndefined(a,(()=>{arrayPush(u,a),f(),N(S),a=void 0,o=1})),T=()=>{a=arrayPop(u),o=1};let b,I;const C=(e=EMPTY_STRING)=>(isUndefined(a)&&(a=EMPTY_STRING+t++,mapSet(h,a,[l,r]),L(a,e),l=mapNew(),r=mapNew(),o=1),a),F=()=>{arrayIsEmpty(u)||(arrayUnshift(S,C()),g(0,a),a=arrayPop(u),o=1)},j=()=>{arrayIsEmpty(S)||(arrayPush(u,a),a=arrayShift(S),g(1,a),o=1)},M=()=>{o&&(d(i),o=0)},G=e=>{const a=C(e);return M(),a},L=(e,a)=>(U(e)&&mapGet(E,e)!==a&&(mapSet(E,e,a),d(p,[e])),R),U=e=>collHas(h,e),R={setSize:e=>(s=e,f(),R),addCheckpoint:G,setCheckpoint:L,getStore:()=>e,getCheckpointIds:()=>[[...u],a,[...S]],forEachCheckpoint:e=>mapForEach(E,e),hasCheckpoint:U,getCheckpoint:e=>mapGet(E,e),goBackward:()=>(F(),M(),R),goForward:()=>(j(),M(),R),goTo:e=>{const t=arrayHas(u,e)?F:arrayHas(S,e)?j:null;for(;!isUndefined(t)&&e!=a;)t();return M(),R},addCheckpointIdsListener:e=>c(e,i),addCheckpointListener:(e,a)=>c(a,p,[e]),delListener:e=>(m(e),R),clear:()=>(N(u),N(S),isUndefined(a)||y(a),a=void 0,t=0,G(),R),clearForward:()=>(arrayIsEmpty(S)||(N(S),d(i)),R),destroy:()=>{e.delListener(b),e.delListener(I)},getListenerStats:()=>({checkpointIds:collSize2(i),checkpoint:collSize2(p)}),_registerListeners:()=>{b=e.addCellListener(null,null,null,((e,a,t,o,s,r)=>{if(n){w();const e=mapEnsure(l,a,mapNew),n=mapEnsure(e,t,mapNew),i=mapEnsure(n,o,(()=>[r,void 0]));i[1]=s,i[0]===s&&collIsEmpty(mapSet(n,o))&&collIsEmpty(mapSet(e,t))&&collIsEmpty(mapSet(l,a))&&T(),M()}})),I=e.addValueListener(null,((e,a,t,o)=>{if(n){w();const e=mapEnsure(r,a,(()=>[o,void 0]));e[1]=t,e[0]===t&&collIsEmpty(mapSet(r,a))&&T(),M()}}))}};return objFreeze(R.clear())}),(e=>e._registerListeners())),MASK6=63,ENCODE="-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz".split(EMPTY_STRING),DECODE=mapNew(arrayMap(ENCODE,((e,a)=>[e,a]))),encode=e=>ENCODE[63&e],decode=(e,a)=>mapGet(DECODE,e[a])??0,getRandomValues=GLOBAL.crypto?e=>GLOBAL.crypto.getRandomValues(e):e=>arrayMap(e,(()=>mathFloor(256*math.random()))),defaultSorter=(e,a)=>(e??0)<(a??0)?-1:1,getUniqueId=(e=16)=>arrayReduce(getRandomValues(new Uint8Array(e)),((e,a)=>e+encode(a)),""),createIndexes=getCreateFunction((e=>{const a=mapNew(),t=mapNew(),[o,s,l]=getListenerFunctions((()=>y)),[r,n,i,p,c,d,m,,h,E,u,S]=getDefinableFunctions(e,mapNew,(e=>isUndefined(e)?EMPTY_STRING:isArray(e)?arrayMap(e,id):id(e)),o,s),g=(a,t,o)=>{const s=c(a);collForEach(o,((a,o)=>t(o,(t=>collForEach(a,(a=>t(a,(t=>e.forEachCell(s,a,t)))))))))},y={setIndexDefinition:(e,o,l,r,n,i=defaultSorter)=>{const p=isUndefined(n)?void 0:([e],[a])=>n(e,a);return h(e,o,((o,l,n,c,h,E)=>{let u=0;const S=setNew(),g=setNew(),y=d(e);if(collForEach(l,(([e,a],t)=>{const o=setNew(e),s=setNew(a);collForEach(o,(e=>collDel(s,e)?collDel(o,e):0)),collForEach(o,(e=>{setAdd(S,e),ifNotUndefined(mapGet(y,e),(a=>{collDel(a,t),collIsEmpty(a)&&(mapSet(y,e),u=1)}))})),collForEach(s,(e=>{setAdd(S,e),collHas(y,e)||(mapSet(y,e,setNew()),u=1),setAdd(mapGet(y,e),t),isUndefined(r)||setAdd(g,e)}))})),o(),collIsEmpty(h)||(E?mapForEach(y,(e=>setAdd(g,e))):mapForEach(n,(e=>ifNotUndefined(mapGet(c,e),(e=>setAdd(g,e))))),collForEach(g,(e=>{const a=(a,t)=>i(mapGet(h,a),mapGet(h,t),e),t=[...mapGet(y,e)];arrayIsSorted(t,a)||(mapSet(y,e,setNew(arraySort(t,a))),setAdd(S,e))}))),(u||E)&&!isUndefined(p)){const a=[...y];arrayIsSorted(a,p)||(m(e,mapNew(arraySort(a,p))),u=1)}u&&s(a,[e]),collForEach(S,(a=>s(t,[e,a])))}),getRowCellFunction(l),ifNotUndefined(r,getRowCellFunction)),y},delIndexDefinition:e=>(E(e),y),getStore:r,getIndexIds:n,forEachIndex:e=>i(((a,t)=>e(a,(e=>g(a,e,t))))),forEachSlice:(e,a)=>g(e,a,d(e)),hasIndex:p,hasSlice:(e,a)=>collHas(d(e),a),getTableId:c,getSliceIds:e=>mapKeys(d(e)),getSliceRowIds:(e,a)=>collValues(mapGet(d(e),a)),addIndexIdsListener:u,addSliceIdsListener:(e,t)=>o(t,a,[e]),addSliceRowIdsListener:(e,a,s)=>o(s,t,[e,a]),delListener:e=>(l(e),y),destroy:S,getListenerStats:()=>({sliceIds:collSize2(a),sliceRowIds:collSize3(t)})};return objFreeze(y)})),numericAggregators=mapNew([[AVG,[(e,a)=>arraySum(e)/a,(e,a,t)=>e+(a-e)/(t+1),(e,a,t)=>e+(e-a)/(t-1),(e,a,t,o)=>e+(a-t)/o]],[MAX,[e=>mathMax(...e),(e,a)=>mathMax(a,e),(e,a)=>a==e?void 0:e,(e,a,t)=>t==e?void 0:mathMax(a,e)]],[MIN,[e=>mathMin(...e),(e,a)=>mathMin(a,e),(e,a)=>a==e?void 0:e,(e,a,t)=>t==e?void 0:mathMin(a,e)]],[SUM,[e=>arraySum(e),(e,a)=>e+a,(e,a)=>e-a,(e,a,t)=>e-t+a]]]),getAggregateValue=(e,a,t,o,s,l=!1)=>{if(collIsEmpty(t))return;const[r,n,i,p]=s;return l||=isUndefined(e),collForEach(o,(([t,o])=>{l||(e=isUndefined(t)?n?.(e,o,a++):isUndefined(o)?i?.(e,t,a--):p?.(e,o,t,a),l||=isUndefined(e))})),l?r(collValues(t),collSize(t)):e},createMetrics=getCreateFunction((e=>{const a=mapNew(),[t,o,s]=getListenerFunctions((()=>S)),[l,r,n,i,p,c,d,,m,h,E,u]=getDefinableFunctions(e,getUndefined,(e=>isNaN(e)||isUndefined(e)||!0===e||!1===e||e===EMPTY_STRING?void 0:1*e),t,o),S={setMetricDefinition:(e,t,s,l,r,n,i)=>{const p=isFunction(s)?[s,r,n,i]:mapGet(numericAggregators,s)??mapGet(numericAggregators,SUM);return m(e,t,((t,s,l,r,n,i)=>{const m=c(e),h=collSize(r);i||=isUndefined(m),t();let E=getAggregateValue(m,h,r,s,p,i);isFiniteNumber(E)||(E=void 0),E!=m&&(d(e,E),o(a,[e],E,m))}),getRowCellFunction(l,1)),S},delMetricDefinition:e=>(h(e),S),getStore:l,getMetricIds:r,forEachMetric:n,hasMetric:i,getTableId:p,getMetric:c,addMetricIdsListener:E,addMetricListener:(e,o)=>t(o,a,[e]),delListener:e=>(s(e),S),destroy:u,getListenerStats:()=>({metric:collSize2(a)})};return objFreeze(S)})),Persists={StoreOnly:1,MergeableStoreOnly:2,StoreOrMergeableStore:3},scheduleRunning=mapNew(),scheduleActions=mapNew(),getStoreFunctions=(e=1,a)=>1!=e&&a.isMergeable()?[1,a.getMergeableContent,a.getTransactionMergeableChanges,([[e],[a]])=>!objIsEmpty(e)||!objIsEmpty(a),a.setDefaultContent]:2!=e?[0,a.getContent,a.getTransactionChanges,([e,a])=>!objIsEmpty(e)||!objIsEmpty(a),a.setContent]:errorNew("Store type not supported by this Persister"),createCustomPersister=(e,a,t,o,s,l,r,n={},i=[])=>{let p,c,d,m=0,h=0,E=0;mapEnsure(scheduleRunning,i,(()=>0)),mapEnsure(scheduleActions,i,(()=>[]));const[u,S,g,y,N]=getStoreFunctions(r,e),f=a=>{(u&&isArray(a?.[0])?1===a?.[2]?e.applyMergeableChanges:e.setMergeableContent:1===a?.[2]?e.applyChanges:e.setContent)(a)},w=async e=>(2!=m&&(m=1,h++,await C((async()=>{try{const e=await a();isArray(e)?f(e):errorNew(`Content is not an array ${e}`)}catch(a){l?.(a),e&&N(e)}m=0}))),F),T=()=>(c&&(s(c),c=void 0),F),b=async e=>(1!=m&&(m=2,E++,await C((async()=>{try{await t(S,e)}catch(e){l?.(e)}m=0}))),F),I=()=>(ifNotUndefined(d,e.delListener),d=void 0,F),C=async(...e)=>(arrayPush(mapGet(scheduleActions,i),...e),await(async()=>{if(!mapGet(scheduleRunning,i)){for(mapSet(scheduleRunning,i,1);!isUndefined(p=arrayShift(mapGet(scheduleActions,i)));)try{await p()}catch(e){l?.(e)}mapSet(scheduleRunning,i,0)}})(),F),F={load:w,startAutoLoad:async e=>{await T().load(e);try{c=o((async(e,a)=>{a||e?2!=m&&(m=1,h++,f(a??e),m=0):await w()}))}catch(e){l?.(e)}return F},stopAutoLoad:T,isAutoLoading:()=>!isUndefined(c),save:b,startAutoSave:async()=>(await I().save(),d=e.addDidFinishTransactionListener((()=>{const e=g();y(e)&&b(e)})),F),stopAutoSave:I,isAutoSaving:()=>!isUndefined(d),schedule:C,getStore:()=>e,destroy:()=>(arrayClear(mapGet(scheduleActions,i)),T().stopAutoSave()),getStats:()=>({loads:h,saves:E}),...n};return objFreeze(F)},textEncoder=new GLOBAL.TextEncoder,getHash=e=>{let a=2166136261;return arrayForEach(textEncoder.encode(e),(e=>{a^=e,a+=(a<<1)+(a<<4)+(a<<7)+(a<<8)+(a<<24)})),a>>>0},stampCloneWithHash=([e,a,t])=>[e,a,t],stampCloneWithoutHash=([e,a])=>newStamp(e,a),newStamp=(e,a)=>a?[e,a]:[e],getStampHash=e=>e[2],hashIdAndHash=(e,a)=>getHash(e+":"+a),replaceTimeHash=(e,a)=>a>e?(e?getHash(e):0)^getHash(a):0,getLatestTime=(e,a)=>((e??"")>(a??"")?e:a)??"",stampUpdate=(e,a,t)=>{e[2]=a>>>0,t>e[1]&&(e[1]=t)},stampNewObj=(e=EMPTY_STRING)=>newStamp(objNew(),e),stampNewMap=(e=EMPTY_STRING)=>[mapNew(),e,0],stampMapToObjWithHash=([e,a,t],o=stampCloneWithHash)=>[mapToObj(e,o),a,t],stampMapToObjWithoutHash=([e,a],t=stampCloneWithoutHash)=>newStamp(mapToObj(e,t),a),stampValidate=(e,a)=>isArray(e)&&3==size(e)&&isString(e[1])&&getTypeOf(e[2])==NUMBER&&isFiniteNumber(e[2])&&a(e[0]),Message={Response:0,GetContentHashes:1,ContentHashes:2,ContentDiff:3,GetTableDiff:4,GetRowDiff:5,GetCellDiff:6,GetValueDiff:7},createCustomSynchronizer=(e,a,t,o,s,l,r,n,i={})=>{let p,c=0,d=0;const m=mapNew(),h=(e,t,o,s)=>{c++,l?.(e,t,o,s),a(e,t,o,s)},E=async(e,a,t=EMPTY_STRING)=>promiseNew(((o,l)=>{const r=getUniqueId(),n=setTimeout((()=>{collDel(m,r),l(`No response from ${e??"anyone"} to ${r}, `+a)}),1e3*s);mapSet(m,r,[e,(e,a)=>{clearTimeout(n),collDel(m,r),o([e,a])}]),h(e,r,a,t)})),u=(e,[a,t])=>{objForEach(a,(([a,t],o)=>{const s=objEnsure(e[0],o,stampNewObj);objForEach(a,(([e,a],t)=>{const o=objEnsure(s[0],t,stampNewObj);objForEach(e,(([e,a],t)=>o[0][t]=newStamp(e,a))),o[1]=getLatestTime(o[1],a)})),s[1]=getLatestTime(s[1],t)})),e[1]=getLatestTime(e[1],t)},S=async(a=null,t)=>{isUndefined(t)&&([t,a]=await E(a,1));const[o,s]=t,[l,r]=e.getMergeableContentHashes();let n=stampNewObj();if(l!=o){const[t,o]=(await E(a,4,e.getMergeableTableHashes()))[0];if(n=t,!objIsEmpty(o)){const[t,s]=(await E(a,5,e.getMergeableRowHashes(o)))[0];if(u(n,t),!objIsEmpty(s)){const t=(await E(a,6,e.getMergeableCellHashes(s)))[0];u(n,t)}}}return[n,r==s?stampNewObj():(await E(a,7,e.getMergeableValueHashes()))[0],1]},g=createCustomPersister(e,(async()=>{const e=await S();return objIsEmpty(e[0][0])&&objIsEmpty(e[1][0])?void 0:e}),(async(a,t)=>t?h(null,null,3,t):h(null,null,2,e.getMergeableContentHashes())),(e=>p=e),(()=>p=void 0),n,2,{startSync:async e=>await(await g.startAutoLoad(e)).startAutoSave(),stopSync:()=>g.stopAutoLoad().stopAutoSave(),destroy:()=>(o(),g.stopSync()),getSynchronizerStats:()=>({sends:c,receives:d}),...i});return t(((a,t,o,s)=>{d++,r?.(a,t,o,s),0==o?ifNotUndefined(mapGet(m,t),(([e,t])=>isUndefined(e)||e==a?t(s,a):0)):2==o&&g.isAutoLoading()?S(a,s).then((e=>{p?.(void 0,e)})):3==o&&g.isAutoLoading()?p?.(void 0,s):ifNotUndefined(1==o&&g.isAutoSaving()?e.getMergeableContentHashes():4==o?e.getMergeableTableDiff(s):5==o?e.getMergeableRowDiff(s):6==o?e.getMergeableCellDiff(s):7==o?e.getMergeableValueDiff(s):void 0,(e=>{h(a,t,0,e)}))})),g},createQueries=getCreateFunction((e=>{const a=e.createStore,t=a(),o=a(),s=mapNew(),{addListener:l,callListeners:r,delListener:n}=o,[i,p,c,d,m,,,h,,E,u,S,g,y]=getDefinableFunctions(e,(()=>!0),getUndefined,l,r),N=(e,a,...t)=>arrayForEach(t,(t=>setAdd(mapEnsure(mapEnsure(s,a,mapNew),e,setNew),t))),f=e=>{ifNotUndefined(mapGet(s,e),(e=>{mapForEach(e,((e,a)=>collForEach(a,(a=>e.delListener(a))))),collClear(e)})),arrayForEach([o,t],(a=>a.delTable(e)))},w=(e,a,t)=>N(a,e,a.addStartTransactionListener(t.startTransaction),a.addDidFinishTransactionListener((()=>t.finishTransaction()))),T={setQueryDefinition:(a,s,l)=>{h(a,s),f(a);const r=[],n=[[null,[s,null,null,[],mapNew()]]],i=[],p=[],c=[];l({select:(e,a)=>{const t=isFunction(e)?[size(r)+EMPTY_STRING,e]:[isUndefined(a)?e:a,t=>t(e,a)];return arrayPush(r,t),{as:e=>t[0]=e}},join:(e,a,t)=>{const o=isUndefined(t)||isFunction(a)?null:a,s=isUndefined(o)?a:t,l=[e,[e,o,isFunction(s)?s:e=>e(s),[],mapNew()]];return arrayPush(n,l),{as:e=>l[0]=e}},where:(e,a,t)=>arrayPush(i,isFunction(e)?e:isUndefined(t)?t=>t(e)===a:o=>o(e,a)===t),group:(e,a,t,o,s)=>{const l=[e,[e,isFunction(a)?[a,t,o,s]:mapGet(numericAggregators,a)??[(e,a)=>a]]];return arrayPush(p,l),{as:e=>l[0]=e}},having:(e,a)=>arrayPush(c,isFunction(e)?e:t=>t(e)===a)});const d=mapNew(r);if(collIsEmpty(d))return T;const m=mapNew(n);mapForEach(m,((e,[,a])=>ifNotUndefined(mapGet(m,a),(({3:a})=>isUndefined(e)?0:arrayPush(a,e)))));const E=mapNew(p);let u=t;if(collIsEmpty(E)&&arrayIsEmpty(c))u=o;else{w(a,u,o);const e=mapNew();mapForEach(E,((a,[t,o])=>setAdd(mapEnsure(e,t,setNew),[a,o])));const t=setNew();mapForEach(d,(a=>collHas(e,a)?0:setAdd(t,a)));const s=mapNew(),l=(t,s,l,r)=>ifNotUndefined(t,(([n,i,p,d])=>{mapForEach(s,((a,[t])=>{const o=mapEnsure(n,a,mapNew),s=mapGet(o,l),i=r?void 0:t;if(s!==i){const t=setNew([[s,i]]),r=collSize(o);mapSet(o,l,i),collForEach(mapGet(e,a),(([e,a])=>{const s=getAggregateValue(d[e],r,o,t,a);d[e]=isUndefined(getCellOrValueType(s))?null:s}))}})),collIsEmpty(i)||!arrayEvery(c,(e=>e((e=>d[e]))))?o.delRow(a,p):isUndefined(p)?t[2]=o.addRow(a,d):o.setRow(a,p,d)}));N(u,a,u.addRowListener(a,null,((o,r,n,i)=>{const p=[],c=[],d=mapNew(),m=u.hasRow(a,n);let h=!m;collForEach(t,(e=>{const[t,o,s]=i(a,n,e);arrayPush(p,o),arrayPush(c,s),h||=t})),mapForEach(e,(e=>{const[t,,o]=i(a,n,e);(h||t)&&mapSet(d,e,[o])})),h&&l(visitTree(s,p,void 0,(([,e])=>(collDel(e,n),collIsEmpty(e)))),d,n,1),m&&l(visitTree(s,c,(()=>{const e={};return collForEach(t,(t=>e[t]=u.getCell(a,n,t))),[mapNew(),setNew(),void 0,e]}),(([,e])=>{setAdd(e,n)})),d,n)})))}w(a,e,u);const S=(t,o,l,r)=>{const n=a=>e.getCell(o,l,a);arrayForEach(r,(o=>{const[s,,l,r,i]=mapGet(m,o),p=l?.(n,t),[c,d]=mapGet(i,t)??[];p!=c&&(isUndefined(d)||y(a,d),mapSet(i,t,isUndefined(p)?null:[p,...g(a,1,e.addRowListener(s,p,(()=>S(t,s,p,r))))]))})),(t=>{const o=(a,o)=>e.getCell(...isUndefined(o)?[s,t,a]:a===s?[s,t,o]:[mapGet(m,a)?.[0],mapGet(mapGet(m,a)?.[4],t)?.[0],o]);u.transaction((()=>arrayEvery(i,(e=>e(o)))?mapForEach(d,((e,s)=>setOrDelCell(u,a,t,e,s(o,t)))):u.delRow(a,t)))})(t)},{3:b}=mapGet(m,null);return u.transaction((()=>g(a,1,e.addRowListener(s,null,((t,o,l)=>{e.hasRow(s,l)?S(l,s,l,b):(u.delRow(a,l),collForEach(m,(({4:e})=>ifNotUndefined(mapGet(e,l),(([,t])=>{y(a,t),mapSet(e,l)})))))}))))),T},delQueryDefinition:e=>(f(e),E(e),T),getStore:i,getQueryIds:p,forEachQuery:c,hasQuery:d,getTableId:m,addQueryIdsListener:e=>u((()=>e(T))),delListener:e=>(n(e),T),destroy:S,getListenerStats:()=>{const{tables:e,tableIds:a,transaction:t,...s}=o.getListenerStats();return s}};return objToArray({[TABLE]:[1,1],TableCellIds:[0,1],[ROW_COUNT]:[0,1],[ROW_IDS]:[0,1],[SORTED_ROW_IDS]:[0,5],[ROW]:[1,2],[CELL_IDS]:[0,2],[CELL]:[1,3]},(([e,a],t)=>{arrayForEach(e?[GET,"has","forEach"]:[GET],(e=>T[e+RESULT+t]=(...a)=>o[e+t](...a))),T[ADD+RESULT+t+LISTENER]=(...e)=>o[ADD+t+LISTENER](...slice(e,0,a),((t,...o)=>e[a](T,...o)),!0)})),objFreeze(T)})),createRelationships=getCreateFunction((e=>{const a=mapNew(),t=mapNew(),o=mapNew(),s=mapNew(),[l,r,n]=getListenerFunctions((()=>w)),[i,p,c,d,m,h,,,E,u,S,g]=getDefinableFunctions(e,(()=>[mapNew(),mapNew(),mapNew(),mapNew()]),(e=>isUndefined(e)?void 0:e+EMPTY_STRING),l,r),y=(e,a,t)=>ifNotUndefined(h(e),(([o,,s])=>{if(!collHas(s,a)){const l=setNew();if(m(e)!=f(e))setAdd(l,a);else{let e=a;for(;!isUndefined(e)&&!collHas(l,e);)setAdd(l,e),e=mapGet(o,e)}if(t)return l;mapSet(s,a,l)}return mapGet(s,a)})),N=(e,a)=>ifNotUndefined(h(e),(([,,e])=>mapSet(e,a))),f=e=>mapGet(a,e),w={setRelationshipDefinition:(e,l,n,i)=>(mapSet(a,e,n),E(e,l,((a,l)=>{const n=setNew(),i=setNew(),p=setNew(),[c,d]=h(e);collForEach(l,(([a,t],o)=>{isUndefined(a)||(setAdd(i,a),ifNotUndefined(mapGet(d,a),(e=>{collDel(e,o),collIsEmpty(e)&&mapSet(d,a)}))),isUndefined(t)||(setAdd(i,t),collHas(d,t)||mapSet(d,t,setNew()),setAdd(mapGet(d,t),o)),setAdd(n,o),mapSet(c,o,t),mapForEach(mapGet(s,e),(a=>{collHas(y(e,a),o)&&setAdd(p,a)}))})),a(),collForEach(n,(a=>r(t,[e,a]))),collForEach(i,(a=>r(o,[e,a]))),collForEach(p,(a=>{N(e,a),r(s,[e,a])}))}),getRowCellFunction(i)),w),delRelationshipDefinition:e=>(mapSet(a,e),u(e),w),getStore:i,getRelationshipIds:p,forEachRelationship:a=>c((t=>a(t,(a=>e.forEachRow(m(t),a))))),hasRelationship:d,getLocalTableId:m,getRemoteTableId:f,getRemoteRowId:(e,a)=>mapGet(h(e)?.[0],a),getLocalRowIds:(e,a)=>collValues(mapGet(h(e)?.[1],a)),getLinkedRowIds:(e,a)=>isUndefined(h(e))?[a]:collValues(y(e,a,!0)),addRelationshipIdsListener:S,addRemoteRowIdListener:(e,a,o)=>l(o,t,[e,a]),addLocalRowIdsListener:(e,a,t)=>l(t,o,[e,a]),addLinkedRowIdsListener:(e,a,t)=>(y(e,a),l(t,s,[e,a])),delListener:e=>(N(...n(e)??[]),w),destroy:g,getListenerStats:()=>({remoteRowId:collSize3(t),localRowIds:collSize3(o),linkedRowIds:collSize3(s)})};return objFreeze(w)})),pairNew=e=>[e,e],pairCollSize2=(e,a=collSize2)=>a(e[0])+a(e[1]),pairNewMap=()=>[mapNew(),mapNew()],pairClone=e=>[...e],pairIsEqual=([e,a])=>e===a,jsonStringWithMap=e=>JSON.stringify(e,((e,a)=>isInstanceOf(a,Map)?object.fromEntries([...a]):a)),jsonParse=JSON.parse,idsChanged=(e,a,t)=>mapSet(e,a,mapGet(e,a)==-t?void 0:t),createStore=()=>{let e,a,t=!1,o=!1,s=0,l=[];const r=mapNew(),n=mapNew(),i=mapNew(),p=mapNew(),c=mapNew(),d=mapNew(),m=mapNew(),h=mapNew(),E=mapNew(),u=mapNew(),S=mapNew(),g=mapNew(),y=mapNew(),N=mapNew(),f=setNew(),w=mapNew(),T=mapNew(),b=mapNew(),I=mapNew(),C=pairNewMap(),F=pairNewMap(),j=pairNewMap(),M=pairNewMap(),G=pairNewMap(),L=pairNewMap(),U=pairNewMap(),R=pairNewMap(),A=pairNewMap(),O=pairNewMap(),H=pairNewMap(),D=pairNewMap(),V=pairNewMap(),z=pairNewMap(),v=pairNewMap(),P=pairNewMap(),W=pairNewMap(),_=pairNewMap(),Y=pairNewMap(),B=pairNewMap(),x=pairNewMap(),k=pairNewMap(),q=mapNew(),J=pairNewMap(),[K,Q,$,X]=getListenerFunctions((()=>Na)),Z=e=>{if(!objValidate(e,((e,a)=>arrayHas([TYPE,DEFAULT],a))))return!1;const a=e[TYPE];return!(!isTypeStringOrBoolean(a)&&a!=NUMBER)&&(getCellOrValueType(e[DEFAULT])!=a&&objDel(e,DEFAULT),!0)},ee=isArray,ae=(a,t)=>(!e||collHas(S,t)||Ue(t))&&objValidate(a,((e,a)=>te(t,a,e)),(()=>Ue(t))),te=(e,a,t,o)=>objValidate(o?t:re(t,e,a),((o,s)=>ifNotUndefined(oe(e,a,s,o),(e=>(t[s]=e,!0)),(()=>!1))),(()=>Ue(e,a))),oe=(a,t,o,s)=>e?ifNotUndefined(mapGet(mapGet(S,a),o),(e=>getCellOrValueType(s)!=e[TYPE]?Ue(a,t,o,s,e[DEFAULT]):s),(()=>Ue(a,t,o,s))):isUndefined(getCellOrValueType(s))?Ue(a,t,o,s):s,se=(e,a)=>objValidate(a?e:ne(e),((a,t)=>ifNotUndefined(le(t,a),(a=>(e[t]=a,!0)),(()=>!1))),(()=>Re())),le=(e,t)=>a?ifNotUndefined(mapGet(y,e),(a=>getCellOrValueType(t)!=a[TYPE]?Re(e,t,a[DEFAULT]):t),(()=>Re(e,t))):isUndefined(getCellOrValueType(t))?Re(e,t):t,re=(e,a,t)=>(ifNotUndefined(mapGet(g,a),(([o,s])=>{collForEach(o,((a,t)=>{objHas(e,t)||(e[t]=a)})),collForEach(s,(o=>{objHas(e,o)||Ue(a,t,o)}))})),e),ne=e=>(a&&(collForEach(N,((a,t)=>{objHas(e,t)||(e[t]=a)})),collForEach(f,(a=>{objHas(e,a)||Re(a)}))),e),ie=e=>mapMatch(S,e,((e,a,t)=>{const o=mapNew(),s=setNew();mapMatch(mapEnsure(S,a,mapNew),t,((e,a,t)=>{mapSet(e,a,t),ifNotUndefined(t[DEFAULT],(e=>mapSet(o,a,e)),(()=>setAdd(s,a)))})),mapSet(g,a,[o,s])}),((e,a)=>{mapSet(S,a),mapSet(g,a)})),pe=e=>mapMatch(y,e,((e,a,t)=>{mapSet(y,a,t),ifNotUndefined(t[DEFAULT],(e=>mapSet(N,a,e)),(()=>setAdd(f,a)))}),((e,a)=>{mapSet(y,a),mapSet(N,a),collDel(f,a)})),ce=e=>objIsEmpty(e)?ca():la(e),de=e=>mapMatch(b,e,((e,a,t)=>me(a,t)),((e,a)=>we(a))),me=(e,a)=>mapMatch(mapEnsure(b,e,(()=>(Ce(e,1),mapSet(w,e,getPoolFunctions()),mapSet(T,e,mapNew()),mapNew()))),a,((a,t,o)=>he(e,a,t,o)),((a,t)=>Te(e,a,t))),he=(e,a,t,o,s)=>mapMatch(mapEnsure(a,t,(()=>(Fe(e,t,1),mapNew()))),o,((a,o,s)=>Ee(e,t,a,o,s)),((o,l)=>be(e,a,t,o,l,s))),Ee=(e,a,t,o,s)=>{collHas(t,o)||je(e,a,o,1);const l=mapGet(t,o);s!==l&&(Me(e,a,o,l,s),mapSet(t,o,s))},ue=(e,a,t,o,s)=>ifNotUndefined(mapGet(a,t),(a=>Ee(e,t,a,o,s)),(()=>he(e,a,t,re({[o]:s},e,t)))),Se=e=>objIsEmpty(e)?ha():ra(e),ge=e=>mapMatch(I,e,((e,a,t)=>ye(a,t)),((e,a)=>Ie(a))),ye=(e,a)=>{collHas(I,e)||Ge(e,1);const t=mapGet(I,e);a!==t&&(Le(e,t,a),mapSet(I,e,a))},Ne=(e,a)=>{const[t]=mapGet(w,e),o=t(a);return collHas(mapGet(b,e),o)?Ne(e,a):o},fe=e=>mapGet(b,e)??me(e,{}),we=e=>me(e,{}),Te=(e,a,t)=>{const[,o]=mapGet(w,e);o(t),he(e,a,t,{},!0)},be=(e,a,t,o,s,l)=>{const r=mapGet(mapGet(g,e)?.[0],s);if(!isUndefined(r)&&!l)return Ee(e,t,o,s,r);const n=a=>{Me(e,t,a,mapGet(o,a)),je(e,t,a,-1),mapSet(o,a)};isUndefined(r)?n(s):mapForEach(o,n),collIsEmpty(o)&&(Fe(e,t,-1),collIsEmpty(mapSet(a,t))&&(Ce(e,-1),mapSet(b,e),mapSet(w,e),mapSet(T,e)))},Ie=e=>{const a=mapGet(N,e);if(!isUndefined(a))return ye(e,a);Le(e,mapGet(I,e)),Ge(e,-1),mapSet(I,e)},Ce=(e,a)=>idsChanged(r,e,a),Fe=(e,a,t)=>idsChanged(mapEnsure(p,e,mapNew),a,t)&&mapSet(i,e,mapEnsure(i,e,(()=>0))+t),je=(e,a,t,o)=>{const s=mapGet(T,e),l=mapGet(s,t)??0;(0==l&&1==o||1==l&&-1==o)&&idsChanged(mapEnsure(n,e,mapNew),t,o),mapSet(s,t,l!=-o?l+o:null),idsChanged(mapEnsure(mapEnsure(c,e,mapNew),a,mapNew),t,o)},Me=(e,a,t,o,s)=>{mapEnsure(mapEnsure(mapEnsure(d,e,mapNew),a,mapNew),t,(()=>[o,0]))[1]=s,l[3]?.(e,a,t,s)},Ge=(e,a)=>idsChanged(m,e,a),Le=(e,a,t)=>{mapEnsure(h,e,(()=>[a,0]))[1]=t,l[4]?.(e,t)},Ue=(e,a,t,o,s)=>(arrayPush(mapEnsure(mapEnsure(mapEnsure(E,e,mapNew),a,mapNew),t,(()=>[])),o),s),Re=(e,a,t)=>(arrayPush(mapEnsure(u,e,(()=>[])),a),t),Ae=(e,a,t)=>ifNotUndefined(mapGet(mapGet(mapGet(d,e),a),t),(([e,a])=>[!0,e,a]),(()=>[!1,...pairNew(Je(e,a,t))])),Oe=e=>ifNotUndefined(mapGet(h,e),(([e,a])=>[!0,e,a]),(()=>[!1,...pairNew($e(e))])),He=e=>collIsEmpty(E)||collIsEmpty(P[e])?0:collForEach(e?mapClone3(E):E,((a,t)=>collForEach(a,((a,o)=>collForEach(a,((a,s)=>Q(P[e],[t,o,s],a))))))),De=e=>collIsEmpty(u)||collIsEmpty(W[e])?0:collForEach(e?mapClone(u):u,((a,t)=>Q(W[e],[t],a))),Ve=(e,a,t,o)=>{if(!collIsEmpty(e))return Q(a,o,(()=>mapToObj(e))),mapForEach(e,((e,a)=>Q(t,[...o??[],e],1==a))),1},ze=e=>{const a=Xe();a!=t&&Q(C[e],void 0,a);const o=collIsEmpty(O[e]),s=collIsEmpty(V[e])&&collIsEmpty(z[e])&&collIsEmpty(A[e])&&collIsEmpty(H[e])&&collIsEmpty(L[e])&&collIsEmpty(U[e])&&collIsEmpty(R[e])&&o&&collIsEmpty(j[e])&&collIsEmpty(M[e]),l=collIsEmpty(v[e])&&collIsEmpty(D[e])&&collIsEmpty(G[e])&&collIsEmpty(F[e]);if(!s||!l){const a=e?[mapClone(r),mapClone2(n),mapClone(i),mapClone2(p),mapClone3(c),mapClone3(d)]:[r,n,i,p,c,d];if(!s){Ve(a[0],j[e],M[e]),collForEach(a[1],((a,t)=>Ve(a,L[e],U[e],[t]))),collForEach(a[2],((a,t)=>{0!=a&&Q(R[e],[t],Be(t))}));const t=setNew();collForEach(a[3],((a,s)=>{Ve(a,A[e],H[e],[s])&&!o&&(Q(O[e],[s,null]),setAdd(t,s))})),o||collForEach(a[5],((a,o)=>{if(!collHas(t,o)){const t=setNew();collForEach(a,(e=>collForEach(e,(([a,o],s)=>o!==a?setAdd(t,s):collDel(e,s))))),collForEach(t,(a=>Q(O[e],[o,a])))}})),collForEach(a[4],((a,t)=>collForEach(a,((a,o)=>Ve(a,V[e],z[e],[t,o])))))}if(!l){let t;collForEach(a[5],((a,o)=>{let s;collForEach(a,((a,l)=>{let r;collForEach(a,(([a,n],i)=>{n!==a&&(Q(v[e],[o,l,i],n,a,Ae),t=s=r=1)})),r&&Q(D[e],[o,l],Ae)})),s&&Q(G[e],[o],Ae)})),t&&Q(F[e],void 0,Ae)}}},ve=e=>{const a=oa();a!=o&&Q(_[e],void 0,a);const t=collIsEmpty(B[e])&&collIsEmpty(x[e]),s=collIsEmpty(k[e])&&collIsEmpty(Y[e]);if(!t||!s){const a=e?[mapClone(m),mapClone(h)]:[m,h];if(t||Ve(a[0],B[e],x[e]),!s){let t;collForEach(a[1],(([a,o],s)=>{o!==a&&(Q(k[e],[s],o,a,Oe),t=1)})),t&&Q(Y[e],void 0,Oe)}}},Pe=(e,...a)=>(Sa((()=>e(...arrayMap(a,id)))),Na),We=()=>mapToObj3(b),_e=()=>mapKeys(b),Ye=e=>mapKeys(mapGet(T,id(e))),Be=e=>collSize(mapGet(b,id(e))),xe=e=>mapKeys(mapGet(b,id(e))),ke=(e,a,t,o=0,s)=>arrayMap(slice(arraySort(mapMap(mapGet(b,id(e)),((e,t)=>[isUndefined(a)?t:mapGet(e,id(a)),t])),(([e],[a])=>defaultSorter(e,a)*(t?-1:1))),o,isUndefined(s)?s:o+s),(([,e])=>e)),qe=(e,a)=>mapKeys(mapGet(mapGet(b,id(e)),id(a))),Je=(e,a,t)=>mapGet(mapGet(mapGet(b,id(e)),id(a)),id(t)),Ke=()=>mapToObj(I),Qe=()=>mapKeys(I),$e=e=>mapGet(I,id(e)),Xe=()=>!collIsEmpty(b),Ze=e=>collHas(b,id(e)),ea=(e,a)=>collHas(mapGet(T,id(e)),id(a)),aa=(e,a)=>collHas(mapGet(b,id(e)),id(a)),ta=(e,a,t)=>collHas(mapGet(mapGet(b,id(e)),id(a)),id(t)),oa=()=>!collIsEmpty(I),sa=e=>collHas(I,id(e)),la=e=>Pe((()=>(e=>objValidate(e,ae,Ue))(e)?de(e):0)),ra=e=>Pe((()=>se(e)?ge(e):0)),na=e=>{try{ce(jsonParse(e))}catch{}return Na},ia=a=>Pe((()=>{if((e=objValidate(a,(e=>objValidate(e,Z))))&&(ie(a),!collIsEmpty(b))){const e=We();ca(),la(e)}})),pa=e=>Pe((()=>{if(a=(e=>objValidate(e,Z))(e)){const t=Ke();ua(),ha(),a=!0,pe(e),ra(t)}})),ca=()=>Pe((()=>de({}))),da=e=>Pe((e=>collHas(b,e)?we(e):0),e),ma=(e,a)=>Pe(((e,a)=>ifNotUndefined(mapGet(b,e),(t=>collHas(t,a)?Te(e,t,a):0))),e,a),ha=()=>Pe((()=>ge({}))),Ea=()=>Pe((()=>{ie({}),e=!1})),ua=()=>Pe((()=>{pe({}),a=!1})),Sa=(e,a)=>{if(-1!=s){ga();const t=e();return ya(a),t}},ga=()=>(-1!=s&&s++,1==s&&(l[0]?.(),Q(q)),Na),ya=e=>(s>0&&(s--,0==s&&(s=1,He(1),collIsEmpty(d)||ze(1),De(1),collIsEmpty(h)||ve(1),e?.(Na)&&(collForEach(d,((e,a)=>collForEach(e,((e,t)=>collForEach(e,(([e],o)=>setOrDelCell(Na,a,t,o,e))))))),collClear(d),collForEach(h,(([e],a)=>setOrDelValue(Na,a,e))),collClear(h)),Q(J[0],void 0),s=-1,He(0),collIsEmpty(d)||ze(0),De(0),collIsEmpty(h)||ve(0),l[1]?.(),Q(J[1],void 0),l[2]?.(),s=0,t=Xe(),o=oa(),arrayForEach([r,n,i,p,c,d,E,m,h,u],collClear))),Na),Na={getContent:()=>[We(),Ke()],getTables:We,getTableIds:_e,getTable:e=>mapToObj2(mapGet(b,id(e))),getTableCellIds:Ye,getRowCount:Be,getRowIds:xe,getSortedRowIds:ke,getRow:(e,a)=>mapToObj(mapGet(mapGet(b,id(e)),id(a))),getCellIds:qe,getCell:Je,getValues:Ke,getValueIds:Qe,getValue:$e,hasTables:Xe,hasTable:Ze,hasTableCell:ea,hasRow:aa,hasCell:ta,hasValues:oa,hasValue:sa,getTablesJson:()=>jsonStringWithMap(b),getValuesJson:()=>jsonStringWithMap(I),getJson:()=>jsonStringWithMap([b,I]),getTablesSchemaJson:()=>jsonStringWithMap(S),getValuesSchemaJson:()=>jsonStringWithMap(y),getSchemaJson:()=>jsonStringWithMap([S,y]),hasTablesSchema:()=>e,hasValuesSchema:()=>a,setContent:e=>Pe((()=>ee(e)?(([e,a])=>{(objIsEmpty(e)?ca:la)(e),(objIsEmpty(a)?ha:ra)(a)})(e):0)),setTables:la,setTable:(e,a)=>Pe((e=>ae(a,e)?me(e,a):0),e),setRow:(e,a,t)=>Pe(((e,a)=>te(e,a,t)?he(e,fe(e),a,t):0),e,a),addRow:(e,a,t=!0)=>Sa((()=>{let o;return te(e,o,a)&&(e=id(e),he(e,fe(e),o=Ne(e,t?1:0),a)),o})),setPartialRow:(e,a,t)=>Pe(((e,a)=>{if(te(e,a,t,1)){const o=fe(e);objToArray(t,((t,s)=>ue(e,o,a,s,t)))}}),e,a),setCell:(e,a,t,o)=>Pe(((e,a,t)=>ifNotUndefined(oe(e,a,t,isFunction(o)?o(Je(e,a,t)):o),(o=>ue(e,fe(e),a,t,o)))),e,a,t),setValues:ra,setPartialValues:e=>Pe((()=>se(e,1)?objToArray(e,((e,a)=>ye(a,e))):0)),setValue:(e,a)=>Pe((e=>ifNotUndefined(le(e,isFunction(a)?a($e(e)):a),(a=>ye(e,a)))),e),applyChanges:e=>Pe((()=>{objToArray(e[0],((e,a)=>isUndefined(e)?da(a):objToArray(e,((e,t)=>isUndefined(e)?ma(a,t):objToArray(e,((e,o)=>setOrDelCell(Na,a,t,o,e))))))),objToArray(e[1],((e,a)=>setOrDelValue(Na,a,e)))})),setTablesJson:na,setValuesJson:e=>{try{Se(jsonParse(e))}catch{}return Na},setJson:e=>Pe((()=>{try{const[a,t]=jsonParse(e);ce(a),Se(t)}catch{na(e)}})),setTablesSchema:ia,setValuesSchema:pa,setSchema:(e,a)=>Pe((()=>{ia(e),pa(a)})),delTables:ca,delTable:da,delRow:ma,delCell:(e,a,t,o)=>Pe(((e,a,t)=>ifNotUndefined(mapGet(b,e),(s=>ifNotUndefined(mapGet(s,a),(l=>collHas(l,t)?be(e,s,a,l,t,o):0))))),e,a,t),delValues:ha,delValue:e=>Pe((e=>collHas(I,e)?Ie(e):0),e),delTablesSchema:Ea,delValuesSchema:ua,delSchema:()=>Pe((()=>{Ea(),ua()})),transaction:Sa,startTransaction:ga,getTransactionChanges:()=>[mapToObj(d,((e,a)=>-1===mapGet(r,a)?void 0:mapToObj(e,((e,t)=>-1===mapGet(mapGet(p,a),t)?void 0:mapToObj(e,(([,e])=>e),(e=>pairIsEqual(e)))),collIsEmpty,objIsEmpty)),collIsEmpty,objIsEmpty),mapToObj(h,(([,e])=>e),(e=>pairIsEqual(e))),1],getTransactionLog:()=>[!collIsEmpty(d),!collIsEmpty(h),mapToObj3(d,pairClone,pairIsEqual),mapToObj3(E),mapToObj(h,pairClone,pairIsEqual),mapToObj(u),mapToObj(r),mapToObj2(p),mapToObj3(c),mapToObj(m)],finishTransaction:ya,forEachTable:e=>collForEach(b,((a,t)=>e(t,(e=>collForEach(a,((a,t)=>e(t,(e=>mapForEach(a,e))))))))),forEachTableCell:(e,a)=>mapForEach(mapGet(T,id(e)),a),forEachRow:(e,a)=>collForEach(mapGet(b,id(e)),((e,t)=>a(t,(a=>mapForEach(e,a))))),forEachCell:(e,a,t)=>mapForEach(mapGet(mapGet(b,id(e)),id(a)),t),forEachValue:e=>mapForEach(I,e),addSortedRowIdsListener:(e,a,t,o,s,l,r)=>{let n=ke(e,a,t,o,s);return K((()=>{const r=ke(e,a,t,o,s);arrayIsEqual(r,n)||(n=r,l(Na,e,a,t,o,s,n))}),O[r?1:0],[e,a],[_e])},addStartTransactionListener:e=>K(e,q),addWillFinishTransactionListener:e=>K(e,J[0]),addDidFinishTransactionListener:e=>K(e,J[1]),callListener:e=>(X(e),Na),delListener:e=>($(e),Na),getListenerStats:()=>({hasTables:pairCollSize2(C),tables:pairCollSize2(F),tableIds:pairCollSize2(j),hasTable:pairCollSize2(M),table:pairCollSize2(G),tableCellIds:pairCollSize2(L),hasTableCell:pairCollSize2(U,collSize3),rowCount:pairCollSize2(R),rowIds:pairCollSize2(A),sortedRowIds:pairCollSize2(O),hasRow:pairCollSize2(H,collSize3),row:pairCollSize2(D,collSize3),cellIds:pairCollSize2(V,collSize3),hasCell:pairCollSize2(z,collSize4),cell:pairCollSize2(v,collSize4),invalidCell:pairCollSize2(P,collSize4),hasValues:pairCollSize2(_),values:pairCollSize2(Y),valueIds:pairCollSize2(B),hasValue:pairCollSize2(x),value:pairCollSize2(k),invalidValue:pairCollSize2(W),transaction:collSize2(q)+pairCollSize2(J)}),isMergeable:()=>!1,createStore:createStore,addListener:K,callListeners:Q,setInternalListeners:(e,a,t,o,s)=>l=[e,a,t,o,s]};return objToArray({[HAS+TABLES]:[0,C,[],()=>[Xe()]],[TABLES]:[0,F],[TABLE_IDS]:[0,j],[HAS+TABLE]:[1,M,[_e],e=>[Ze(...e)]],[TABLE]:[1,G,[_e]],TableCellIds:[1,L,[_e]],HasTableCell:[2,U,[_e,Ye],e=>[ea(...e)]],[ROW_COUNT]:[1,R,[_e]],[ROW_IDS]:[1,A,[_e]],[HAS+ROW]:[2,H,[_e,xe],e=>[aa(...e)]],[ROW]:[2,D,[_e,xe]],[CELL_IDS]:[2,V,[_e,xe]],[HAS+CELL]:[3,z,[_e,xe,qe],e=>[ta(...e)]],[CELL]:[3,v,[_e,xe,qe],e=>pairNew(Je(...e))],InvalidCell:[3,P],[HAS+VALUES]:[0,_,[],()=>[oa()]],[VALUES]:[0,Y],[VALUE_IDS]:[0,B],[HAS+VALUE]:[1,x,[Qe],e=>[sa(...e)]],[VALUE]:[1,k,[Qe],e=>pairNew($e(e[0]))],InvalidValue:[1,W]},(([e,a,t,o],s)=>{Na[ADD+s+LISTENER]=(...s)=>K(s[e],a[s[e+1]?1:0],e>0?slice(s,0,e):void 0,t,o)})),objFreeze(Na)},SHIFT36=2**36,SHIFT30=2**30,SHIFT24=2**24,SHIFT18=2**18,SHIFT12=4096,SHIFT6=64,encodeTimeAndCounter=(e,a)=>encode(e/SHIFT36)+encode(e/SHIFT30)+encode(e/SHIFT24)+encode(e/262144)+encode(e/4096)+encode(e/64)+encode(e)+encode(a/262144)+encode(a/4096)+encode(a/64)+encode(a),decodeTimeAndCounter=e=>[decode(e,0)*SHIFT36+decode(e,1)*SHIFT30+decode(e,2)*SHIFT24+262144*decode(e,3)+4096*decode(e,4)+64*decode(e,5)+decode(e,6),262144*decode(e,7)+4096*decode(e,8)+64*decode(e,9)+decode(e,10)],getHlcFunctions=e=>{let a=0,t=-1;const o=ifNotUndefined(e,(e=>{const a=getHash(e);return encode(a/SHIFT24)+encode(a/262144)+encode(a/4096)+encode(a/64)+encode(a)}),(()=>getUniqueId(5))),s=e=>{const o=a,[s,l]=isUndefined(e)||""==e?[0,0]:decodeTimeAndCounter(e);a=mathMax(o,s,GLOBAL.HLC_TIME??Date.now()),t=a==o?a==s?mathMax(t,l):t:a==s?l:-1};return[()=>(s(),encodeTimeAndCounter(a,++t)+o),s]},LISTENER_ARGS={HasTable:1,Table:1,TableCellIds:1,HasTableCell:2,RowCount:1,RowIds:1,SortedRowIds:5,HasRow:2,Row:2,CellIds:2,HasCell:3,Cell:3,HasValue:1,Value:1,InvalidCell:3,InvalidValue:1},newContentStampMap=(e=EMPTY_STRING)=>[stampNewMap(e),stampNewMap(e)],validateMergeableContent=e=>isArray(e)&&2==size(e)&&stampValidate(e[0],(e=>objValidate(e,(e=>stampValidate(e,(e=>objValidate(e,(e=>stampValidate(e,(e=>objValidate(e,(e=>stampValidate(e,isCellOrValueOrNullOrUndefined)),void 0,1)))),void 0,1)))),void 0,1)))&&stampValidate(e[1],(e=>objValidate(e,(e=>stampValidate(e,isCellOrValueOrNullOrUndefined)),void 0,1))),createMergeableStore=e=>{let a=1,t=newContentStampMap(),o=0;const s=mapNew(),l=setNew(),[r,n]=getHlcFunctions(e),i=createStore(),p=e=>{const t=a;return a=0,e(),a=t,E},c=(e,a=0)=>{const o={},s={},[[l,r=EMPTY_STRING,i=0],p]=e,[c,m]=t,[h,E,u]=c;let S=a?i:u,g=r;objForEach(l,(([e,t=EMPTY_STRING,s=0],l)=>{const r=mapEnsure(h,l,stampNewMap),[n,i,p]=r;let c=a?s:p,m=t;objForEach(e,((e,t)=>{const[s,r,i]=d(e,mapEnsure(n,t,stampNewMap),objEnsure(objEnsure(o,l,objNew),t,objNew),a);c^=a?0:(r?hashIdAndHash(t,r):0)^hashIdAndHash(t,i),m=getLatestTime(m,s)})),c^=a?0:replaceTimeHash(i,t),stampUpdate(r,c,t),S^=a?0:(p?hashIdAndHash(l,p):0)^hashIdAndHash(l,r[2]),g=getLatestTime(g,m)})),S^=a?0:replaceTimeHash(E,r),stampUpdate(c,S,r);const[y]=d(p,m,s,a);return n(getLatestTime(g,y)),[o,s,1]},d=(e,a,t,o)=>{const[s,l=EMPTY_STRING,r=0]=e,[n,i,p]=a;let c=l,d=o?r:p;return objForEach(s,(([e,a,s=0],l)=>{const r=mapEnsure(n,l,(()=>[void 0,EMPTY_STRING,0])),[,i,p]=r;(!i||a>i)&&(stampUpdate(r,o?s:getHash(jsonStringWithMap(e??null)+":"+a),a),r[0]=e,t[l]=e,d^=o?0:hashIdAndHash(l,p)^hashIdAndHash(l,r[2]),c=getLatestTime(c,a))})),d^=o?0:replaceTimeHash(i,l),stampUpdate(a,d,l),[c,p,a[2]]},m=()=>[stampMapToObjWithHash(t[0],(e=>stampMapToObjWithHash(e,(e=>stampMapToObjWithHash(e))))),stampMapToObjWithHash(t[1])],h=e=>p((()=>i.applyChanges(c(e)))),E={getMergeableContent:m,getMergeableContentHashes:()=>[t[0][2],t[1][2]],getMergeableTableHashes:()=>mapToObj(t[0][0],getStampHash),getMergeableTableDiff:e=>{const a=stampNewObj(t[0][1]),o={};return mapForEach(t[0][0],((t,[s,l,r])=>objHas(e,t)?r!=e[t]?o[t]=r:0:a[0][t]=stampMapToObjWithoutHash([s,l],(e=>stampMapToObjWithoutHash(e))))),[a,o]},getMergeableRowHashes:e=>{const a={};return objForEach(e,((e,o)=>ifNotUndefined(mapGet(t[0][0],o),(([t,,s])=>s!=e?mapForEach(t,((e,[,,t])=>objEnsure(a,o,objNew)[e]=t)):0)))),a},getMergeableRowDiff:e=>{const a=stampNewObj(t[0][1]),o={};return objForEach(e,((e,s)=>mapForEach(mapGet(t[0][0],s)?.[0],((t,[l,r,n])=>objHas(e,t)?n!==e[t]?objEnsure(o,s,objNew)[t]=n:0:objEnsure(a[0],s,stampNewObj)[0][t]=stampMapToObjWithoutHash([l,r]))))),[a,o]},getMergeableCellHashes:e=>{const a={};return objForEach(e,((e,o)=>ifNotUndefined(mapGet(t[0][0],o),(([t])=>objForEach(e,((e,s)=>ifNotUndefined(mapGet(t,s),(([t,,l])=>l!==e?mapForEach(t,((e,[,,t])=>objEnsure(objEnsure(a,o,objNew),s,objNew)[e]=t)):0)))))))),a},getMergeableCellDiff:e=>{const[[a,o]]=t,s={};return objForEach(e,((e,t)=>objForEach(e,((e,o)=>ifNotUndefined(mapGet(a,t),(([a,l])=>ifNotUndefined(mapGet(a,o),(([a,r])=>mapForEach(a,((a,[n,i,p])=>p!==e[a]?objEnsure(objEnsure(s,t,(()=>stampNewObj(l)))[0],o,(()=>stampNewObj(r)))[0][a]=[n,i]:0)))))))))),newStamp(s,o)},getMergeableValueHashes:()=>mapToObj(t[1][0],getStampHash),getMergeableValueDiff:e=>{const[,[a,o]]=t,s=mapToObj(a,stampCloneWithoutHash,(([,,a],t)=>a==e?.[t]));return newStamp(s,o)},setMergeableContent:e=>p((()=>validateMergeableContent(e)?i.transaction((()=>{i.delTables().delValues(),t=newContentStampMap(),i.applyChanges(c(e,1))})):0)),setDefaultContent:e=>(i.transaction((()=>{o=1,i.setContent(e),o=0})),E),getTransactionMergeableChanges:()=>{const[[e,a],[o,r]]=t,n={};collForEach(s,((a,t)=>ifNotUndefined(mapGet(e,t),(([e,o])=>{const s={};collForEach(a,((a,t)=>ifNotUndefined(mapGet(e,t),(([e,o])=>{const l={};collForEach(a,(a=>{ifNotUndefined(mapGet(e,a),(([e,t])=>l[a]=newStamp(e,t)))})),s[t]=newStamp(l,o)})))),n[t]=newStamp(s,o)}))));const i={};return collForEach(l,(e=>ifNotUndefined(mapGet(o,e),(([a,t])=>i[e]=newStamp(a,t))))),[newStamp(n,a),newStamp(i,r),1]},applyMergeableChanges:h,merge:e=>{const a=m(),t=e.getMergeableContent();return e.applyMergeableChanges(a),h(t)}};return i.setInternalListeners((()=>{}),(()=>{}),(()=>{collClear(s),collClear(l)}),((e,t,l,n)=>{setAdd(mapEnsure(mapEnsure(s,e,mapNew),t,setNew),l),a&&c([[{[e]:[{[t]:[{[l]:[n,o?EMPTY_STRING:r()]}]}]}],[{}],1])}),((e,t)=>{setAdd(l,e),a&&c([[{}],[{[e]:[t,o?EMPTY_STRING:r()]}],1])})),objToArray(i,((e,a)=>E[a]=strStartsWith(a,"set")||strStartsWith(a,"del")||strStartsWith(a,"apply")||strEndsWith(a,"Transaction")||"callListener"==a?(...a)=>(e(...a),E):strStartsWith(a,"add")&&strEndsWith(a,"Listener")?(...t)=>{const o=LISTENER_ARGS[slice(a,3,-8)]??0,s=t[o];return t[o]=(e,...a)=>s(E,...a),e(...t)}:"isMergeable"==a?()=>!0:e)),objFreeze(E)};export{Message,Persists,createCheckpoints,createCustomPersister,createCustomSynchronizer,createIndexes,createMergeableStore,createMetrics,createQueries,createRelationships,createStore,defaultSorter,getUniqueId};
//# sourceMappingURL=/sm/67931e75ae48075182e4b5b6e390d42fab0afea62a6601e5e3b903018c285c1d.map