@kition/kit-table-ui 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ var e=/* @__PURE__ */ ((e,t)=>()=>(t||(e((t={exports:{}}).exports,t,t.exports),e=null),t.exports))(((e,t)=>{let n=[`Map`,`List`,`MovableList`,`Text`,`Tree`,`Counter`];function r(e,t){if(typeof e!=`object`||!e||Array.isArray(e))throw Error(`Invalid toContainerTree options`);for(let n of Object.keys(e))if(n!==`text`&&!(t&&n===`roots`))throw Error(`Unknown toContainerTree option: `+n);if(t&&e.roots!==void 0&&!Array.isArray(e.roots))throw Error(`roots must be an array`);return e}function i(e,t,n){return{cid:e.cid,start:t,totalLength:n,items:e.value}}function a(e,t,r){return{type:n[e],cid:t,value:r}}function o(e,t,r){return`cid:`+t+`@`+e+`:`+n[r]}function s(e,t){return`cid:root-`+e+`:`+n[t]}function c(e){return{type:`Value`,value:e}}function l(e){return e.slice()}function u(e,t,n){Object.defineProperty(e,t,{value:n,enumerable:!0,writable:!0,configurable:!0})}function d(e,t,n){Object.defineProperty(e,t,{value:n,enumerable:!0,writable:!0,configurable:!0})}let f,p=Array(128).fill(void 0);p.push(void 0,null,!0,!1);function m(e){return p[e]}let h=0,g=null;function _(){return(g===null||g.byteLength===0)&&(g=new Uint8Array(f.memory.buffer)),g}let ee=typeof TextEncoder<`u`?new TextEncoder(`utf-8`):{encode:()=>{throw Error(`TextEncoder not available`)}},te=typeof ee.encodeInto==`function`?function(e,t){return ee.encodeInto(e,t)}:function(e,t){let n=ee.encode(e);return t.set(n),{read:e.length,written:n.length}};function v(e,t,n){if(n===void 0){let n=ee.encode(e),r=t(n.length,1)>>>0;return _().subarray(r,r+n.length).set(n),h=n.length,r}let r=e.length,i=t(r,1)>>>0,a=_(),o=0;for(;o<r;o++){let t=e.charCodeAt(o);if(t>127)break;a[i+o]=t}if(o!==r){o!==0&&(e=e.slice(o)),i=n(i,r,r=o+e.length*3,1)>>>0;let t=_().subarray(i+o,i+r),a=te(e,t);o+=a.written,i=n(i,r,o,1)>>>0}return h=o,i}let ne=null;function y(){return(ne===null||ne.buffer.detached===!0||ne.buffer.detached===void 0&&ne.buffer!==f.memory.buffer)&&(ne=new DataView(f.memory.buffer)),ne}let re=p.length;function b(e){re===p.length&&p.push(p.length+1);let t=re;return re=p[t],p[t]=e,t}function x(e,t){try{return e.apply(this,t)}catch(e){f.__wbindgen_exn_store(b(e))}}let ie=typeof TextDecoder<`u`?new TextDecoder(`utf-8`,{ignoreBOM:!0,fatal:!0}):{decode:()=>{throw Error(`TextDecoder not available`)}};typeof TextDecoder<`u`&&ie.decode();function S(e,t){return e>>>=0,ie.decode(_().subarray(e,e+t))}function ae(e){e<132||(p[e]=re,re=e)}function C(e){let t=m(e);return ae(e),t}function oe(e,t){return e>>>=0,_().subarray(e/1,e/1+t)}function w(e){return e==null}let se=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>{f.__wbindgen_export_4.get(e.dtor)(e.a,e.b)});function ce(e,t,n,r){let i={a:e,b:t,cnt:1,dtor:n},a=(...e)=>{i.cnt++;let t=i.a;i.a=0;try{return r(t,i.b,...e)}finally{--i.cnt===0?(f.__wbindgen_export_4.get(i.dtor)(t,i.b),se.unregister(i)):i.a=t}};return a.original=i,se.register(a,i,i),a}function le(e){let t=typeof e;if(t==`number`||t==`boolean`||e==null)return`${e}`;if(t==`string`)return`"${e}"`;if(t==`symbol`){let t=e.description;return t==null?`Symbol`:`Symbol(${t})`}if(t==`function`){let t=e.name;return typeof t==`string`&&t.length>0?`Function(${t})`:`Function`}if(Array.isArray(e)){let t=e.length,n=`[`;t>0&&(n+=le(e[0]));for(let r=1;r<t;r++)n+=`, `+le(e[r]);return n+=`]`,n}let n=/\[object ([^\]]+)\]/.exec(toString.call(e)),r;if(n&&n.length>1)r=n[1];else return toString.call(e);if(r==`Object`)try{return`Object(`+JSON.stringify(e)+`)`}catch{return`Object`}return e instanceof Error?`${e.name}: ${e.message}
2
+ ${e.stack}`:r}function ue(e,t){let n=t(e.length*1,1)>>>0;return _().set(e,n/1),h=e.length,n}function de(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=ue(e,f.__wbindgen_malloc),o=h;f.decodeImportBlobMeta(i,a,o,t);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}function fe(){f.callPendingEvents()}function pe(e){let t=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),n=h;return f.isValidContainerId(t,n)!==0}function me(e,t){let n=t(e.length*4,4)>>>0,r=y();for(let t=0;t<e.length;t++)r.setUint32(n+4*t,b(e[t]),!0);return h=e.length,n}let T=128;function E(e){if(T==1)throw Error(`out of js stack`);return p[--T]=e,T}function he(e,t){if(!(e instanceof t))throw Error(`expected instance of ${t.name}`)}function ge(e,t){e>>>=0;let n=y(),r=[];for(let i=e;i<e+4*t;i+=4)r.push(C(n.getUint32(i,!0)));return r}function _e(e,t,n){f._dyn_core_ed718c3d60ebd546___ops__function__FnMut_______Output______as_wasm_bindgen_cfd5deec16e3bd1c___closure__WasmClosure___describe__invoke___wasm_bindgen_cfd5deec16e3bd1c___JsValue_____(e,t,b(n))}function ve(e,t){f._dyn_core_ed718c3d60ebd546___ops__function__FnMut_____Output______as_wasm_bindgen_cfd5deec16e3bd1c___closure__WasmClosure___describe__invoke______(e,t)}typeof FinalizationRegistry>`u`||new FinalizationRegistry(e=>f.__wbg_awarenesswasm_free(e>>>0,1));let ye=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_changemodifier_free(e>>>0,1));var be=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,ye.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,ye.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_changemodifier_free(e,0)}setMessage(t){let n=v(t,f.__wbindgen_malloc,f.__wbindgen_realloc),r=h,i=f.changemodifier_setMessage(this.__wbg_ptr,n,r);return e.__wrap(i)}setTimestamp(t){let n=f.changemodifier_setTimestamp(this.__wbg_ptr,t);return e.__wrap(n)}};let xe=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_cursor_free(e>>>0,1));var Se=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,xe.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,xe.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_cursor_free(e,0)}containerId(){return C(f.cursor_containerId(this.__wbg_ptr))}pos(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.cursor_pos(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}kind(){return C(f.cursor_kind(this.__wbg_ptr))}side(){return C(f.cursor_side(this.__wbg_ptr))}static decode(t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=ue(t,f.__wbindgen_malloc),o=h;f.cursor_decode(i,a,o);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return e.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}encode(){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.cursor_encode(r,this.__wbg_ptr);var e=y().getInt32(r+0,!0),t=y().getInt32(r+4,!0),n=oe(e,t).slice();return f.__wbindgen_free(e,t*1,1),n}finally{f.__wbindgen_add_to_stack_pointer(16)}}};let Ce=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_ephemeralstorewasm_free(e>>>0,1));var we=class{__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,Ce.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_ephemeralstorewasm_free(e,0)}getAllStates(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.ephemeralstorewasm_getAllStates(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}removeOutdated(){f.ephemeralstorewasm_removeOutdated(this.__wbg_ptr)}subscribeLocalUpdates(e){return C(f.ephemeralstorewasm_subscribeLocalUpdates(this.__wbg_ptr,b(e)))}get(e){let t=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),n=h;return C(f.ephemeralstorewasm_get(this.__wbg_ptr,t,n))}constructor(e){let t=f.ephemeralstorewasm_new(e);return this.__wbg_ptr=t>>>0,Ce.register(this,this.__wbg_ptr,this),this}set(e,t){let n=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),r=h;f.ephemeralstorewasm_set(this.__wbg_ptr,n,r,b(t))}keys(){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.ephemeralstorewasm_keys(r,this.__wbg_ptr);var e=y().getInt32(r+0,!0),t=y().getInt32(r+4,!0),n=ge(e,t).slice();return f.__wbindgen_free(e,t*4,4),n}finally{f.__wbindgen_add_to_stack_pointer(16)}}apply(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16),r=ue(e,f.__wbindgen_malloc),i=h;f.ephemeralstorewasm_apply(n,this.__wbg_ptr,r,i);var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}delete(e){let t=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),n=h;f.ephemeralstorewasm_delete(this.__wbg_ptr,t,n)}encode(e){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),o=h;f.ephemeralstorewasm_encode(i,this.__wbg_ptr,a,o);var t=y().getInt32(i+0,!0),n=y().getInt32(i+4,!0),r=oe(t,n).slice();return f.__wbindgen_free(t,n*1,1),r}finally{f.__wbindgen_add_to_stack_pointer(16)}}isEmpty(){return f.ephemeralstorewasm_isEmpty(this.__wbg_ptr)!==0}encodeAll(){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.ephemeralstorewasm_encodeAll(r,this.__wbg_ptr);var e=y().getInt32(r+0,!0),t=y().getInt32(r+4,!0),n=oe(e,t).slice();return f.__wbindgen_free(e,t*1,1),n}finally{f.__wbindgen_add_to_stack_pointer(16)}}subscribe(e){return C(f.ephemeralstorewasm_subscribe(this.__wbg_ptr,b(e)))}};let Te=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_lorocounter_free(e>>>0,1));var Ee=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,Te.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,Te.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_lorocounter_free(e,0)}toContainerTree(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorocounter_toContainerTree(r,this.__wbg_ptr,w(e)?0:b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}isAttached(){return f.lorocounter_isAttached(this.__wbg_ptr)!==0}getAttached(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorocounter_getAttached(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getShallowValue(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorocounter_getShallowValue(n,this.__wbg_ptr);var e=y().getFloat64(n+0,!0),t=y().getInt32(n+8,!0);if(y().getInt32(n+12,!0))throw C(t);return e}finally{f.__wbindgen_add_to_stack_pointer(16)}}get id(){return C(f.lorocounter_id(this.__wbg_ptr))}constructor(){let e=f.lorocounter_new();return this.__wbg_ptr=e>>>0,Te.register(this,this.__wbg_ptr,this),this}kind(){return C(f.lorocounter_kind(this.__wbg_ptr))}parent(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorocounter_parent(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toJSON(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorocounter_getShallowValue(n,this.__wbg_ptr);var e=y().getFloat64(n+0,!0),t=y().getInt32(n+8,!0);if(y().getInt32(n+12,!0))throw C(t);return e}finally{f.__wbindgen_add_to_stack_pointer(16)}}decrement(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorocounter_decrement(n,this.__wbg_ptr,e);var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}get value(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorocounter_getShallowValue(n,this.__wbg_ptr);var e=y().getFloat64(n+0,!0),t=y().getInt32(n+8,!0);if(y().getInt32(n+12,!0))throw C(t);return e}finally{f.__wbindgen_add_to_stack_pointer(16)}}increment(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorocounter_increment(n,this.__wbg_ptr,e);var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}subscribe(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorocounter_subscribe(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}};let De=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_lorodoc_free(e>>>0,1));var Oe=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,De.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,De.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_lorodoc_free(e,0)}applyDiff(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_applyDiff(r,this.__wbg_ptr,b(e),w(t)?0:b(t));var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}isShallow(){return f.lorodoc_isShallow(this.__wbg_ptr)!==0}changeCount(){return f.lorodoc_changeCount(this.__wbg_ptr)>>>0}getByPath(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.lorodoc_getByPath(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getCounter(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getCounter(r,this.__wbg_ptr,E(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Ee.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}isDetached(){return f.lorodoc_isDetached(this.__wbg_ptr)!==0}get peerIdStr(){return C(f.lorodoc_peerIdStr(this.__wbg_ptr))}setPeerId(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_setPeerId(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getCursorPos(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);he(e,Se),f.lorodoc_getCursorPos(r,this.__wbg_ptr,e.__wbg_ptr);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}hasContainer(e){return f.lorodoc_hasContainer(this.__wbg_ptr,b(e))!==0}importBatch(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_importBatch(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}cmpFrontiers(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=me(e,f.__wbindgen_malloc),o=h,s=me(t,f.__wbindgen_malloc),c=h;f.lorodoc_cmpFrontiers(i,this.__wbg_ptr,a,o,s,c);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}debugHistory(){f.lorodoc_debugHistory(this.__wbg_ptr)}static fromSnapshot(t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=ue(t,f.__wbindgen_malloc),o=h;f.lorodoc_fromSnapshot(i,a,o);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return e.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getChangeAt(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getChangeAt(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}oplogVersion(){let e=f.lorodoc_oplogVersion(this.__wbg_ptr);return We.__wrap(e)}getMovableList(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getMovableList(r,this.__wbg_ptr,E(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Pe.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}frontiersToVV(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=me(e,f.__wbindgen_malloc),a=h;f.lorodoc_frontiersToVV(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return We.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getAllChanges(){return C(f.lorodoc_getAllChanges(this.__wbg_ptr))}oplogFrontiers(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_oplogFrontiers(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}vvToFrontiers(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);he(e,We),f.lorodoc_vvToFrontiers(r,this.__wbg_ptr,e.__wbg_ptr);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}shallowSinceVV(){let e=f.lorodoc_shallowSinceVV(this.__wbg_ptr);return We.__wrap(e)}configTextStyle(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_configTextStyle(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getOpsInChange(e){try{let a=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getOpsInChange(a,this.__wbg_ptr,b(e));var t=y().getInt32(a+0,!0),n=y().getInt32(a+4,!0),r=y().getInt32(a+8,!0);if(y().getInt32(a+12,!0))throw C(r);var i=ge(t,n).slice();return f.__wbindgen_free(t,n*4,4),i}finally{f.__wbindgen_add_to_stack_pointer(16)}}getShallowValue(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getShallowValue(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}checkoutToLatest(){try{let t=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_checkoutToLatest(t,this.__wbg_ptr);var e=y().getInt32(t+0,!0);if(y().getInt32(t+4,!0))throw C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}cmpWithFrontiers(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=me(e,f.__wbindgen_malloc),a=h;f.lorodoc_cmpWithFrontiers(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return t}finally{f.__wbindgen_add_to_stack_pointer(16)}}exportJsonInIdSpan(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_exportJsonInIdSpan(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}subscribeJsonpath(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),o=h;f.lorodoc_subscribeJsonpath(i,this.__wbg_ptr,a,o,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}deleteRootContainer(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_deleteRootContainer(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}exportJsonUpdates(e,t,n){try{let a=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_exportJsonUpdates(a,this.__wbg_ptr,b(e),b(t),w(n)?16777215:+!!n);var r=y().getInt32(a+0,!0),i=y().getInt32(a+4,!0);if(y().getInt32(a+8,!0))throw C(i);return C(r)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getContainerById(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getContainerById(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getPendingTxnLength(){return f.lorodoc_getPendingTxnLength(this.__wbg_ptr)>>>0}importJsonUpdates(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_importJsonUpdates(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}importUpdateBatch(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_importBatch(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}setDetachedEditing(e){f.lorodoc_setDetachedEditing(this.__wbg_ptr,e)}setRecordTimestamp(e){f.lorodoc_setRecordTimestamp(this.__wbg_ptr,e)}subscribePreCommit(e){return C(f.lorodoc_subscribePreCommit(this.__wbg_ptr,b(e)))}findIdSpansBetween(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=me(e,f.__wbindgen_malloc),o=h,s=me(t,f.__wbindgen_malloc),c=h;f.lorodoc_findIdSpansBetween(i,this.__wbg_ptr,a,o,s,c);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getChangeAtLamport(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),o=h;f.lorodoc_getChangeAtLamport(i,this.__wbg_ptr,a,o,t);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getPathToContainer(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getPathToContainer(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getChangedContainersIn(e,t){try{let o=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getChangedContainersIn(o,this.__wbg_ptr,b(e),t);var n=y().getInt32(o+0,!0),r=y().getInt32(o+4,!0),i=y().getInt32(o+8,!0);if(y().getInt32(o+12,!0))throw C(i);var a=ge(n,r).slice();return f.__wbindgen_free(n,r*4,4),a}finally{f.__wbindgen_add_to_stack_pointer(16)}}getDeepValueWithID(){return C(f.lorodoc_getDeepValueWithID(this.__wbg_ptr))}setNextCommitOrigin(e){let t=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),n=h;f.lorodoc_setNextCommitOrigin(this.__wbg_ptr,t,n)}getUncommittedOpsAsJson(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getUncommittedOpsAsJson(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}setNextCommitMessage(e){let t=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),n=h;f.lorodoc_setNextCommitMessage(this.__wbg_ptr,t,n)}setNextCommitOptions(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_setNextCommitOptions(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}shallowSinceFrontiers(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_shallowSinceFrontiers(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}subscribeLocalUpdates(e){return C(f.lorodoc_subscribeLocalUpdates(this.__wbg_ptr,b(e)))}travelChangeAncestors(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=me(e,f.__wbindgen_malloc),a=h;f.lorodoc_travelChangeAncestors(r,this.__wbg_ptr,i,a,b(t));var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}clearNextCommitOptions(){f.lorodoc_clearNextCommitOptions(this.__wbg_ptr)}configDefaultTextStyle(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_configDefaultTextStyle(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}setChangeMergeInterval(e){f.lorodoc_setChangeMergeInterval(this.__wbg_ptr,e)}setNextCommitTimestamp(e){f.lorodoc_setNextCommitTimestamp(this.__wbg_ptr,e)}setHideEmptyRootContainers(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_setHideEmptyRootContainers(n,this.__wbg_ptr,e);var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}isDetachedEditingEnabled(){return f.lorodoc_isDetachedEditingEnabled(this.__wbg_ptr)!==0}subscribeFirstCommitFromPeer(e){return C(f.lorodoc_subscribeFirstCommitFromPeer(this.__wbg_ptr,b(e)))}constructor(){let e=f.lorodoc_new();return this.__wbg_ptr=e>>>0,De.register(this,this.__wbg_ptr,this),this}diff(e,t,n){try{let a=f.__wbindgen_add_to_stack_pointer(-16),o=me(e,f.__wbindgen_malloc),s=h,c=me(t,f.__wbindgen_malloc),l=h;f.lorodoc_diff(a,this.__wbg_ptr,o,s,c,l,w(n)?16777215:+!!n);var r=y().getInt32(a+0,!0),i=y().getInt32(a+4,!0);if(y().getInt32(a+8,!0))throw C(i);return C(r)}finally{f.__wbindgen_add_to_stack_pointer(16)}}fork(){let t=f.lorodoc_fork(this.__wbg_ptr);return e.__wrap(t)}attach(){f.lorodoc_attach(this.__wbg_ptr)}commit(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_commit(n,this.__wbg_ptr,w(e)?0:b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}detach(){f.lorodoc_detach(this.__wbg_ptr)}export(e){try{let a=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_export(a,this.__wbg_ptr,b(e));var t=y().getInt32(a+0,!0),n=y().getInt32(a+4,!0),r=y().getInt32(a+8,!0);if(y().getInt32(a+12,!0))throw C(r);var i=oe(t,n).slice();return f.__wbindgen_free(t,n*1,1),i}finally{f.__wbindgen_add_to_stack_pointer(16)}}import(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=ue(e,f.__wbindgen_malloc),a=h;f.lorodoc_import(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}forkAt(t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=me(t,f.__wbindgen_malloc),o=h;f.lorodoc_forkAt(i,this.__wbg_ptr,a,o);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return e.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getMap(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getMap(r,this.__wbg_ptr,E(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Me.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}opCount(){return f.lorodoc_opCount(this.__wbg_ptr)>>>0}get peerId(){let e=f.lorodoc_peerId(this.__wbg_ptr);return BigInt.asUintN(64,e)}toJSON(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_toJSON(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}version(){let e=f.lorodoc_version(this.__wbg_ptr);return We.__wrap(e)}checkout(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16),r=me(e,f.__wbindgen_malloc),i=h;f.lorodoc_checkout(n,this.__wbg_ptr,r,i);var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getList(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getList(r,this.__wbg_ptr,E(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Ae.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}getText(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getText(r,this.__wbg_ptr,E(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Ie.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}getTree(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_getTree(r,this.__wbg_ptr,E(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Re.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}frontiers(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_frontiers(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}JSONPath(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.lorodoc_JSONPath(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}revertTo(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16),r=me(e,f.__wbindgen_malloc),i=h;f.lorodoc_revertTo(n,this.__wbg_ptr,r,i);var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}subscribe(e){return C(f.lorodoc_subscribe(this.__wbg_ptr,b(e)))}toContainerTree(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorodoc_toContainerTree(r,this.__wbg_ptr,w(e)?0:b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}};let ke=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_lorolist_free(e>>>0,1));var Ae=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,ke.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,ke.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_lorolist_free(e,0)}isAttached(){return f.lorolist_isAttached(this.__wbg_ptr)!==0}getAttached(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_getAttached(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}pushContainer(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_pushContainer(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getRangeValue(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_getRangeValue(i,this.__wbg_ptr,e,t);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}insertContainer(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_insertContainer(i,this.__wbg_ptr,e,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getShallowValue(){return C(f.lorolist_getShallowValue(this.__wbg_ptr))}getDeepValueWithID(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_getDeepValueWithID(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getRangeDeepValueWithID(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_getRangeDeepValueWithID(i,this.__wbg_ptr,e,t);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}get id(){return C(f.lorolist_id(this.__wbg_ptr))}get(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_get(r,this.__wbg_ptr,e);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}constructor(){let e=f.lorolist_new();return this.__wbg_ptr=e>>>0,ke.register(this,this.__wbg_ptr,this),this}pop(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_pop(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}kind(){return C(f.lorolist_kind(this.__wbg_ptr))}push(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_push(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}clear(){try{let t=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_clear(t,this.__wbg_ptr);var e=y().getInt32(t+0,!0);if(y().getInt32(t+4,!0))throw C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}delete(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_delete(r,this.__wbg_ptr,e,t);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}insert(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_insert(r,this.__wbg_ptr,e,b(t));var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}get length(){return f.lorolist_length(this.__wbg_ptr)>>>0}parent(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_parent(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getIdAt(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_getIdAt(r,this.__wbg_ptr,e);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toJSON(){return C(f.lorolist_toJSON(this.__wbg_ptr))}toArray(){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_toArray(i,this.__wbg_ptr);var e=y().getInt32(i+0,!0),t=y().getInt32(i+4,!0),n=y().getInt32(i+8,!0);if(y().getInt32(i+12,!0))throw C(n);var r=ge(e,t).slice();return f.__wbindgen_free(e,t*4,4),r}finally{f.__wbindgen_add_to_stack_pointer(16)}}getCursor(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_getCursor(i,this.__wbg_ptr,e,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return n===0?void 0:Se.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}isDeleted(){return f.lorolist_isDeleted(this.__wbg_ptr)!==0}subscribe(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_subscribe(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toContainerTree(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_toContainerTree(r,this.__wbg_ptr,w(e)?0:b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toContainerTreeSlice(e,t,n){try{let a=f.__wbindgen_add_to_stack_pointer(-16);f.lorolist_toContainerTreeSlice(a,this.__wbg_ptr,e,t,w(n)?0:b(n));var r=y().getInt32(a+0,!0),i=y().getInt32(a+4,!0);if(y().getInt32(a+8,!0))throw C(i);return C(r)}finally{f.__wbindgen_add_to_stack_pointer(16)}}};let je=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_loromap_free(e>>>0,1));var Me=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,je.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,je.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_loromap_free(e,0)}isAttached(){return f.loromap_isAttached(this.__wbg_ptr)!==0}getAttached(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.loromap_getAttached(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getLastEditor(e){let t=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),n=h;return C(f.loromap_getLastEditor(this.__wbg_ptr,t,n))}setContainer(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),o=h;f.loromap_setContainer(i,this.__wbg_ptr,a,o,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getShallowValue(){return C(f.loromap_getShallowValue(this.__wbg_ptr))}ensureMergeableMap(t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=v(t,f.__wbindgen_malloc,f.__wbindgen_realloc),o=h;f.loromap_ensureMergeableMap(i,this.__wbg_ptr,a,o);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return e.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}ensureMergeableList(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.loromap_ensureMergeableList(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Ae.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}ensureMergeableText(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.loromap_ensureMergeableText(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Ie.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}ensureMergeableTree(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.loromap_ensureMergeableTree(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Re.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getDeepValueWithID(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.loromap_getDeepValueWithID(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getOrCreateContainer(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),o=h;f.loromap_getOrCreateContainer(i,this.__wbg_ptr,a,o,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}ensureMergeableCounter(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.loromap_ensureMergeableCounter(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Ee.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}ensureMergeableMovableList(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.loromap_ensureMergeableMovableList(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return Pe.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}get id(){return C(f.loromap_id(this.__wbg_ptr))}get(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.loromap_get(r,this.__wbg_ptr,i,a);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}constructor(){let e=f.loromap_new();return this.__wbg_ptr=e>>>0,je.register(this,this.__wbg_ptr,this),this}keys(){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromap_keys(r,this.__wbg_ptr);var e=y().getInt32(r+0,!0),t=y().getInt32(r+4,!0),n=ge(e,t).slice();return f.__wbindgen_free(e,t*4,4),n}finally{f.__wbindgen_add_to_stack_pointer(16)}}kind(){return C(f.loromap_kind(this.__wbg_ptr))}get size(){return f.loromap_size(this.__wbg_ptr)>>>0}clear(){try{let t=f.__wbindgen_add_to_stack_pointer(-16);f.loromap_clear(t,this.__wbg_ptr);var e=y().getInt32(t+0,!0);if(y().getInt32(t+4,!0))throw C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}delete(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16),r=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),i=h;f.loromap_delete(n,this.__wbg_ptr,r,i);var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}set(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.loromap_set(r,this.__wbg_ptr,i,a,b(t));var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}parent(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.loromap_parent(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}values(){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.loromap_values(i,this.__wbg_ptr);var e=y().getInt32(i+0,!0),t=y().getInt32(i+4,!0),n=y().getInt32(i+8,!0);if(y().getInt32(i+12,!0))throw C(n);var r=ge(e,t).slice();return f.__wbindgen_free(e,t*4,4),r}finally{f.__wbindgen_add_to_stack_pointer(16)}}entries(){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.loromap_entries(i,this.__wbg_ptr);var e=y().getInt32(i+0,!0),t=y().getInt32(i+4,!0),n=y().getInt32(i+8,!0);if(y().getInt32(i+12,!0))throw C(n);var r=ge(e,t).slice();return f.__wbindgen_free(e,t*4,4),r}finally{f.__wbindgen_add_to_stack_pointer(16)}}toJSON(){return C(f.loromap_toJSON(this.__wbg_ptr))}isDeleted(){return f.loromap_isDeleted(this.__wbg_ptr)!==0}subscribe(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromap_subscribe(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toContainerTree(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromap_toContainerTree(r,this.__wbg_ptr,w(e)?0:b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}};let Ne=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_loromovablelist_free(e>>>0,1));var Pe=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,Ne.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,Ne.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_loromovablelist_free(e,0)}isAttached(){return f.loromovablelist_isAttached(this.__wbg_ptr)!==0}getCreatorAt(e){return C(f.loromovablelist_getCreatorAt(this.__wbg_ptr,e))}getAttached(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_getAttached(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}setContainer(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_setContainer(i,this.__wbg_ptr,e,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getLastMoverAt(e){return C(f.loromovablelist_getLastMoverAt(this.__wbg_ptr,e))}pushContainer(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_pushContainer(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getLastEditorAt(e){return C(f.loromovablelist_getLastEditorAt(this.__wbg_ptr,e))}getRangeValue(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_getRangeValue(i,this.__wbg_ptr,e,t);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}insertContainer(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_insertContainer(i,this.__wbg_ptr,e,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getShallowValue(){return C(f.loromovablelist_getShallowValue(this.__wbg_ptr))}getDeepValueWithID(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_getDeepValueWithID(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getRangeDeepValueWithID(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_getRangeDeepValueWithID(i,this.__wbg_ptr,e,t);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}get id(){return C(f.loromovablelist_id(this.__wbg_ptr))}get(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_get(r,this.__wbg_ptr,e);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}move(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_move(r,this.__wbg_ptr,e,t);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}constructor(){let e=f.loromovablelist_new();return this.__wbg_ptr=e>>>0,Ne.register(this,this.__wbg_ptr,this),this}pop(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_pop(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}set(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_set(r,this.__wbg_ptr,e,b(t));var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}kind(){return C(f.loromovablelist_kind(this.__wbg_ptr))}push(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_push(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}clear(){try{let t=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_clear(t,this.__wbg_ptr);var e=y().getInt32(t+0,!0);if(y().getInt32(t+4,!0))throw C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}delete(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_delete(r,this.__wbg_ptr,e,t);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}insert(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_insert(r,this.__wbg_ptr,e,b(t));var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}get length(){return f.loromovablelist_length(this.__wbg_ptr)>>>0}parent(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_parent(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toJSON(){return C(f.loromovablelist_toJSON(this.__wbg_ptr))}toArray(){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_toArray(i,this.__wbg_ptr);var e=y().getInt32(i+0,!0),t=y().getInt32(i+4,!0),n=y().getInt32(i+8,!0);if(y().getInt32(i+12,!0))throw C(n);var r=ge(e,t).slice();return f.__wbindgen_free(e,t*4,4),r}finally{f.__wbindgen_add_to_stack_pointer(16)}}getCursor(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_getCursor(i,this.__wbg_ptr,e,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return n===0?void 0:Se.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}isDeleted(){return f.loromovablelist_isDeleted(this.__wbg_ptr)!==0}subscribe(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_subscribe(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toContainerTree(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_toContainerTree(r,this.__wbg_ptr,w(e)?0:b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toContainerTreeSlice(e,t,n){try{let a=f.__wbindgen_add_to_stack_pointer(-16);f.loromovablelist_toContainerTreeSlice(a,this.__wbg_ptr,e,t,w(n)?0:b(n));var r=y().getInt32(a+0,!0),i=y().getInt32(a+4,!0);if(y().getInt32(a+8,!0))throw C(i);return C(r)}finally{f.__wbindgen_add_to_stack_pointer(16)}}};let Fe=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_lorotext_free(e>>>0,1));var Ie=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,Fe.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,Fe.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_lorotext_free(e,0)}applyDelta(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_applyDelta(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}convertPos(e,t,n){let r=v(t,f.__wbindgen_malloc,f.__wbindgen_realloc),i=h,a=v(n,f.__wbindgen_malloc,f.__wbindgen_realloc),o=h;return C(f.lorotext_convertPos(this.__wbg_ptr,e,r,i,a,o))}deleteUtf8(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_deleteUtf8(r,this.__wbg_ptr,e,t);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getEditorOf(e){return C(f.lorotext_getEditorOf(this.__wbg_ptr,e))}insertUtf8(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(t,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.lorotext_insertUtf8(r,this.__wbg_ptr,e,i,a);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}isAttached(){return f.lorotext_isAttached(this.__wbg_ptr)!==0}sliceDelta(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_sliceDelta(i,this.__wbg_ptr,e,t);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getAttached(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_getAttached(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}updateByLine(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.lorotext_updateByLine(r,this.__wbg_ptr,i,a,b(t));var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}sliceDeltaUtf8(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_sliceDeltaUtf8(i,this.__wbg_ptr,e,t);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getShallowValue(){let e,t;try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_getShallowValue(i,this.__wbg_ptr);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);return e=n,t=r,S(n,r)}finally{f.__wbindgen_add_to_stack_pointer(16),f.__wbindgen_free(e,t,1)}}getDeepValueWithID(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_getDeepValueWithID(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}get id(){return C(f.lorotext_id(this.__wbg_ptr))}constructor(){let e=f.lorotext_new();return this.__wbg_ptr=e>>>0,Fe.register(this,this.__wbg_ptr,this),this}iter(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_iter(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}kind(){return C(f.lorotext_kind(this.__wbg_ptr))}mark(e,t,n){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=v(t,f.__wbindgen_malloc,f.__wbindgen_realloc),o=h;f.lorotext_mark(i,this.__wbg_ptr,b(e),a,o,b(n));var r=y().getInt32(i+0,!0);if(y().getInt32(i+4,!0))throw C(r)}finally{f.__wbindgen_add_to_stack_pointer(16)}}push(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16),r=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),i=h;f.lorotext_push(n,this.__wbg_ptr,r,i);var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}slice(e,t){let n,r;try{let u=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_slice(u,this.__wbg_ptr,e,t);var i=y().getInt32(u+0,!0),a=y().getInt32(u+4,!0),o=y().getInt32(u+8,!0),s=y().getInt32(u+12,!0),c=i,l=a;if(s)throw c=0,l=0,C(o);return n=c,r=l,S(c,l)}finally{f.__wbindgen_add_to_stack_pointer(16),f.__wbindgen_free(n,r,1)}}delete(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_delete(r,this.__wbg_ptr,e,t);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}insert(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(t,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.lorotext_insert(r,this.__wbg_ptr,e,i,a);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}get length(){return f.lorotext_length(this.__wbg_ptr)>>>0}parent(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_parent(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}splice(e,t,n){let r,i;try{let d=f.__wbindgen_add_to_stack_pointer(-16),p=v(n,f.__wbindgen_malloc,f.__wbindgen_realloc),m=h;f.lorotext_splice(d,this.__wbg_ptr,e,t,p,m);var a=y().getInt32(d+0,!0),o=y().getInt32(d+4,!0),s=y().getInt32(d+8,!0),c=y().getInt32(d+12,!0),l=a,u=o;if(c)throw l=0,u=0,C(s);return r=l,i=u,S(l,u)}finally{f.__wbindgen_add_to_stack_pointer(16),f.__wbindgen_free(r,i,1)}}unmark(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(t,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.lorotext_unmark(r,this.__wbg_ptr,b(e),i,a);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}update(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16),i=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;f.lorotext_update(r,this.__wbg_ptr,i,a,b(t));var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}charAt(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_charAt(r,this.__wbg_ptr,e);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return String.fromCodePoint(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toJSON(){return C(f.lorotext_toJSON(this.__wbg_ptr))}toDelta(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_toDelta(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getCursor(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_getCursor(i,this.__wbg_ptr,e,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return n===0?void 0:Se.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}isDeleted(){return f.lorotext_isDeleted(this.__wbg_ptr)!==0}subscribe(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_subscribe(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toString(){let e,t;try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_toString(i,this.__wbg_ptr);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);return e=n,t=r,S(n,r)}finally{f.__wbindgen_add_to_stack_pointer(16),f.__wbindgen_free(e,t,1)}}toContainerTree(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotext_toContainerTree(r,this.__wbg_ptr,w(e)?0:b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}};let Le=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_lorotree_free(e>>>0,1));var Re=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,Le.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,Le.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_lorotree_free(e,0)}createNode(e,t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_createNode(i,this.__wbg_ptr,E(e),w(t)?4294967297:t>>>0);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return Be.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}isAttached(){return f.lorotree_isAttached(this.__wbg_ptr)!==0}getAttached(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_getAttached(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getNodeByID(e){try{let t=f.lorotree_getNodeByID(this.__wbg_ptr,E(e));return t===0?void 0:Be.__wrap(t)}finally{p[T++]=void 0}}isNodeDeleted(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_isNodeDeleted(r,this.__wbg_ptr,E(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return t!==0}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}getShallowValue(){return C(f.lorotree_getShallowValue(this.__wbg_ptr))}getDeepValueWithID(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_getDeepValueWithID(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}enableFractionalIndex(e){f.lorotree_enableFractionalIndex(this.__wbg_ptr,e)}disableFractionalIndex(){f.lorotree_disableFractionalIndex(this.__wbg_ptr)}isFractionalIndexEnabled(){return f.lorotree_isFractionalIndexEnabled(this.__wbg_ptr)!==0}get id(){return C(f.lorotree_id(this.__wbg_ptr))}move(e,t,n){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_move(i,this.__wbg_ptr,E(e),E(t),w(n)?4294967297:n>>>0);var r=y().getInt32(i+0,!0);if(y().getInt32(i+4,!0))throw C(r)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0,p[T++]=void 0}}constructor(){let e=f.lorotree_new();return this.__wbg_ptr=e>>>0,Le.register(this,this.__wbg_ptr,this),this}kind(){return C(f.lorotree_kind(this.__wbg_ptr))}nodes(){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_nodes(r,this.__wbg_ptr);var e=y().getInt32(r+0,!0),t=y().getInt32(r+4,!0),n=ge(e,t).slice();return f.__wbindgen_free(e,t*4,4),n}finally{f.__wbindgen_add_to_stack_pointer(16)}}roots(){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_roots(r,this.__wbg_ptr);var e=y().getInt32(r+0,!0),t=y().getInt32(r+4,!0),n=ge(e,t).slice();return f.__wbindgen_free(e,t*4,4),n}finally{f.__wbindgen_add_to_stack_pointer(16)}}delete(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_delete(n,this.__wbg_ptr,E(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}parent(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_parent(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toJSON(){return C(f.lorotree_toJSON(this.__wbg_ptr))}has(e){try{return f.lorotree_has(this.__wbg_ptr,E(e))!==0}finally{p[T++]=void 0}}toArray(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_toArray(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getNodes(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_getNodes(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}isDeleted(){return f.lorotree_isDeleted(this.__wbg_ptr)!==0}subscribe(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_subscribe(r,this.__wbg_ptr,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toContainerTree(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotree_toContainerTree(r,this.__wbg_ptr,w(e)?0:b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}};let ze=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_lorotreenode_free(e>>>0,1));var Be=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,ze.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,ze.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_lorotreenode_free(e,0)}__getClassname(){let e,t;try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode___getClassname(i,this.__wbg_ptr);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);return e=n,t=r,S(n,r)}finally{f.__wbindgen_add_to_stack_pointer(16),f.__wbindgen_free(e,t,1)}}creationId(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_creationId(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}isDeleted(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_isDeleted(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return e!==0}finally{f.__wbindgen_add_to_stack_pointer(16)}}moveBefore(t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);he(t,e),f.lorotreenode_moveBefore(r,this.__wbg_ptr,t.__wbg_ptr);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}createNode(t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_createNode(i,this.__wbg_ptr,w(t)?4294967297:t>>>0);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return e.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}getLastMoveId(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_getLastMoveId(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toJSON(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_toJSON(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}fractionalIndex(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_fractionalIndex(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}get id(){return C(f.lorotreenode_id(this.__wbg_ptr))}move(e,t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_move(r,this.__wbg_ptr,E(e),w(t)?4294967297:t>>>0);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16),p[T++]=void 0}}get data(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_data(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return Me.__wrap(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}index(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_index(n,this.__wbg_ptr);var e=y().getFloat64(n+0,!0),t=y().getInt32(n+8,!0);if(y().getInt32(n+12,!0))throw C(t);return e===4294967297?void 0:e}finally{f.__wbindgen_add_to_stack_pointer(16)}}parent(){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.lorotreenode_parent(r,this.__wbg_ptr);var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return t===0?void 0:e.__wrap(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}creator(){return C(f.lorotreenode_creator(this.__wbg_ptr))}children(){return C(f.lorotreenode_children(this.__wbg_ptr))}moveAfter(t){try{let r=f.__wbindgen_add_to_stack_pointer(-16);he(t,e),f.lorotreenode_moveAfter(r,this.__wbg_ptr,t.__wbg_ptr);var n=y().getInt32(r+0,!0);if(y().getInt32(r+4,!0))throw C(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}};let Ve=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_undomanager_free(e>>>0,1));var He=class{__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,Ve.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_undomanager_free(e,0)}groupStart(){try{let t=f.__wbindgen_add_to_stack_pointer(-16);f.undomanager_groupStart(t,this.__wbg_ptr);var e=y().getInt32(t+0,!0);if(y().getInt32(t+4,!0))throw C(e)}finally{f.__wbindgen_add_to_stack_pointer(16)}}topRedoValue(){return C(f.undomanager_topRedoValue(this.__wbg_ptr))}topUndoValue(){return C(f.undomanager_topUndoValue(this.__wbg_ptr))}setMaxUndoSteps(e){f.undomanager_setMaxUndoSteps(this.__wbg_ptr,e)}setMergeInterval(e){f.undomanager_setMergeInterval(this.__wbg_ptr,e)}addExcludeOriginPrefix(e){let t=v(e,f.__wbindgen_malloc,f.__wbindgen_realloc),n=h;f.undomanager_addExcludeOriginPrefix(this.__wbg_ptr,t,n)}constructor(e,t){he(e,Oe);let n=f.undomanager_new(e.__wbg_ptr,b(t));return this.__wbg_ptr=n>>>0,Ve.register(this,this.__wbg_ptr,this),this}peer(){return C(f.undomanager_peer(this.__wbg_ptr))}redo(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.undomanager_redo(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return e!==0}finally{f.__wbindgen_add_to_stack_pointer(16)}}undo(){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.undomanager_undo(n,this.__wbg_ptr);var e=y().getInt32(n+0,!0),t=y().getInt32(n+4,!0);if(y().getInt32(n+8,!0))throw C(t);return e!==0}finally{f.__wbindgen_add_to_stack_pointer(16)}}clear(){f.undomanager_clear(this.__wbg_ptr)}pause(){f.undomanager_pause(this.__wbg_ptr)}resume(){f.undomanager_resume(this.__wbg_ptr)}canRedo(){return f.undomanager_canRedo(this.__wbg_ptr)!==0}canUndo(){return f.undomanager_canUndo(this.__wbg_ptr)!==0}groupEnd(){f.undomanager_groupEnd(this.__wbg_ptr)}isPaused(){return f.undomanager_isPaused(this.__wbg_ptr)!==0}setOnPop(e){f.undomanager_setOnPop(this.__wbg_ptr,b(e))}clearRedo(){f.undomanager_clearRedo(this.__wbg_ptr)}clearUndo(){f.undomanager_clearUndo(this.__wbg_ptr)}setOnPush(e){f.undomanager_setOnPush(this.__wbg_ptr,b(e))}};let Ue=typeof FinalizationRegistry>`u`?{register:()=>{},unregister:()=>{}}:new FinalizationRegistry(e=>f.__wbg_versionvector_free(e>>>0,1));var We=class e{static __wrap(t){t>>>=0;let n=Object.create(e.prototype);return n.__wbg_ptr=t,Ue.register(n,n.__wbg_ptr,n),n}__destroy_into_raw(){let e=this.__wbg_ptr;return this.__wbg_ptr=0,Ue.unregister(this),e}free(){let e=this.__destroy_into_raw();f.__wbg_versionvector_free(e,0)}get(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.versionvector_get(r,this.__wbg_ptr,b(e));var t=y().getFloat64(r+0,!0),n=y().getInt32(r+8,!0);if(y().getInt32(r+12,!0))throw C(n);return t===4294967297?void 0:t}finally{f.__wbindgen_add_to_stack_pointer(16)}}constructor(e){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.versionvector_new(r,b(e));var t=y().getInt32(r+0,!0),n=y().getInt32(r+4,!0);if(y().getInt32(r+8,!0))throw C(n);return this.__wbg_ptr=t>>>0,Ue.register(this,this.__wbg_ptr,this),this}finally{f.__wbindgen_add_to_stack_pointer(16)}}static decode(t){try{let i=f.__wbindgen_add_to_stack_pointer(-16),a=ue(t,f.__wbindgen_malloc),o=h;f.versionvector_decode(i,a,o);var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return e.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}encode(){try{let r=f.__wbindgen_add_to_stack_pointer(-16);f.versionvector_encode(r,this.__wbg_ptr);var e=y().getInt32(r+0,!0),t=y().getInt32(r+4,!0),n=oe(e,t).slice();return f.__wbindgen_free(e,t*1,1),n}finally{f.__wbindgen_add_to_stack_pointer(16)}}length(){return f.versionvector_length(this.__wbg_ptr)>>>0}remove(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.versionvector_remove(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}setEnd(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.versionvector_setEnd(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}compare(t){he(t,e);let n=f.versionvector_compare(this.__wbg_ptr,t.__wbg_ptr);return n===4294967297?void 0:n}setLast(e){try{let n=f.__wbindgen_add_to_stack_pointer(-16);f.versionvector_setLast(n,this.__wbg_ptr,b(e));var t=y().getInt32(n+0,!0);if(y().getInt32(n+4,!0))throw C(t)}finally{f.__wbindgen_add_to_stack_pointer(16)}}toJSON(){return C(f.versionvector_toJSON(this.__wbg_ptr))}static parseJSON(t){try{let i=f.__wbindgen_add_to_stack_pointer(-16);f.versionvector_parseJSON(i,b(t));var n=y().getInt32(i+0,!0),r=y().getInt32(i+4,!0);if(y().getInt32(i+8,!0))throw C(r);return e.__wrap(n)}finally{f.__wbindgen_add_to_stack_pointer(16)}}};async function Ge(e,t){if(typeof Response==`function`&&e instanceof Response){if(typeof WebAssembly.instantiateStreaming==`function`)try{return await WebAssembly.instantiateStreaming(e,t)}catch(t){if(e.headers.get(`Content-Type`)!=`application/wasm`)console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",t);else throw t}let n=await e.arrayBuffer();return await WebAssembly.instantiate(n,t)}{let n=await WebAssembly.instantiate(e,t);return n instanceof WebAssembly.Instance?{instance:n,module:e}:n}}function Ke(){let e={};return e.wbg={},e.wbg.__wbg_String_8f0eb39a4a4c2f66=function(e,t){let n=v(String(m(t)),f.__wbindgen_malloc,f.__wbindgen_realloc),r=h;y().setInt32(e+4,r,!0),y().setInt32(e+0,n,!0)},e.wbg.__wbg_apply_36be6a55257c99bf=function(){return x(function(e,t,n){return b(m(e).apply(m(t),m(n)))},arguments)},e.wbg.__wbg_apply_eb9e9b97497f91e4=function(){return x(function(e,t,n){return b(Reflect.apply(m(e),m(t),m(n)))},arguments)},e.wbg.__wbg_buffer_609cc3eee51ed158=function(e){let t=m(e).buffer;return b(t)},e.wbg.__wbg_call_672a4d21634d4a24=function(){return x(function(e,t){return b(m(e).call(m(t)))},arguments)},e.wbg.__wbg_call_7cccdd69e0791ae2=function(){return x(function(e,t,n){return b(m(e).call(m(t),m(n)))},arguments)},e.wbg.__wbg_call_833bed5770ea2041=function(){return x(function(e,t,n,r){return b(m(e).call(m(t),m(n),m(r)))},arguments)},e.wbg.__wbg_call_b8adc8b1d0a0d8eb=function(){return x(function(e,t,n,r,i){return b(m(e).call(m(t),m(n),m(r),m(i)))},arguments)},e.wbg.__wbg_changemodifier_new=function(e){return b(be.__wrap(e))},e.wbg.__wbg_crypto_574e78ad8b13b65f=function(e){let t=m(e).crypto;return b(t)},e.wbg.__wbg_cursor_new=function(e){return b(Se.__wrap(e))},e.wbg.__wbg_done_769e5ede4b31c67b=function(e){return m(e).done},e.wbg.__wbg_entries_3265d4158b33e5dc=function(e){return b(Object.entries(m(e)))},e.wbg.__wbg_entries_c8a90a7ed73e84ce=function(e){return b(m(e).entries())},e.wbg.__wbg_error_1d6f2b3f2c5a33ec=function(e,t){console.error(S(e,t))},e.wbg.__wbg_from_2a5d3e218e67aa85=function(e){return b(Array.from(m(e)))},e.wbg.__wbg_getOwnPropertySymbols_97eebed6fe6e08be=function(e){return b(Object.getOwnPropertySymbols(m(e)))},e.wbg.__wbg_getRandomValues_b8f5dbd5f3995a9e=function(){return x(function(e,t){m(e).getRandomValues(m(t))},arguments)},e.wbg.__wbg_get_67b2ba62fc30de12=function(){return x(function(e,t){return b(Reflect.get(m(e),m(t)))},arguments)},e.wbg.__wbg_get_b9b93047fe3cf45b=function(e,t){let n=m(e)[t>>>0];return b(n)},e.wbg.__wbg_getindex_5b00c274b05714aa=function(e,t){return m(e)[t>>>0]},e.wbg.__wbg_getwithrefkey_1dc361bd10053bfe=function(e,t){let n=m(e)[m(t)];return b(n)},e.wbg.__wbg_instanceof_ArrayBuffer_e14585432e3737fc=function(e){let t;try{t=m(e)instanceof ArrayBuffer}catch{t=!1}return t},e.wbg.__wbg_instanceof_Map_f3469ce2244d2430=function(e){let t;try{t=m(e)instanceof Map}catch{t=!1}return t},e.wbg.__wbg_instanceof_Object_7f2dcef8f78644a4=function(e){let t;try{t=m(e)instanceof Object}catch{t=!1}return t},e.wbg.__wbg_instanceof_Uint8Array_17156bcf118086a9=function(e){let t;try{t=m(e)instanceof Uint8Array}catch{t=!1}return t},e.wbg.__wbg_isArray_a1eab7e0d067391b=function(e){return Array.isArray(m(e))},e.wbg.__wbg_isSafeInteger_343e2beeeece1bb0=function(e){return Number.isSafeInteger(m(e))},e.wbg.__wbg_iterator_9a24c88df860dc65=function(){return b(Symbol.iterator)},e.wbg.__wbg_length_a446193dc22c12f8=function(e){return m(e).length},e.wbg.__wbg_length_e2d2a49132c1b256=function(e){return m(e).length},e.wbg.__wbg_log_0cc1b7768397bcfe=function(e,t,n,r,i,a,o,s){let c,l;try{c=e,l=t,console.log(S(e,t),S(n,r),S(i,a),S(o,s))}finally{f.__wbindgen_free(c,l,1)}},e.wbg.__wbg_log_cb9e190acc5753fb=function(e,t){let n,r;try{n=e,r=t,console.log(S(e,t))}finally{f.__wbindgen_free(n,r,1)}},e.wbg.__wbg_log_f665cabe523a97df=function(e,t){console.log(S(e,t))},e.wbg.__wbg_lorocounter_new=function(e){return b(Ee.__wrap(e))},e.wbg.__wbg_lorolist_new=function(e){return b(Ae.__wrap(e))},e.wbg.__wbg_loromap_new=function(e){return b(Me.__wrap(e))},e.wbg.__wbg_loromovablelist_new=function(e){return b(Pe.__wrap(e))},e.wbg.__wbg_lorotext_new=function(e){return b(Ie.__wrap(e))},e.wbg.__wbg_lorotree_new=function(e){return b(Re.__wrap(e))},e.wbg.__wbg_lorotreenode_new=function(e){return b(Be.__wrap(e))},e.wbg.__wbg_mark_7438147ce31e9d4b=function(e,t){performance.mark(S(e,t))},e.wbg.__wbg_measure_fb7825c11612c823=function(){return x(function(e,t,n,r){let i,a,o,s;try{i=e,a=t,o=n,s=r,performance.measure(S(e,t),S(n,r))}finally{f.__wbindgen_free(i,a,1),f.__wbindgen_free(o,s,1)}},arguments)},e.wbg.__wbg_msCrypto_a61aeb35a24c1329=function(e){let t=m(e).msCrypto;return b(t)},e.wbg.__wbg_new_405e22f390576ce2=function(){return b({})},e.wbg.__wbg_new_5e0be73521bc8c17=function(){return b(/* @__PURE__ */ new Map)},e.wbg.__wbg_new_78feb108b6472713=function(){return b([])},e.wbg.__wbg_new_a12002a7f91c75be=function(e){return b(new Uint8Array(m(e)))},e.wbg.__wbg_new_c68d7209be747379=function(e,t){return b(Error(S(e,t)))},e.wbg.__wbg_newnoargs_105ed471475aaf50=function(e,t){return b(Function(S(e,t)))},e.wbg.__wbg_newwithbyteoffsetandlength_d97e637ebe145a9a=function(e,t,n){return b(new Uint8Array(m(e),t>>>0,n>>>0))},e.wbg.__wbg_newwithlength_a381634e90c276d4=function(e){return b(new Uint8Array(e>>>0))},e.wbg.__wbg_newwithlength_c4c419ef0bc8a1f8=function(e){return b(Array(e>>>0))},e.wbg.__wbg_next_25feadfc0913fea9=function(e){let t=m(e).next;return b(t)},e.wbg.__wbg_next_6574e1a8a62d1055=function(){return x(function(e){return b(m(e).next())},arguments)},e.wbg.__wbg_node_905d3e251edff8a2=function(e){let t=m(e).node;return b(t)},e.wbg.__wbg_now_454cbad00d40f177=function(){return Date.now()},e.wbg.__wbg_ownKeys_3930041068756f1f=function(){return x(function(e){return b(Reflect.ownKeys(m(e)))},arguments)},e.wbg.__wbg_process_dc0fbacc7c1c06f7=function(e){let t=m(e).process;return b(t)},e.wbg.__wbg_push_737cfc8c1432c2c6=function(e,t){return m(e).push(m(t))},e.wbg.__wbg_randomFillSync_ac0988aba3254290=function(){return x(function(e,t){m(e).randomFillSync(C(t))},arguments)},e.wbg.__wbg_require_60cc747a6bc5215a=function(){return x(function(){let e=t.require;return b(e)},arguments)},e.wbg.__wbg_resolve_4851785c9c5f573d=function(e){return b(Promise.resolve(m(e)))},e.wbg.__wbg_set_37837023f3d740e8=function(e,t,n){m(e)[t>>>0]=C(n)},e.wbg.__wbg_set_3f1d0b984ed272ed=function(e,t,n){m(e)[C(t)]=C(n)},e.wbg.__wbg_set_65595bdd868b3009=function(e,t,n){m(e).set(m(t),n>>>0)},e.wbg.__wbg_set_8fc6bf8a5b1071d1=function(e,t,n){return b(m(e).set(m(t),m(n)))},e.wbg.__wbg_set_bb8cecf6a62b9f46=function(){return x(function(e,t,n){return Reflect.set(m(e),m(t),m(n))},arguments)},e.wbg.__wbg_setindex_dcd71eabf405bde1=function(e,t,n){m(e)[t>>>0]=n},e.wbg.__wbg_stateBinary_9e1da8a4daa74bc6=function(e,t){return b(l(oe(e,t)))},e.wbg.__wbg_stateCid_c43ea762fb462319=function(e,t,n){return b(o(m(e),t,n))},e.wbg.__wbg_stateContainer_cd983f42c6fe987d=function(e,t,n){return b(a(e,m(t),C(n)))},e.wbg.__wbg_stateIndex_1fbac6394e58cff9=function(){return x(function(e,t,n){u(m(e),t>>>0,m(n))},arguments)},e.wbg.__wbg_stateOptions_dcb483514d1b925d=function(){return x(function(e,t){return b(r(C(e),t!==0))},arguments)},e.wbg.__wbg_stateRootCid_9198769cdd9f37d5=function(e,t,n){return b(s(S(e,t),n))},e.wbg.__wbg_stateSet_a3e74b27d3d26f9d=function(){return x(function(e,t,n){d(m(e),m(t),m(n))},arguments)},e.wbg.__wbg_stateSlice_aafffe8ecba122b8=function(e,t,n){return b(i(C(e),t>>>0,n>>>0))},e.wbg.__wbg_stateValue_8ed644fcdb61558c=function(e){return b(c(C(e)))},e.wbg.__wbg_static_accessor_GLOBAL_88a902d13a557d07=function(){let e=typeof global>`u`?null:global;return w(e)?0:b(e)},e.wbg.__wbg_static_accessor_GLOBAL_THIS_56578be7e9f832b0=function(){let e=typeof globalThis>`u`?null:globalThis;return w(e)?0:b(e)},e.wbg.__wbg_static_accessor_SELF_37c5d418e4bf5819=function(){let e=typeof self>`u`?null:self;return w(e)?0:b(e)},e.wbg.__wbg_static_accessor_WINDOW_5de37043a91a9c40=function(){let e=typeof window>`u`?null:window;return w(e)?0:b(e)},e.wbg.__wbg_subarray_aa9065fa9dc5df96=function(e,t,n){return b(m(e).subarray(t>>>0,n>>>0))},e.wbg.__wbg_then_44b73946d2fb3e7d=function(e,t){return b(m(e).then(m(t)))},e.wbg.__wbg_value_cd1ffa7b1ab794f1=function(e){let t=m(e).value;return b(t)},e.wbg.__wbg_versions_c01dfd4722a88165=function(e){let t=m(e).versions;return b(t)},e.wbg.__wbg_versionvector_new=function(e){return b(We.__wrap(e))},e.wbg.__wbg_warn_52b319b1b6be3d2f=function(e,t){console.warn(S(e,t))},e.wbg.__wbindgen_as_number=function(e){return+m(e)},e.wbg.__wbindgen_bigint_from_i64=function(e){return b(e)},e.wbg.__wbindgen_bigint_from_u64=function(e){return b(BigInt.asUintN(64,e))},e.wbg.__wbindgen_bigint_get_as_i64=function(e,t){let n=m(t),r=typeof n==`bigint`?n:void 0;y().setBigInt64(e+8,w(r)?BigInt(0):r,!0),y().setInt32(e+0,!w(r),!0)},e.wbg.__wbindgen_boolean_get=function(e){let t=m(e);return typeof t==`boolean`?+!!t:2},e.wbg.__wbindgen_cb_drop=function(e){let t=C(e).original;return t.cnt--==1&&(t.a=0,!0)},e.wbg.__wbindgen_closure_wrapper285=function(e,t,n){return b(ce(e,t,15,_e))},e.wbg.__wbindgen_closure_wrapper544=function(e,t,n){return b(ce(e,t,15,ve))},e.wbg.__wbindgen_debug_string=function(e,t){let n=v(le(m(t)),f.__wbindgen_malloc,f.__wbindgen_realloc),r=h;y().setInt32(e+4,r,!0),y().setInt32(e+0,n,!0)},e.wbg.__wbindgen_error_new=function(e,t){return b(Error(S(e,t)))},e.wbg.__wbindgen_in=function(e,t){return m(e)in m(t)},e.wbg.__wbindgen_is_array=function(e){return Array.isArray(m(e))},e.wbg.__wbindgen_is_bigint=function(e){return typeof m(e)==`bigint`},e.wbg.__wbindgen_is_function=function(e){return typeof m(e)==`function`},e.wbg.__wbindgen_is_null=function(e){return m(e)===null},e.wbg.__wbindgen_is_object=function(e){let t=m(e);return typeof t==`object`&&!!t},e.wbg.__wbindgen_is_string=function(e){return typeof m(e)==`string`},e.wbg.__wbindgen_is_undefined=function(e){return m(e)===void 0},e.wbg.__wbindgen_jsval_eq=function(e,t){return m(e)===m(t)},e.wbg.__wbindgen_jsval_loose_eq=function(e,t){return m(e)==m(t)},e.wbg.__wbindgen_memory=function(){let e=f.memory;return b(e)},e.wbg.__wbindgen_number_get=function(e,t){let n=m(t),r=typeof n==`number`?n:void 0;y().setFloat64(e+8,w(r)?0:r,!0),y().setInt32(e+0,!w(r),!0)},e.wbg.__wbindgen_number_new=function(e){return b(e)},e.wbg.__wbindgen_object_clone_ref=function(e){return b(m(e))},e.wbg.__wbindgen_object_drop_ref=function(e){C(e)},e.wbg.__wbindgen_rethrow=function(e){throw C(e)},e.wbg.__wbindgen_string_get=function(e,t){let n=m(t),r=typeof n==`string`?n:void 0;var i=w(r)?0:v(r,f.__wbindgen_malloc,f.__wbindgen_realloc),a=h;y().setInt32(e+4,a,!0),y().setInt32(e+0,i,!0)},e.wbg.__wbindgen_string_new=function(e,t){return b(S(e,t))},e.wbg.__wbindgen_throw=function(e,t){throw Error(S(e,t))},e.wbg.__wbindgen_typeof=function(e){return b(typeof m(e))},e}function qe(e,t){return f=e.exports,Je.__wbindgen_wasm_module=t,ne=null,g=null,f.__wbindgen_start(),f}async function Je(e){if(f!==void 0)return f;typeof e<`u`&&(Object.getPrototypeOf(e)===Object.prototype?{module_or_path:e}=e:console.warn(`using deprecated parameters for the initialization function; pass a single object instead`)),typeof e>`u`&&(e=new URL(new URL(`loro_wasm_bg-Gulzks_0-Gulzks_0.wasm`,import.meta.url).href,``+import.meta.url));let t=Ke();(typeof e==`string`||typeof Request==`function`&&e instanceof Request||typeof URL==`function`&&e instanceof URL)&&(e=fetch(e));let{instance:n,module:r}=await Ge(await e,t);return qe(n,r)}let Ye=Symbol(`loroContainerId`);function Xe(e){let t=e.prototype,n=Object.getOwnPropertyDescriptor(t,`id`),r=n&&n.get,i=Object.getOwnPropertyDescriptor(t,`__destroy_into_raw`),a=i&&i.value;if(!r||!i||!a)throw Error(`Unexpected wasm-bindgen container wrapper shape`);Object.defineProperty(t,"id",{...n,get(){if(this.__wbg_ptr===0)return r.call(this);let e=this[Ye];if(e!==void 0)return e;let t=r.call(this);try{Object.defineProperty(this,Ye,{configurable:!0,value:t})}catch{}return t}}),Object.defineProperty(t,"__destroy_into_raw",{...i,value(){try{delete this[Ye]}catch{}return a.call(this)}})}function Ze(e){let t=e.prototype,n=Object.getOwnPropertyDescriptor(t,`kind`),r=n&&n.value;if(typeof r!=`function`)throw Error(`Unexpected wasm-bindgen container wrapper shape`);let i;Object.defineProperty(t,"kind",{...n,value(){return this.__wbg_ptr===0?r.call(this):(i===void 0&&(i=r.call(this)),i)}})}for(let e of[Me,Ie,Ae,Re,Pe,Ee])Xe(e),Ze(e);let Qe=[`Map`,`Text`,`List`,`Tree`,`MovableList`,`Counter`];function $e(e){return pe(e)}function et(e){if(typeof e!=`object`||!e)return!1;let t=Object.getPrototypeOf(e);return typeof t!=`object`||!t||typeof t.kind!=`function`?!1:Qe.includes(e.kind())}Oe.prototype.toJsonWithReplacer=function(e){let t=/* @__PURE__ */ new Set,n=this,r=(r,a)=>{if(typeof a==`string`&&$e(a)&&!t.has(a)){t.add(a);let o=n.getContainerById(a);if(o==null)throw Error(`ContainerID not found: ${a}`);let s=e(r,o);if(s===o){let e=o.getShallowValue();return typeof e==`object`?i(e):e}if(et(s))throw Error(`Using new container is not allowed in toJsonWithReplacer`);return typeof s==`object`&&s?i(s):s}if(typeof a==`object`&&a)return i(a);let o=e(r,a);if(et(o))throw Error(`Using new container is not allowed in toJsonWithReplacer`);return o},i=e=>{if(Array.isArray(e))return e.map((e,t)=>r(t,e)).filter(e=>e!==void 0);let t={};for(let[n,i]of Object.entries(e)){let e=r(n,i);e!==void 0&&(t[n]=e)}return t};return i(n.getShallowValue())};let tt=Symbol(`loro.callPendingEventsWrapped`);function nt(e,t){let n=Object.getOwnPropertyDescriptor(e,t);if(!n||typeof n.value!=`function`)return;let r=n.value;if(r[tt])return;let i=function(...e){let t;try{return t=r.apply(this,e),t}finally{t&&typeof t.then==`function`?t.finally(()=>{fe()}):fe()}};i[tt]=!0,Object.defineProperty(e,t,{...n,value:i})}function rt(e,t){for(let n of t)nt(e,n)}rt(Oe.prototype,[`setDetachedEditing`,`attach`,`detach`,`fork`,`forkAt`,`checkoutToLatest`,`checkout`,`commit`,`getCursorPos`,`revertTo`,`export`,`exportJsonUpdates`,`exportJsonInIdSpan`,`importJsonUpdates`,`import`,`importUpdateBatch`,`importBatch`,`travelChangeAncestors`,`getChangedContainersIn`,`diff`,`applyDiff`,`setPeerId`]),rt(we.prototype,[`set`,`delete`,`apply`,`removeOutdated`]),rt(He.prototype,[`undo`,`redo`]);let it=[],at=!1,ot=e=>{at||(e.stopImmediatePropagation(),it.push(e))};self.addEventListener(`message`,ot),Je().then(()=>{at=!0,self.removeEventListener(`message`,ot);for(let e of it.splice(0))self.dispatchEvent(new MessageEvent(`message`,{data:e.data,origin:e.origin,lastEventId:e.lastEventId,ports:[...e.ports]}))},e=>{setTimeout(()=>{throw Error(`Loro's WebAssembly did not load: `+(e&&e.message?e.message:String(e)))})});async function st(e){try{let t=await fetch(new URL(e,globalThis.location.href));return t.ok?new Uint8Array(await t.arrayBuffer()):null}catch{return null}}var ct=class{constructor(e,t,n,r=st){this.addFont=t,this.wantsFonts=n,this.read=r,this.left=t?[...e]:[]}left;loading=null;listeners=/* @__PURE__ */ new Set;get pending(){return this.left.length}onSettled(e){return this.listeners.add(e),()=>this.listeners.delete(e)}check(){if(this.loading||this.left.length===0)return;let e=!0;try{e=this.wantsFonts?.()??!0}catch{}e&&this.loadAll()}loadAll(){return this.loading||=this.left.length?this.load():Promise.resolve(),this.loading}async load(){for(;this.left.length;){let e=await this.read(this.left[0]);if(e)try{this.addFont(e)}catch{}this.left.shift();for(let e of this.listeners)e(this.left.length)}}};function lt(e,t){let n=e.base??`/kits/${e.dir}/`;return new URL(t,new URL(n.endsWith(`/`)?n:`${n}/`,globalThis.location.href))}let ut=/* @__PURE__ */ new Map,dt=/* @__PURE__ */ new Map;function ft(e){let t=ut.get(e.kitId);return t||(t=(async()=>{let t=await import(
3
+ /* @vite-ignore */
4
+ lt(e,`${e.module}.js`).href);if(e.runtime===`ts-worker`){let n=t.default;typeof n==`function`&&await n({base:lt(e,`./`).href})}else await t.default({module_or_path:lt(e,`${e.module}_bg.wasm`)});let n=t,r=typeof n.addFont==`function`?n.addFont:null,i=typeof n.wantsFonts==`function`?n.wantsFonts:null;return r&&await new ct(e.fonts??[],r,null).loadAll(),dt.set(e.kitId,new ct(e.lazyFonts??[],r,i)),t})(),ut.set(e.kitId,t),t.catch(()=>ut.delete(e.kitId))),t}function pt(e){return dt.get(e.kitId)??null}function mt(e,t){let n=e[t.documentClass];if(typeof n?.open!=`function`)throw Error(`${t.module} has no ${t.documentClass}.open`);let r=n.contractVersion?.()??1;if(r>1)throw Error(`${t.module} needs kit engine contract ${r}; this app supports 1`);return n}function ht(e,t,n,r,i=null){let a=mt(e,t);return i&&typeof a.openWithReplica==`function`?a.openWithReplica(n,r,new Uint8Array(i.snapshot),new Uint8Array(i.blobs)):a.open(n,r)}function gt(e,t,n,r,i){let a=mt(e,t);if(typeof a.create!=`function`)throw Error(`${t.module} cannot create documents`);return a.create(n,r,i)}function _t(e){return JSON.parse(e).map(e=>({id:e.id,title:e.title,description:e.description??null,group:e.id.split(`.`)[0]??``,mutates:e.mutates,enabled:e.enabled,menu:e.menu??[],shortcut:e.shortcut,schema:e.schema??null,outputSchema:e.outputSchema??null,params:e.params??null}))}let vt=1<<20,yt=/* @__PURE__ */ new Map;function bt(e,t){if(!e.startsWith(`fh_`))throw Error(`${e} is not a file handle`);t?yt.set(e,t):yt.delete(e)}function xt(e){let t=yt.get(e);if(!t)throw`${e} was not granted to this kit`;return t}function St(e,t){if(!t.trim()||t.endsWith(`/`))throw`${t||`an empty path`} names no file`;let n=[],r=t.startsWith(`/`)?[]:e.split(`/`);for(let e of[...r,...t.split(`/`)])if(e&&e!==`.`){if(e===`..`){if(!n.length)throw`${t} is outside the workspace`;n.pop()}else n.push(e)}if(!n.length)throw`${t} names no file`;return n.join(`/`)}function Ct(e,t,n){let r=new XMLHttpRequest;r.open(e,t,!1),r.responseType=`arraybuffer`,n&&r.setRequestHeader(`Range`,`bytes=${n[0]}-${n[1]}`);try{r.send()}catch{throw`could not read ${t}`}return r}function wt(e){yt.clear();let t=/* @__PURE__ */ new Map,n=/* @__PURE__ */ new Map,r=0,i=t=>{if(!e)throw`this host lends no files`;let n=St(e.folder,t);if(n.split(`/`).some(e=>e.startsWith(`.`)))throw`${t} is not available to the kit`;return new URL(n.split(`/`).map(encodeURIComponent).join(`/`),e.root).href},a=e=>{if(e.startsWith(`fh_`))return xt(e)!=null;let n=i(e);if(t.has(n))return!0;let r=Ct(`HEAD`,n);if(r.status===404)return!1;if(r.status>=400)throw`${e}: HTTP ${r.status}`;return!0},o=e=>{if(e.startsWith(`fh_`))return xt(e).byteLength;let n=i(e),r=t.get(n);if(r!=null)return r;let a=Ct(`HEAD`,n);if(a.status===404)throw`${e} is not in the workspace`;if(a.status>=400)throw`${e}: HTTP ${a.status}`;let o=Number(a.getResponseHeader(`Content-Length`));if(!Number.isFinite(o)||o<0)throw`${e}: the host reported no size`;return t.set(n,o),o},s=(e,t,i,a)=>{let o=`${i}:${e}`,s=n.get(o);if(s)return n.delete(o),n.set(o,s),s;let c=i*vt,l=Math.min(a,c+vt)-1,u=Ct(`GET`,e,[c,l]);if(u.status!==206&&u.status!==200)throw`${t}: HTTP ${u.status}`;let d=new Uint8Array(u.response);u.status===200&&(d=d.slice(c,l+1)),n.set(o,d),r+=d.byteLength;for(let[e,t]of n){if(r<=268435456)break;n.delete(e),r-=t.byteLength}return d};globalThis.kitionHost={size:o,readRange:(e,t,n)=>{if(e.startsWith(`fh_`))return xt(e).slice(t,t+n);let r=i(e),a=o(e),c=Math.min(a,t+n),l=new Uint8Array(Math.max(0,c-t));for(let n=t;n<c;){let i=Math.floor(n/vt),o=s(r,e,i,a),u=n-i*vt,d=Math.min(o.byteLength-u,c-n);if(d<=0)throw`${e} is shorter than the host said`;l.set(o.subarray(u,u+d),n-t),n+=d}return l},exists:a}}function Tt(e){return typeof e.modelBatch==`function`&&typeof e.modelDescribe==`function`?`engine`:typeof e.exportChanges==`function`&&typeof e.importChanges==`function`&&typeof e.version==`function`?`replica`:null}var Et=class extends Error{constructor(e){super(e.message),this.data=e,this.name=`KitModelError`}};function Dt(e){return Array.isArray(e.ops)&&e.ops.length>0}let Ot=/* @__PURE__ */ new Set([`edit.undo`,`undo`]),kt=/* @__PURE__ */ new Set([`edit.redo`,`redo`]),At=Object.freeze({viewingGroups:Object.freeze([`view`,`select`,`selection`,`go`,`navigate`,`help`,`playback`,`transport`,`playhead`,`caret`]),viewingCommands:Object.freeze([`markers.goNext`,`markers.goPrev`,`markers.goToIn`,`markers.goToOut`,`source.open`,`source.setPlayhead`,`source.inspect`,`sequence.open`,`sequence.moveTab`,`sequence.inspect`,`sequence.matchFrame`,`sequence.revealInProject`]),editingCommands:Object.freeze([`edit.undo`,`edit.redo`,`undo`,`redo`,`u`]),documentSettings:Object.freeze(/* @__PURE__ */ `view.freezePanes,view.freezeTopRow,view.freezeFirstColumn,view.unfreezePanes,view.gridlines,view.headings,view.showZeros,view.rightToLeft,view.newWindow,view.addGuide,view.clearGuides,view.importGuides,view.moveGuide,view.removeGuide,view.lookAtSelected,view.lookAtAll,view.reset3DView,view.newGuide,view.newGuideLayout,view.newGuidesFromShape,view.deleteGuide,view.clearCanvasGuides,view.clearSelectedArtboardGuides,view.clearSlices,view.lockSlices,select.toWorkPath,select.convertToShape,select.saveSelection,select.float,select.drop,view.guides.make,view.guides.release,view.guides.clear,view.guides.lock,view.saved.new,view.saved.edit,select.save`.split(`,`)),editingGroups:Object.freeze([`file`]),fileOutputs:/^file\.(export|saveCopy|saveACopy|print)/,refusedTools:Object.freeze([`pointer`,`toolKey`,`toolText`]),refusedMethods:Object.freeze([`executeAs`,`undo`,`redo`,`importChanges`,`addBlobs`])});function jt(e){let{id:t}=e,n=t.split(`.`)[0]??``;return At.editingCommands.includes(t)||At.documentSettings.includes(t)?!0:At.editingGroups.includes(n)?!At.fileOutputs.test(t):At.viewingCommands.includes(t)||At.viewingGroups.includes(n)?!1:e.mutates===!0}function Mt(e,t){return!t||!e?/* @__PURE__ */ new Set:new Set(e.filter(e=>typeof e==`string`&&!At.editingCommands.includes(e)&&!At.editingGroups.includes(e.split(`.`)[0]??``)))}function Nt(e,t){return t.size===0?null:Ot.has(e)?`undo`:kt.has(e)?`redo`:null}var Pt=class extends Error{reason=`read-only`;constructor(e,t){super(t??`${e} is not allowed: the document is read-only`),this.name=`ReadOnlyError`}};let Ft=`unsupported`;var It=class extends Error{constructor(e,t){super(e),this.errors=t,this.name=`KitParamsError`}reason=`invalid-params`},Lt=class extends Error{constructor(e,t){super(t),this.reason=e,this.name=`KitEngineRefusal`}};function Rt(e){if(!e||typeof e!=`object`)return null;let{path:t,message:n,expected:r}=e;return typeof t!=`string`||typeof n!=`string`?null:typeof r==`string`?{path:t,message:n,expected:r}:{path:t,message:n}}function zt(e){if(e instanceof It)return e;let t=e instanceof Error?e.message:typeof e==`string`?e:null,n=t?.indexOf(``)??-1;if(t==null||n<0)return null;let r=[];try{let e=JSON.parse(t.slice(n+1));Array.isArray(e)&&(r=e.map(Rt).filter(e=>e!==null))}catch{}return new It(t.slice(0,n),r)}let Bt=[`command_not_found`,`command_disabled`,`invalid_params`];var Vt=class extends Error{constructor(e,t){super(t),this.reason=e,this.name=`KitCommandFailure`}};function Ht(e){if(e instanceof Vt)return e;let t=e instanceof Error?e.message:typeof e==`string`?e:null,n=t?.lastIndexOf(``)??-1;if(t==null||n<0)return null;let r=Bt.find(e=>e===t.slice(n+1));return r?new Vt(r,t.slice(0,n)):null}let D=null,Ut=null,Wt,Gt=!1,O=!1,Kt=/* @__PURE__ */ new Set,qt=null,k={undo:0,redo:0},Jt=null,Yt=null,Xt=null,Zt=!1,A=null,Qt=null,$t=/* @__PURE__ */ new Map,en=0;function tn(){for(let e of $t.values())e.doc.free();$t.clear()}async function nn(e,t,n){if(e.action===`begin`||e.action===`advance`){let t=e.steps,r=e.ops,i=JSON.stringify(e).length;if(!Array.isArray(t)||r!==void 0&&!Array.isArray(r)||t.length+(Array.isArray(r)?r.length:0)>256||i>1048576||e.action===`begin`&&$t.size>=16)throw Error(`premise plan limit`);let a=e.action===`advance`?String(e.token):String(++en),o=e.action===`advance`?$t.get(a):await(async()=>{let e=j();if(!e.forkForPremises)throw Error(`the kit cannot fork for premise planning`);let t=e.forkForPremises();try{let r=JSON.parse(e.editorState?.()??`null`),{ReplicaModel:i}=await rn(),a=n?new i(t):(await an(e)).forkForPremises(t);return{doc:t,model:a,step:0,beforeState:r,before:a.checkpointForPremises(),changes:null,refs:{},operations:0,bytes:0}}catch(e){throw t.free(),e}})();if(!o)throw Error(`premise plan unavailable`);$t.delete(a);try{if(o.operations+=t.length+(Array.isArray(r)?r.length:0),o.bytes+=i,o.operations>256||o.bytes>1048576)throw Error(`premise plan limit`);if(Array.isArray(r)){if(r.some(e=>typeof e?.ref==`string`&&Object.hasOwn(o.refs,e.ref)))throw Error(`premise batch reference is already bound`);let e=t=>typeof t==`string`&&Object.hasOwn(o.refs,t)?o.refs[t]:Array.isArray(t)?t.map(e):t&&typeof t==`object`?Object.fromEntries(Object.entries(t).map(([t,n])=>[t,e(n)])):t,t=(await o.model.batch({ops:e(r)},null,!1)).refs;Object.assign(o.refs,t)}let n=o.doc;for(let e of t){if(e.context&&Object.keys(e.context).length){if(!n.setContext)throw Error(`the kit cannot set premise context`);n.setContext(JSON.stringify(e.context))}if(!n.executeAs)throw Error(`the kit cannot execute premise plans as the agent`);if(Wn(n.executeAs(e.command,JSON.stringify(e.params??{}),cn({kind:`agent`,principal:`premise-plan`}))))return n.free(),{closed:!0}}return o.changes=o.model.changesForPremises(o.before),o.step=Number(e.step??o.operations),$t.set(a,o),{token:a,beforeState:o.beforeState,state:JSON.parse(n.editorState?.()??`null`),changes:o.changes,refs:o.refs}}catch(e){throw o.doc.free(),e}}let r=String(e.token),i=$t.get(r);if(!i)throw Error(`premise plan unavailable`);if(e.action===`end`)return $t.delete(r),i.doc.free(),{};if(e.action!==`read`)throw Error(`premise plan action required`);let a=e.params??{},o=String(e.method);if(o===`model.batch`){if(!Array.isArray(a.ops)||a.ops.length)throw Error(`premise plan only accepts model reads`);return i.model.batch(a,null,!0)}if(o===`model.describe`)return i.model.describe(null);if(![`under`,`covered`,`content`,`state`,`identity`,`derived`,`blob`,`blob-support`].includes(o))throw Error(`unsupported premise plan read`);if(o===`state`)return{state:JSON.parse(i.doc.editorState?.()??`null`),beforeState:i.beforeState};if(!i.doc.premiseRead)throw Error(`premise reads unavailable`);let s=()=>JSON.parse(i.doc.premiseRead(o,JSON.stringify({...a,step:i.step,changes:i.changes,state:JSON.parse(i.doc.editorState?.()??`null`)}))),{on:c,part:l,time:u}=a,d={...c?{on:c}:{},...l?{part:l}:{},...u===void 0?{}:{time:u}};return Zt?kn(i.doc,{state:null,set:d},s).value:s()}function rn(){return Qt??=Promise.resolve().then(function(){return tc}),Qt}async function an(e){let{ReplicaModel:t}=await rn();return A??=new t(e),A}function on(e,t){let n=t===`undo`?e.canUndo:e.canRedo;return typeof n==`function`?n.call(e):JSON.parse(e.inspect())[t===`undo`?`canUndo`:`canRedo`]===!0}function sn(e,t){if(!A?.canRedo())return t();let n=jn(e),r=t();return!Wn(r)&&jn(e)!==n&&A.dropRedo(),r}let cn=e=>JSON.stringify([e.kind,e.principal,e.label??null]),ln=/* @__PURE__ */ new Set([`free`,`execute`,`save`,`markSaved`,`constructor`,`open`,`create`,`openWithReplica`,`takeOutput`]);function un(e){if(!Gt||typeof e.version!=`function`||e.hasReplica===!1)return null;try{return e.version()}catch{return null}}function dn(e,t){let n=e[t];if(typeof n!=`function`)throw Error(`the kit module has no replica (${t})`);return n.bind(e)}function fn(e){return typeof e.docId==`string`?e.docId:null}function pn(e,t){return typeof e.blobsSince==`function`?e.blobsSince(t):dn(e,`blobs`)()}function mn(e){if(typeof e.schemaVersion!=`function`||typeof e.authorSchemaVersion!=`function`||e.hasReplica===!1)return null;try{let t=Number(e.schemaVersion()),n=e.authorSchemaVersion();return Number.isFinite(t)?{reads:t,newestAuthor:typeof n==`number`&&Number.isFinite(n)?n:null}:null}catch{return null}}function hn(e,t){if(k[t]===0)return!1;try{return on(e,t)}catch{return!1}}let gn=null,_n=null;function vn(e){let t=un(e);return[e.revision,e.width,e.height,e.pinnedLeft??0,e.pinnedTop??0,!O&&e.dirty,t?t.join(`,`):``,A?.canUndo()??!1,A?.canRedo()??!1,JSON.stringify(Jt??null)].join(`|`)}function yn(e){return _n=e,e}function bn(e,t=!1){let n=xn(e,t);return n===_n?null:(_n=n,n)}function xn(e,t=!1){let n=vn(e);if(!t&&gn?.key===n)return gn.value;let r=Sn(e);return gn?.key===n&&JSON.stringify(gn.value.inspected)===JSON.stringify(r.inspected)?gn.value:(gn={key:n,value:r},_n=null,r)}function Sn(e){let t=JSON.parse(e.inspect());return{replica:un(e),width:e.width,height:e.height,pinned:{left:e.pinnedLeft??0,top:e.pinnedTop??0},revision:Number(e.revision),dirty:!O&&e.dirty,canUndo:O?hn(e,`undo`):t.canUndo===!0||(A?.canUndo()??!1),canRedo:O?hn(e,`redo`):t.canRedo===!0||(A?.canRedo()??!1),inspected:t,model:Tt(e),...typeof e.executeAs==`function`?{origins:!0}:{},...Zt?{context:!0}:{},...Ln(e)?{changes:!0}:{},...typeof e.premiseRead==`function`?{premises:!0}:{},schema:Jt}}let Cn=/* @__PURE__ */ new Set([`tools`,`tool`,`selectTool`,`pointer`,`toolKey`,`toolText`,`toolState`]);function wn(e,t){for(let n of t.setup??[]){let t=e[n.method];if(ln.has(n.method)||typeof t!=`function`)throw Error(`the kit module has no method ${n.method}`);t.apply(e,n.args)}}function Tn(e){let t=pt(e);t!==Ut&&(Ut=t,t?.onSettled(e=>En({fonts:e}))),En({fonts:Ut?.pending??0})}function En(e){typeof self<`u`&&typeof self.postMessage==`function`&&typeof window>`u`&&self.postMessage(e)}function Dn(e,t,n){O=e,Kt=Mt(n.commentCommands,e&&t),qt=null,k={undo:0,redo:0},Jt=D?mn(D):null,Xt=null,A=null,Zt=D?On(D):!1}function On(e){if(typeof e.editorState!=`function`||typeof e.restoreEditorState!=`function`||typeof e.setContext!=`function`)return!1;try{return e.editorState()!==`null`}catch{return!1}}function kn(e,t,n){if(!Zt)throw new Lt(Ft,`this kit's engine keeps no editor state of the agent's own: it does not answer editorState, restoreEditorState and setContext`);let r=e.editorState(),i=!1;try{if(t.state!==null&&e.restoreEditorState(t.state),t.set&&Object.keys(t.set).length){let n=Object.keys(t.set).map(e=>`\`${e}\``).join(`, `);try{e.setContext(JSON.stringify(t.set))}catch(e){throw new Lt(Ft,`the kit could not set ${n}: ${e instanceof Error?e.message:String(e)}`)}}let r=n();return i=Wn(r),{value:r,state:i?`null`:e.editorState()}}finally{i||e.restoreEditorState(r)}}function An(e,t){return Xt||=new Map(_t(e.commands()).map(e=>[e.id,jt(e)])),Xt.get(t)??jt({id:t})}function jn(e){if(typeof e.version!=`function`||e.hasReplica===!1)return null;try{return e.version().join(`,`)}catch{return null}}function Mn(e,t,n){if(!O)return n();let r=jn(e),i=e.dirty,a=n();if(Wn(a))return a;let o=jn(e);if(!(r==null?o!=null||!i&&e.dirty:o!==r))return a;throw Nn(e),new Pt(t)}function Nn(e,t=`undo`){let n=e[t];try{typeof n==`function`?n.call(e):e.execute(t===`undo`?`edit.undo`:`edit.redo`,`{}`)}catch{}}async function Pn(e,t,n,r=`undo`){if(typeof e.version!=`function`||typeof e.exportChanges!=`function`)return{value:Mn(e,`\`${t}\``,n),changed:!1};try{e.version()}catch{return{value:Mn(e,`\`${t}\``,n),changed:!1}}let{ChangeAudit:i,touchesOnlyComments:a}=await(Yt??=Promise.resolve().then(function(){return Ws})),o=dn(e,`exportChanges`);qt?qt.follow(o(qt.version().encode())):qt=i.load(o(/* @__PURE__ */ new Uint8Array));let s=jn(e),c=n();if(jn(e)===s)return{value:c,changed:!1};let l=!1;try{l=a(qt.touched(o(qt.version().encode())))}catch{}if(l)return{value:c,changed:!0};throw Nn(e,r),new Pt(`\`${t}\``,`\`${t}\` is not allowed: it changed more than comments, and the document is read-only`)}async function Fn(e,t,n,r){if(k[r]===0)throw new Pt(`\`${t}\``,`\`${t}\` is not allowed: the document is read-only, and there is no comment of yours to ${r}`);let{value:i,changed:a}=await Pn(e,t,()=>e.execute(t,JSON.stringify(n)),r===`undo`?`redo`:`undo`);return a&&(k=r===`undo`?{undo:k.undo-1,redo:k.redo+1}:{undo:k.undo+1,redo:k.redo-1}),i}async function In(e,t,n){try{let{value:r,changed:i}=await Pn(e,t,()=>sn(e,()=>e.execute(t,JSON.stringify(n))));return i&&(k={undo:k.undo+1,redo:0}),r}catch(e){throw e instanceof Pt&&(k={undo:k.undo,redo:0}),e}}function Ln(e){return typeof e.version==`function`&&typeof e.exportChanges==`function`}function Rn(e){let t=``;for(let n of e)t+=String.fromCharCode(n);return btoa(t)}function zn(e){try{return Uint8Array.from(atob(e),e=>e.charCodeAt(0))}catch{return/* @__PURE__ */ new Uint8Array}}function Bn(e){if(typeof e.version!=`function`||e.hasReplica===!1)return null;try{return e.version()}catch{return null}}async function Vn(e){return e instanceof Et?e:new Et((await rn()).modelErrorData(e))}function Hn(e){try{let t=JSON.parse(e instanceof Error?e.message:String(e));return typeof t?.code!=`string`||typeof t.message!=`string`?null:new Et({code:t.code,index:typeof t.index==`number`?t.index:null,message:t.message})}catch{return null}}function j(){if(!D)throw Error(`no document is open`);return D}async function Un(e){let t=j();if(O&&An(t,e.command)){let n=Nt(e.command,Kt);if(n)return{value:{result:await Fn(t,e.command,e.params,n),info:yn(xn(t))}};if(!Kt.has(e.command))throw new Pt(`\`${e.command}\``);return{value:{result:await In(t,e.command,e.params),info:yn(xn(t))}}}let n=Ot.has(e.command),r=kt.has(e.command),i=(e.params.scope??(e.origin?.kind===`agent`?`agent`:`person`))===`agent`;if(A&&(n||r)&&!i){let e=on(t,n?`undo`:`redo`),r=n?A.undo(e):A.redo(e);if(r||!e)return{value:{result:JSON.stringify({done:r}),info:yn(xn(t))}}}let a=e.origin,o=a&&typeof t.executeAs==`function`,s=a?.kind===`system`;if(s&&!o)throw new Lt(Ft,`\`${e.command}\` was not run: this kit cannot keep a host's change out of the undo history`);let c=o?()=>t.executeAs(e.command,JSON.stringify(e.params),cn(a)):()=>t.execute(e.command,JSON.stringify(e.params)),l=()=>Mn(t,`\`${e.command}\``,n||r||s?c:()=>sn(t,c));if(e.agent){let{value:i,state:a}=kn(t,e.agent,l);return Wn(i)?Gn(t,i):((n||r)&&A?.engineMoved(),{value:{result:i,info:bn(t,!0),state:a}})}let u=l();return Wn(u)?Gn(t,u):((n||r)&&A?.engineMoved(),{value:{result:u,info:bn(t,!0)}})}function Wn(e){try{let t=typeof e==`string`?JSON.parse(e):e;return typeof t==`object`&&!!t&&t.closed===!0}catch{return!1}}function Gn(e,t){return e.free(),D=null,A=null,gn=null,_n=null,tn(),{value:{result:t,info:null,closed:!0,state:null}}}async function Kn(e){switch(e.method){case`open`:{let t=await ft(e.manifest);return wt(e.lent),tn(),D?.free(),Gt=e.replica?.follow??!1,D=ht(t,e.manifest,e.name,new Uint8Array(e.bytes),e.replica?.stored??null),Wt=e.name,gn=null,_n=null,wn(D,e.manifest),Dn(e.readOnly===!0,e.canComment===!0,e.manifest),Tn(e.manifest),{value:yn(xn(D))}}case`create`:{let t=await ft(e.manifest);return wt(e.lent),tn(),D?.free(),Gt=!1,D=gt(t,e.manifest,e.name,e.width,e.height),Wt=e.name,gn=null,_n=null,wn(D,e.manifest),Dn(e.readOnly===!0,!1,e.manifest),Tn(e.manifest),{value:yn(xn(D))}}case`execute`:try{return await Un(e)}catch(e){throw zt(e)??Ht(e)??e}case`render`:{let t=j().render(e.x,e.y,e.scale,e.width,e.height);return{value:t.buffer,transfer:[t.buffer]}}case`renderArea`:{let t=j().renderArea(e.x0,e.y0,e.x1,e.y1,e.width,e.height);return{value:t.buffer,transfer:[t.buffer]}}case`commands`:return{value:_t(j().commands())};case`premiseRead`:{if(e.name===`plan`)return{value:await nn(e.params)};let t=j();if(typeof t.premiseRead!=`function`)throw new Lt(Ft,`the kit cannot answer premise reads in its editor`);let n=()=>JSON.parse(t.premiseRead(e.name,JSON.stringify(e.params))),{on:r,part:i,time:a}=e.params,o={...r?{on:r}:{},...i?{part:i}:{},...a===void 0?{}:{time:a}};if(!Zt){if(Object.keys(o).length)throw new Lt(Ft,`the kit cannot isolate the premise call context`);return{value:n()}}return{value:kn(t,{state:null,set:o},n).value}}case`inspectAt`:{let t=j();if(!Zt)throw new Lt(Ft,`the kit cannot isolate the inspection context`);return{value:kn(t,{state:null,set:e.context},()=>{let n=JSON.parse(t.inspect()),r=e.part===void 0?void 0:_t(t.commands()).find(e=>[`slide.inspect`,`page.inspect`].includes(e.id)&&e.mutates===!1),i=r?JSON.parse(t.execute(r.id,JSON.stringify({index:e.part-1}))):void 0;return{details:n,...i===void 0?{}:{part:i}}}).value}}case`disabledCommands`:{let e=j();return typeof e.disabledCommands==`function`?{value:JSON.parse(e.disabledCommands())}:{value:_t(e.commands()).filter(e=>!e.enabled).map(e=>e.id)}}case`check`:{let e=j();if(typeof e.check!=`function`)return{value:[]};let t=JSON.parse(e.check());return{value:Array.isArray(t.diagnostics)?t.diagnostics:[]}}case`lendHandle`:return bt(e.handle,e.bytes?new Uint8Array(e.bytes):null),{value:null};case`takeOutput`:{let t=j();if(typeof t.takeOutput!=`function`)throw Error(`there is no output ${e.id}`);let n=t.takeOutput(e.id,e.max);return{value:n.buffer,transfer:[n.buffer]}}case`save`:{let e=j(),t=e.save(Wt);return{value:{bytes:t.buffer,revision:Number(e.revision)},transfer:[t.buffer]}}case`invoke`:{let t=j(),n=t[e.name];if(ln.has(e.name)||e.name.startsWith(`_`)||typeof n!=`function`)throw Error(`the kit module has no method ${e.name}`);if(At.refusedMethods.includes(e.name))throw Error(`the kit module's ${e.name} is not open to editors`);let r=Mn(j(),e.name,()=>sn(j(),()=>n.apply(t,e.args)));return ArrayBuffer.isView(r)?{value:r,transfer:[r.buffer]}:{value:r}}case`tool`:{let t=j(),n=t[e.name];if(!Cn.has(e.name)||typeof n!=`function`)throw Error(`the kit module has no engine tools (${e.name})`);if(O&&At.refusedTools.includes(e.name))throw new Pt(`the engine tools' ${e.name}`);let r=t.revision,i=Mn(t,`the engine tools' ${e.name}`,()=>sn(t,()=>n.apply(t,e.args)));return{value:{value:e.name===`tool`?i:JSON.parse(i),info:e.settled||t.revision!==r?yn(xn(t,e.settled)):null}}}case`info`:return{value:xn(j(),!0)};case`fonts`:return await Ut?.loadAll(),{value:null};case`markSaved`:{if(O)throw new Pt(`markSaved`);let t=j();return(e.revision==null||Number(t.revision)===e.revision)&&t.markSaved(),{value:yn(xn(t))}}case`saveWithReplica`:{let e=j(),t=e.save(Wt),n=null;try{n={snapshot:dn(e,`snapshot`)().buffer,blobs:dn(e,`blobs`)().buffer}}catch{}let r=[t.buffer,...n?[n.snapshot,n.blobs]:[]];return{value:{bytes:t.buffer,replica:n,revision:Number(e.revision)},transfer:r}}case`replicaVersion`:{let e=dn(j(),`version`)();return{value:e.buffer,transfer:[e.buffer]}}case`replicaExport`:{let t=j(),n=new Uint8Array(e.since),r=dn(t,`exportChanges`)(n),i=pn(t,n);return{value:{changes:r.buffer,blobs:i.buffer},transfer:[r.buffer,i.buffer]}}case`replicaImport`:{let t=j();e.blobs.byteLength&&dn(t,`addBlobs`)(new Uint8Array(e.blobs));let n=JSON.parse(dn(t,`importChanges`)(new Uint8Array(e.changes)));return Jt=mn(t)??Jt,{value:{damage:n,info:yn(xn(t))}}}case`replicaDocId`:return{value:fn(j())};case`replicaMerge`:{let t=j();if(O)throw new Pt(`the draft merge`);e.blobs.byteLength&&typeof t.addBlobs==`function`&&t.addBlobs(new Uint8Array(e.blobs));let n;try{n=(await an(t)).merge(new Uint8Array(e.changes),e.origin,e.schema)}catch(e){throw await Vn(e)}return{value:{summary:n,info:yn(xn(t))}}}case`replicaCompare`:{let t=j();try{return{value:(await an(t)).compare(new Uint8Array(e.changes),e.schema)}}catch(e){throw await Vn(e)}}case`modelVersion`:{let e=Bn(j());if(!e)throw Error(`the kit's engine keeps no replica, so it cannot say what changed`);return{value:Rn(e)}}case`modelChanges`:{let t=j();if(!Ln(t))throw Error(`the kit's engine keeps no replica, so it cannot say what changed`);let n;try{n=(await an(t)).changesSince(zn(e.since),e.schema)}catch(e){throw await Vn(e)}return{value:{...n,version:Rn(dn(t,`version`)())}}}case`modelReply`:{let t=j();return{value:Ln(t)?(await an(t)).reply(e.value):e.value}}case`modelDescribe`:{let t=j(),n=Tt(t);try{if(n===`engine`)return e.schema&&Ln(t)&&(await an(t)).describe(e.schema),{value:JSON.parse(t.modelDescribe())};if(n===`replica`)return{value:(await an(t)).describe(e.schema)}}catch(e){throw await Vn(e)}throw Error(`the kit's engine has no object model`)}case`modelBatch`:{let t=j(),n=Tt(t);if(!n)throw Error(`the kit's engine has no object model`);if(O&&Dt(e.request))throw new Et({code:`read-only`,index:null,message:`the document is read-only`});let r;try{r=n===`engine`?JSON.parse(t.modelBatch(JSON.stringify(e.request))):{...await(await an(t)).batch(e.request,e.schema,O)}}catch(e){throw Hn(e)??await Vn(e)}let i=Bn(t);return{value:{answer:{...r,version:i?.buffer??null},info:yn(xn(t))},transfer:i?[i.buffer]:[]}}}}let qn=Promise.resolve();self.onmessage=e=>{let{id:t,request:n}=e.data;qn=qn.then(async()=>{try{let{value:e,transfer:r=[]}=await Kn(n);self.postMessage({id:t,ok:!0,value:e},r)}catch(e){let n=e instanceof Pt||e instanceof It||e instanceof Lt?e.reason:void 0,r=e instanceof Et?e.data:void 0,i=e instanceof It?e.errors:void 0;self.postMessage({id:t,ok:!1,error:e instanceof Error?e.message:String(e),reason:n,data:r,errors:i})}Ut?.check()})};var M=class extends Error{reason;constructor(e,t){super(t),this.name=`KdmError`,this.reason=e}};let N={schema:e=>new M(`schema`,`schema: ${e}`),unknownType:e=>new M(`unknown-type`,`unknown type \`${e}\``),unknownField:(e,t,n=[])=>{let r=``;return(t.includes(`.`)||t.includes(`/`))&&(r+=`; a field is one name, not a path: load the nested object and set its own fields`),n.length>0&&(r+=`; its fields: ${n.slice(0,40).join(`, `)}${n.length>40?`, …`:``}`),new M(`unknown-field`,`type \`${e}\` has no field \`${t}\`${r}`)},invalidValue:(e,t,n)=>new M(`invalid-value`,`field \`${t}\` of \`${e}\` ${n}`),notFound:e=>new M(`not-found`,`object \`${e}\` not found`),cycle:()=>new M(`cycle`,`moving that node would create a cycle`),outOfRange:(e,t)=>new M(`out-of-range`,`index ${e} is out of range (length ${t})`),wrongDocument:(e,t)=>new M(`wrong-document`,`the changes belong to document ${t}, not ${e}`),newerSchema:(e,t)=>new M(`newer-schema`,`the document needs schema version ${e}; this kit reads schema versions up to ${t}`),historyMissing:e=>new M(`history-missing`,`history missing: ${e}`),encoding:e=>new M(`encoding`,`encoding: ${e}`),engine:e=>new M(`engine`,`engine: ${e}`)};function Jn(e){return e===`person`||e===`extension`?`person`:e===`agent`?`agent`:null}let Yn=(e,t)=>({kind:`person`,principal:e,label:t}),Xn=(e,t)=>({kind:`agent`,principal:e,label:t}),Zn=e=>({kind:`system`,principal:`kdm`,label:e});function Qn(e){return JSON.stringify([e.kind,e.principal,e.label??null])}let $n=[`person`,`agent`,`extension`,`import`,`system`];function er(e){let t=typeof e==`object`&&e?e:null;if(t===null||typeof t.kind!=`string`||!$n.includes(t.kind)||typeof t.principal!=`string`||t.label!==void 0&&typeof t.label!=`string`)throw new M(`invalid-value`,`an origin needs a kind (${$n.join(`, `)}), a string principal, and an optional string label`)}function tr(e,t){return JSON.stringify([e.kind,e.principal,e.label??null,t])}function nr(e){let t=rr(e);if(t===null)return null;let n=JSON.parse(e)[3];return{origin:t,schemaVersion:typeof n==`number`&&Number.isInteger(n)&&n>=0?n:null}}function rr(e){let t;try{t=JSON.parse(e)}catch{return null}return!Array.isArray(t)||typeof t[0]!=`string`||!$n.includes(t[0])||typeof t[1]!=`string`?null:{kind:t[0],principal:t[1],label:typeof t[2]==`string`?t[2]:void 0}}let ir=2**53;function P(e,t,n){t===`__proto__`?Object.defineProperty(e,t,{value:n,enumerable:!0,writable:!0,configurable:!0}):e[t]=n}function F(e){if(Array.isArray(e))return e.map(F);if(typeof e==`object`&&e){let t=e,n={};for(let e of Object.keys(t).sort(I))t[e]!==void 0&&P(n,e,F(t[e]));return n}return e}function I(e,t){if(e===t)return 0;let n=Math.min(e.length,t.length);for(let r=0;r<n;r+=1){let n=e.codePointAt(r),i=t.codePointAt(r);if(n!==i)return n<i?-1:1;n>65535&&(r+=1)}return e.length-t.length}function ar(e){if(!Number.isFinite(e))return`null`;if(Number.isInteger(e)&&Math.abs(e)<=ir)return String(e);let[t,n]=Math.abs(e).toExponential().split(`e`),r=t.replace(`.`,``),i=Number(n)+1,a=e<0?`-`:``,o;if(i>=r.length&&i<=16)o=`${r}${`0`.repeat(i-r.length)}.0`;else if(i>0&&i<=16)o=`${r.slice(0,i)}.${r.slice(i)}`;else if(i>-5&&i<=0)o=`0.${`0`.repeat(-i)}${r}`;else{let e=i-1,t=r.length>1?`.${r.slice(1)}`:``;o=`${r[0]}${t}e${e<0?`-`:`+`}${Math.abs(e)}`}return a+o}function L(e){if(e==null)return`null`;if(typeof e==`number`)return ar(e);if(typeof e==`string`||typeof e==`boolean`)return JSON.stringify(e);if(Array.isArray(e))return`[${e.map(L).join(`,`)}]`;if(typeof e==`object`){let t=e,n=[];for(let e of Object.keys(t).sort(I))t[e]!==void 0&&n.push(`${JSON.stringify(e)}:${L(t[e])}`);return`{${n.join(`,`)}}`}return`null`}let or=e=>e>=56320&&e<=57343;function sr(e){let t=``,n=0;for(let r=0;r<e.length;r+=1){let i=e.charCodeAt(r);if(!(i<55296||i>57343)){if(i<=56319&&or(e.charCodeAt(r+1))){r+=1;continue}t+=`${e.slice(n,r)}�`,n=r+1}}return n===0?e:t+e.slice(n)}let cr=e=>{let t=Object.getPrototypeOf(e);return t===Object.prototype||t===null};function lr(e,t){let n=/* @__PURE__ */ new Set,r=(e,i)=>{let a=e=>{throw t(`holds ${e}${i?` at ${i}`:``}, which is not JSON`)};switch(typeof e){case`string`:return sr(e);case`boolean`:return e;case`number`:return Number.isFinite(e)?e:a(String(e));case`object`:break;default:return a(typeof e>`u`?`undefined`:`a ${typeof e}`)}if(e===null)return null;n.has(e)&&a(`a cycle`),n.add(e);try{if(Array.isArray(e)){Object.getPrototypeOf(e)!==Array.prototype&&a(`an array subclass`);let t=[];for(let n=0;n<e.length;n+=1)n in e||a(`a hole`),t.push(r(e[n],`${i}[${n}]`));return t}cr(e)||a(`a ${e.constructor?.name??`non-plain object`}`),Object.getOwnPropertySymbols(e).length>0&&a(`a symbol key`);let t={};for(let n of Object.keys(e))n===`__proto__`&&a("a `__proto__` key"),P(t,sr(n),r(e[n],`${i}.${n}`));return t}finally{n.delete(e)}};return e===void 0?null:r(e,``)}let ur=2**53,dr=2**32-1,fr=e=>e.kind===`list`||e.kind===`tree`||(e.kind===`ref`||e.kind===`map`)&&e.owns===!0;function pr(e,t){let n=e=>typeof e==`object`&&!!e&&!Array.isArray(e),r=e=>typeof e==`string`&&e!==``&&t(e)?e:null,i=[];if(!n(e))return i;for(let t of Object.keys(e).sort(I)){let a=e[t],o=r(a);if(o!==null)i.push([o,t]);else if(n(a))for(let e of Object.keys(a).sort(I)){let n=r(a[e]);n!==null&&i.push([n,`${t}.${e}`])}}return i}function mr(e){return e===``?null:e.length>32?`a prefix has at most 32 characters`:/^[A-Za-z]/.test(e)?/^[A-Za-z0-9_:/-]*$/.test(e)?null:"a prefix holds ASCII letters, digits, `_`, `-`, `:`, and `/` only":`a prefix starts with a letter`}let hr=e=>Object.values(e.types).some(e=>e.toolId!==void 0);function gr(e,t){if(e.readOnly)return!0;let n=e.readOnlyWhen;if(n===void 0)return!1;let r=t(n.field);return typeof r==`string`&&n.values.includes(r)}let _r=[`after`,`before`,`both`,`none`],vr=[`string`,`number`,`integer`,`boolean`,`enum`,`json`,`blob`,`ref`,`text`,`list`,`tree`,`map`],yr=[`text`,`list`,`tree`,`map`],br=[`ref`,`list`,`tree`],xr=e=>yr.includes(e),R=e=>Object.keys(e).sort(I),z=e=>typeof e==`object`&&!!e&&!Array.isArray(e),Sr=e=>typeof e==`number`&&Number.isInteger(e)&&e>=0&&e<=dr;function B(e,t,n){for(let r of Object.keys(t))if(!n.includes(r))throw N.schema(`${e}: unknown field \`${r}\``)}function Cr(e,t){if(!z(t))throw N.schema(`${e}: a field definition is an object`);if(B(e,t,[`kind`,`of`,`values`,`optional`,`marks`,`readOnly`,`owns`,`cascade`,`position`,`display`,`sample`,`minimum`,`maximum`,`multipleOf`,`label`,`members`]),typeof t.kind!=`string`||!vr.includes(t.kind))throw N.schema(`${e}: unknown kind \`${String(t.kind)}\``);let n={kind:t.kind};if(t.label!==void 0&&t.label!==null){if(typeof t.label!=`string`)throw N.schema(`${e}: label is a string`);n.label=t.label}for(let r of[`minimum`,`maximum`,`multipleOf`]){let i=t[r];if(i!=null){if(![`number`,`integer`,`json`].includes(n.kind)||typeof i!=`number`||!Number.isFinite(i)||r===`multipleOf`&&i<=0)throw N.schema(`${e}: invalid numeric limit ${r}`);n[r]=i}}if(n.minimum!==void 0&&n.maximum!==void 0&&n.minimum>n.maximum)throw N.schema(`${e}: minimum exceeds maximum`);if(t.members!==void 0){if(!z(t.members)||![`json`,`map`].includes(n.kind)&&Object.keys(t.members).length>0)throw N.schema(`${e}: members requires json or map and an object`);n.members={};for(let[r,i]of Object.entries(t.members)){if(r.split(`.`).some(e=>!/^[^.[\]]+(?:\[\])*$/.test(e)))throw N.schema(`${e}: invalid member path ${r}`);if(!z(i))throw N.schema(`${e}: member limits must be an object`);B(e,i,[`minimum`,`maximum`,`multipleOf`,`nullable`]);let{nullable:t,...a}=i;if(t!==void 0&&typeof t!=`boolean`)throw N.schema(`${e}: nullable is a boolean`);let o=Cr(`${e}.${r}`,{kind:`number`,...a}),s={};t===!0&&(s.nullable=!0);for(let e of[`minimum`,`maximum`,`multipleOf`])o[e]!==void 0&&(s[e]=o[e]);P(n.members,r,s)}}if(t.of!==void 0&&t.of!==null){if(typeof t.of!=`string`)throw N.schema(`${e}: \`of\` is a type name`);n.of=t.of}if(t.values!==void 0){if(!Array.isArray(t.values)||!t.values.every(e=>typeof e==`string`))throw N.schema(`${e}: \`values\` is a list of strings`);n.values=[...t.values]}if(t.optional!==void 0){if(typeof t.optional!=`boolean`)throw N.schema(`${e}: \`optional\` is a boolean`);n.optional=t.optional}if(t.marks!==void 0){if(!z(t.marks))throw N.schema(`${e}: \`marks\` is an object of mark definitions`);let r={};for(let n of R(t.marks)){let i=t.marks[n];if(!z(i))throw N.schema(`${e} mark \`${n}\`: a mark definition is an object`);if(B(`${e} mark \`${n}\``,i,[`expand`]),typeof i.expand!=`string`||!_r.includes(i.expand))throw N.schema(`${e} mark \`${n}\`: \`expand\` is after, before, both, or none`);P(r,n,{expand:i.expand})}n.marks=r}if(t.readOnly!==void 0){if(typeof t.readOnly!=`boolean`)throw N.schema(`${e}: \`readOnly\` is a boolean`);t.readOnly&&(n.readOnly=!0)}if(t.owns!==void 0){if(typeof t.owns!=`boolean`)throw N.schema(`${e}: \`owns\` is a boolean`);t.owns&&(n.owns=!0)}if(t.cascade!==void 0){if(typeof t.cascade!=`boolean`||t.cascade&&n.kind!==`tree`)throw N.schema(`${e}: cascade requires a tree and a boolean`);t.cascade&&(n.cascade=!0)}if(t.position!==void 0){if(typeof t.position!=`boolean`)throw N.schema(`${e}: \`position\` is a boolean`);t.position&&(n.position=!0)}if(t.display!==void 0&&t.display!==null){let r=t.display,i=e=>typeof e==`number`&&Number.isFinite(e)&&e>0;if(!z(r)||r.kind!==`time`||!i(r.perSecond)||r.fps!==void 0&&r.fps!==null&&!i(r.fps))throw N.schema(`${e}: \`display\` is { kind: "time", perSecond, fps? } with positive numbers`);B(`${e} display`,r,[`kind`,`perSecond`,`fps`]),n.display={kind:`time`,perSecond:r.perSecond,...i(r.fps)?{fps:r.fps}:{}}}if(t.sample!==void 0&&t.sample!==null){if(typeof t.sample!=`boolean`)throw N.schema(`${e}: \`sample\` is a boolean`);n.sample=t.sample}return n}function wr(e){return e===``?`a mark key is not empty`:e.startsWith(`$`)?"names starting with `$` are reserved":e.includes(`:`)?"a mark key holds no `:`":e===`__proto__`?"`__proto__` is not a mark key":null}let Tr=e=>e.kind===`text`&&e.marks!==void 0&&Object.keys(e.marks).length>0;function Er(e){let t=/* @__PURE__ */ new Map;for(let n of R(e.types)){let r=e.types[n].fields;for(let e of R(r)){let n=r[e].marks??{};for(let e of R(n))t.set(e,n[e].expand)}}return t}function Dr(e){if(!z(e))throw N.schema(`a schema is a JSON object`);if(B(`schema`,e,[`kdm`,`kit`,`schemaVersion`,`root`,`types`]),!Sr(e.kdm))throw N.schema("`kdm` is required and is a whole number");if(typeof e.kit!=`string`)throw N.schema("`kit` is required and is a string");if(!Sr(e.schemaVersion))throw N.schema("`schemaVersion` is required and is a whole number");if(typeof e.root!=`string`)throw N.schema("`root` is required and is a string");if(!z(e.types))throw N.schema("`types` is required and is an object");let t=e.types,n={};for(let r of R(e.types)){let i=e.types[r];if(!z(i))throw N.schema(`type \`${r}\` is an object`);if(B(`type \`${r}\``,i,[`fields`,`projected`,`projectedOps`,`batchOps`,`creation`,`readOnly`,`readOnlyWhen`,`toolId`,`pathName`,`label`,`derived`,`text`,`batchIndependent`,`moveConsequences`,`structuralConsequences`]),!z(i.fields))throw N.schema(`type \`${r}\` needs \`fields\``);let a={};for(let e of R(i.fields))P(a,e,Cr(`\`${r}.${e}\``,i.fields[e]));let o={fields:a};if(i.projected!==void 0){if(typeof i.projected!=`boolean`)throw N.schema(`type \`${r}\`: projected is a boolean`);i.projected&&(o.projected=!0)}if(i.projectedOps!==void 0){if(!o.projected||!z(i.projectedOps))throw N.schema(`type \`${r}\`: projectedOps requires projected:true`);let e={};for(let[t,n]of Object.entries(i.projectedOps)){if(!t||!z(n)||typeof n.command!=`string`||!n.command||Object.keys(n).some(e=>![`command`,`params`].includes(e))||n.params!==void 0&&(!z(n.params)||Object.entries(n.params).some(([e,t])=>!e||typeof t!=`string`||t.split(`.`).some(e=>!e))))throw N.schema(`type \`${r}\`: projectedOps needs an operation, command and parameter paths`);P(e,t,{command:n.command,...z(n.params)?{params:{...n.params}}:{}})}Object.keys(e).length&&(o.projectedOps=e)}if(i.structuralConsequences!==void 0){let e=i.structuralConsequences;if(!Array.isArray(e)||e.some(e=>!z(e)||Object.keys(e).some(e=>![`container`,`on`,`fields`].includes(e))||typeof e.container!=`string`||!Object.hasOwn(t,e.container)||!Array.isArray(e.on)||!e.on.length||e.on.some(e=>![`create`,`delete`,`move`,`rename`].includes(e)&&!(e===`set`&&Object.values(t).some(e=>z(e)&&e.projected===!0)))||!Array.isArray(e.fields)||!e.fields.length||e.fields.some(e=>typeof e!=`string`||e.split(`.`).some(e=>!e)||!a[e.split(`.`)[0]]||e.includes(`.`)&&![`json`,`map`].includes(a[e.split(`.`)[0]].kind))))throw N.schema(`type \`${r}\`: structuralConsequences needs a declared container, structural actions and stored field paths`);e.length&&(o.structuralConsequences=e.map(e=>({container:e.container,on:[...e.on],fields:[...e.fields]})))}if(i.moveConsequences!==void 0){if(!Array.isArray(i.moveConsequences)||i.moveConsequences.some(e=>{if(typeof e!=`string`||e.split(`.`).some(e=>!e))return!0;let t=a[e.split(`.`)[0]];return!t?.position||e.includes(`.`)&&t.kind!==`json`}))throw N.schema(`type \`${r}\`: moveConsequences must name position fields or their JSON members`);i.moveConsequences.length&&(o.moveConsequences=[...i.moveConsequences])}if(i.creation!==void 0){if(!z(i.creation))throw N.schema(`creation is an object`);B(`creation`,i.creation,[`fields`,`command`,`fixtures`]);for(let e of[`fields`,`fixtures`])if(i.creation[e]!==void 0&&!z(i.creation[e]))throw N.schema(`creation.${e} is an object`);let e=i.creation.command;if(e!==void 0){if(!z(e)||typeof e.command!=`string`||!e.command)throw N.schema(`creation command required`);if(B(`creation command`,e,[`command`,`params`]),e.params!==void 0&&(!z(e.params)||Object.entries(e.params).some(([e,t])=>!e||typeof t!=`string`||t.split(`.`).some(e=>!e))))throw N.schema(`invalid creation command paths`)}o.creation=structuredClone(i.creation)}if(i.batchOps!==void 0){if(!z(i.batchOps))throw N.schema(`type \`${r}\`: batchOps is a mapping`);let e={};for(let[t,n]of Object.entries(i.batchOps)){if(!t||!z(n))throw N.schema(`invalid batchOps operation`);if(n.direct===!0)B(`direct batchOps`,n,[`direct`]),e[t]={direct:!0};else{if(B(`command batchOps`,n,[`command`,`params`]),typeof n.command!=`string`||!n.command||n.params!==void 0&&(!z(n.params)||Object.entries(n.params).some(([e,t])=>!e||typeof t!=`string`||t.split(`.`).some(e=>!e))))throw N.schema(`invalid batchOps command`);e[t]={command:n.command,params:n.params}}}o.batchOps=e}if(i.batchIndependent!==void 0){if(typeof i.batchIndependent!=`boolean`)throw N.schema(`type \`${r}\`: \`batchIndependent\` is a boolean`);i.batchIndependent&&(o.batchIndependent=!0)}if(i.readOnly!==void 0){if(typeof i.readOnly!=`boolean`)throw N.schema(`type \`${r}\`: \`readOnly\` is a boolean`);i.readOnly&&(o.readOnly=!0)}if(i.readOnlyWhen!==void 0&&i.readOnlyWhen!==null){let e=i.readOnlyWhen;if(!z(e))throw N.schema(`type \`${r}\`: \`readOnlyWhen\` is { field, values }`);if(B(`type \`${r}\` readOnlyWhen`,e,[`field`,`values`]),typeof e.field!=`string`)throw N.schema(`type \`${r}\`: \`readOnlyWhen.field\` is a field name`);if(!Array.isArray(e.values)||!e.values.every(e=>typeof e==`string`))throw N.schema(`type \`${r}\`: \`readOnlyWhen.values\` is a list of strings`);o.readOnlyWhen={field:e.field,values:[...e.values]}}if(i.toolId!==void 0&&i.toolId!==null){let e=i.toolId;if(!z(e))throw N.schema(`type \`${r}\`: \`toolId\` is { field, prefix?, parent? }`);if(B(`type \`${r}\` toolId`,e,[`field`,`prefix`,`parent`]),typeof e.field!=`string`)throw N.schema(`type \`${r}\`: \`toolId.field\` is a field name`);if(e.prefix!==void 0&&typeof e.prefix!=`string`)throw N.schema(`type \`${r}\`: \`toolId.prefix\` is a string`);if(e.parent!==void 0&&e.parent!==null&&typeof e.parent!=`string`)throw N.schema(`type \`${r}\`: \`toolId.parent\` is a type name`);o.toolId={field:e.field,prefix:typeof e.prefix==`string`?e.prefix:``,...typeof e.parent==`string`?{parent:e.parent}:{}}}if(i.pathName!==void 0&&i.pathName!==null){let e=i.pathName,t=typeof e==`string`?a[e.split(`.`)[0]]?.kind:void 0,n=t===`json`||(t===`string`||t===`enum`)&&typeof e==`string`&&!e.includes(`.`);if(typeof e!=`string`||!n)throw N.schema(`type \`${r}\`: \`pathName\` \`${String(e)}\` names no string or enum field, nor a member of a json field`);o.pathName=e}for(let e of[`label`,`text`])if(i[e]!==void 0&&i[e]!==null){let t=i[e],n=typeof t==`string`?a[t.split(`.`)[0]]?.kind:void 0,s=n===`json`||(n===`string`||n===`text`||n===`enum`)&&typeof t==`string`&&!t.includes(`.`);if(typeof t!=`string`||!s)throw N.schema(`type \`${r}\`: \`${e}\` \`${String(t)}\` names no string, text or enum field, nor a member of a json field`);o[e]=t}if(i.derived!==void 0&&i.derived!==null){if(!z(i.derived))throw N.schema(`type \`${r}\`: \`derived\` is an object of names`);let e={};for(let t of R(i.derived)){if(!/^[A-Za-z0-9_]+$/.test(t))throw N.schema(`type \`${r}\`: derived \`${t}\` is no name (ASCII letters, digits and \`_\`)`);let n=i.derived[t];if(!z(n))throw N.schema(`type \`${r}\`: derived \`${t}\` is { description? }`);if(B(`type \`${r}\` derived \`${t}\``,n,[`description`,`premiseOnly`,`onDemand`]),n.description!==void 0&&typeof n.description!=`string`)throw N.schema(`type \`${r}\`: derived \`${t}\`.description is a string`);if(n.premiseOnly!==void 0&&typeof n.premiseOnly!=`boolean`)throw N.schema(`type \`${r}\`: derived \`${t}\`.premiseOnly is a boolean`);if(n.onDemand!==void 0&&typeof n.onDemand!=`boolean`)throw N.schema(`type \`${r}\`: derived \`${t}\`.onDemand is a boolean`);if(n.onDemand&&!n.premiseOnly)throw N.schema(`type \`${r}\`: onDemand derived facts require premiseOnly`);P(e,t,{...typeof n.description==`string`?{description:n.description}:{},...n.premiseOnly?{premiseOnly:!0}:{},...n.onDemand?{onDemand:!0}:{}})}Object.keys(e).length>0&&(o.derived=e)}P(n,r,o)}return{kdm:e.kdm,kit:e.kit,schemaVersion:e.schemaVersion,root:e.root,types:n}}function Or(e){if(e.kdm!==1)throw N.schema(`kdm ${e.kdm} is not supported; this build reads 1`);if(!Object.hasOwn(e.types,e.root))throw N.schema(`root type \`${e.root}\` is not declared`);if(e.types[e.root].projected)throw N.schema(`the root type cannot be projected`);let t=/* @__PURE__ */ new Map;for(let n of R(e.types)){if(n.startsWith(`$`))throw N.schema(`type \`${n}\`: names starting with \`$\` are reserved`);let r=e.types[n],i=r.readOnlyWhen;if(i!==void 0){if(r.readOnly)throw N.schema(`type \`${n}\`: \`readOnly\` and \`readOnlyWhen\` exclude each other`);if(!Object.hasOwn(r.fields,i.field))throw N.schema(`type \`${n}\`: \`readOnlyWhen\` names undeclared field \`${i.field}\``);let e=r.fields[i.field];if(e.kind!==`enum`)throw N.schema(`type \`${n}\`: \`readOnlyWhen\` names \`${i.field}\`, which is not an enum`);if(i.values.length===0)throw N.schema(`type \`${n}\`: \`readOnlyWhen\` needs \`values\``);let t=i.values.find(t=>!(e.values??[]).includes(t));if(t!==void 0)throw N.schema(`type \`${n}\`: \`readOnlyWhen\` value \`${t}\` is not a value of \`${n}.${i.field}\``)}r.toolId!==void 0&&kr(e,n,r.toolId);for(let i of R(r.fields)){let a=r.fields[i];if(i.startsWith(`$`)||i.startsWith(`~`))throw N.schema(`\`${n}.${i}\`: names starting with \`$\` or \`~\` are reserved`);let o=br.includes(a.kind),s=a.kind===`map`&&a.owns===!0;if(a.of!==void 0&&!o&&!s)throw N.schema(`\`${n}.${i}\`: only ref, list, and tree fields take \`of\``);if(a.owns&&(a.kind===`list`||a.kind===`tree`))throw N.schema(`\`${n}.${i}\`: only ref fields take \`owns\` (list and tree fields always own their items)`);if(a.owns&&a.kind!==`ref`&&a.kind!==`map`)throw N.schema(`\`${n}.${i}\`: only ref and map fields take \`owns\``);if(a.values?.length&&a.kind!==`enum`)throw N.schema(`\`${n}.${i}\`: only enum fields take \`values\``);if(a.kind===`enum`&&!a.values?.length)throw N.schema(`\`${n}.${i}\`: an enum needs \`values\``);if(o||s&&a.of!==void 0){if(a.of===void 0)throw N.schema(`\`${n}.${i}\` needs \`of\``);if(!Object.hasOwn(e.types,a.of))throw N.schema(`\`${n}.${i}\` refers to undeclared type \`${a.of}\``)}let c=a.marks??{},l=R(c);if(l.length>0&&a.kind!==`text`)throw N.schema(`\`${n}.${i}\`: only text fields take \`marks\``);for(let e of l){let r=wr(e);if(r!==null)throw N.schema(`\`${n}.${i}\` mark \`${e}\`: ${r}`);let a=c[e].expand,o=t.get(e);if(o!==void 0&&o!==a)throw N.schema(`\`${n}.${i}\` mark \`${e}\` expands \`${a}\`, but it is declared elsewhere expanding \`${o}\`; a mark key has one expand in a schema`);t.set(e,a)}}}}function kr(e,t,n){let r=e=>N.schema(`type \`${t}\`: \`toolId\` ${e}`),i=e.types[t].fields;if(!Object.hasOwn(i,n.field))throw r(`names undeclared field \`${n.field}\``);let a=i[n.field];if(a.kind!==`integer`&&a.kind!==`string`)throw r(`names \`${n.field}\`, which is not an integer or a string`);if(!a.readOnly)throw r(`names \`${n.field}\`, which is not \`readOnly\`: only the kit writes an object's tool id`);let o=mr(n.prefix);if(o!==null)throw r(`prefix \`${n.prefix}\`: ${o}`);let s=n.parent;if(s===void 0)return;if(!Object.hasOwn(e.types,s))throw r(`names undeclared parent type \`${s}\``);let c=e.types[s];if(c.toolId===void 0)throw r(`names parent \`${s}\`, which declares no \`toolId\``);if(!Object.values(c.fields).some(e=>(e.kind===`list`||e.kind===`tree`||e.kind===`ref`||e.kind===`map`&&e.owns)&&e.of===t))throw r(`names parent \`${s}\`, which has no list, tree, or ref field of \`${t}\``);let l=[],u=s;for(;u!==void 0;){if(u===t)throw r(`parent \`${s}\` leads back to \`${t}\`: parents form no cycle`);if(l.includes(u))break;l.push(u),u=Object.hasOwn(e.types,u)?e.types[u].toolId?.parent:void 0}}function Ar(e){let t=e;if(typeof e==`string`)try{t=JSON.parse(e)}catch(e){throw N.schema(String(e))}let n=Dr(t);return Or(n),n}function jr(e,t){if(!Object.hasOwn(e.types,t))throw N.unknownType(t);return e.types[t]}function Mr(e,t,n){let r=jr(e,t);if(!Object.hasOwn(r.fields,n))throw N.unknownField(t,n,Object.keys(r.fields).sort());return r.fields[n]}function Nr(e,t,n,r){Pr(e,t,n,r)}function Pr(e,t,n,r){let i=Mr(e,t,n);if(xr(i.kind))throw N.invalidValue(t,n,zr(i.kind));if(r==null){if(i.optional)return null;throw N.invalidValue(t,n,`is required`)}let a=lr(r,e=>N.invalidValue(t,n,e));if(Ir(t,n,a,i),i.members!==void 0&&Object.keys(i.members).length>0){for(let[e,r]of Object.entries(i.members))for(let i of Fr(a,e))if(!(Array.isArray(i)||z(i)||i===null&&r.nullable)){if(typeof i!=`number`)throw N.invalidValue(t,n,`bounded members require a number or an explicitly nullable null`);Ir(t,n,i,r)}}let o;switch(i.kind){case`string`:case`ref`:o=typeof a==`string`;break;case`number`:o=typeof a==`number`;break;case`integer`:o=typeof a==`number`&&Number.isInteger(a)&&Math.abs(a)<=ur;break;case`boolean`:o=typeof a==`boolean`;break;case`enum`:o=typeof a==`string`&&(i.values??[]).includes(a);break;case`blob`:o=z(a)&&typeof a.hash==`string`&&typeof a.mime==`string`&&typeof a.size==`number`&&Number.isInteger(a.size)&&a.size>=0&&a.size<=ur;break;default:o=!0}if(!o&&i.kind===`enum`)throw N.invalidValue(t,n,Rr(i.values??[],a));if(!o)throw N.invalidValue(t,n,`expects ${/^[aeiou]/.test(i.kind)?`an`:`a`} ${i.kind} value`);return a}function Fr(e,t){if(t===``)return[e];let n=t.search(/[.\[]/),r=n<0?t:t.slice(0,n);return r===`*`?z(e)?Object.values(e).flatMap(e=>Fr(e,t.slice(r.length))):[]:r===``?t.startsWith(`[]`)?Array.isArray(e)?e.flatMap(e=>Fr(e,t.slice(2))):[]:t.startsWith(`.`)?Fr(e,t.slice(1)):[]:z(e)&&Object.hasOwn(e,r)?Fr(e[r],t.slice(r.length)):[]}function Ir(e,t,n,r){if(typeof n!=`number`)return;let i=r.multipleOf===void 0?0:n/r.multipleOf;if(r.minimum!==void 0&&n<r.minimum||r.maximum!==void 0&&n>r.maximum||!Number.isFinite(i)||Math.abs(i-Math.round(i))>1e-9)throw N.invalidValue(e,t,`is outside minimum/maximum or is not a multipleOf the declared step`)}function Lr(e,t,n,r,i){let a=Mr(e,t,n);if(a.of!==void 0&&(typeof i!=`string`||i===``))throw N.invalidValue(t,n,`map entry \`${r}\` expects the id of a \`${a.of}\``);for(let[e,o]of Object.entries(a.members??{}))for(let a of Fr({[r]:i},e))if(!(Array.isArray(a)||z(a)||a===null&&o.nullable)){if(typeof a!=`number`)throw N.invalidValue(t,n,`map entry \`${r}\` requires a number`);Ir(t,n,a,o)}}function Rr(e,t){let n=e.slice(0,20).map(e=>`\`${e}\``),r=Math.max(0,e.length-20),i=r>0?`, and ${r} more (\`describe\` lists them all)`:``,a=typeof t==`string`?`\`${t}\``:typeof t==`number`?`a number`:typeof t==`boolean`?`a boolean`:Array.isArray(t)?`an array`:t===null?`null`:`an object`;return`takes one of ${n.join(`, `)}${i}, not ${a}`}function zr(e){return`is a container, not a value: change it with ${{text:`text.insert, text.delete, text.set, text.mark, text.unmark`,list:`list.insert, list.move, list.remove`,tree:`tree.insert, tree.move`,map:`map.set, map.delete`}[e]??`its operations`}, or set the fields of the objects it holds`}let Br=(e,t)=>{try{return e()}catch{return t}};function Vr(e){return{objectsOfType:t=>e.objectsOfType(t),register:(t,n)=>Br(()=>e.get(t,n),null),held:(t,n,r)=>{if(r===`list`)return Br(()=>e.list(t,n),[]);if(r===`tree`)return Br(()=>e.tree(t,n),[]).map(e=>e.object).filter(t=>e.exists(t));if(r===`ref`){let r=Br(()=>e.get(t,n),null);return typeof r==`string`?[r]:[]}return[]},map:(t,n)=>Object.fromEntries(Br(()=>e.mapEntries(t,n),[])),exists:t=>e.exists(t)}}let Hr=e=>typeof e==`object`&&!!e&&!Array.isArray(e);function Ur(e){let t=Hr(e)&&Hr(e.objects)?e.objects:{},n=(e,n)=>{let r=Object.hasOwn(t,e)?t[e]:void 0;return Hr(r)&&Object.hasOwn(r,n)?r[n]:null},r=(e,t)=>{for(let n of Array.isArray(e)?e:[])Hr(n)&&(typeof n.object==`string`&&t.push(n.object),r(n.children,t))};return{objectsOfType:e=>Object.keys(t).filter(n=>Hr(t[n])&&t[n].$type===e).sort(I),register:n,held:(e,i,a)=>{let o=n(e,i),s=[];return a===`list`&&Array.isArray(o)?s.push(...o.filter(e=>typeof e==`string`)):a===`tree`?r(o,s):a===`ref`&&typeof o==`string`&&s.push(o),s.filter(e=>Object.hasOwn(t,e))},map:(e,t)=>n(e,t),exists:e=>Object.hasOwn(t,e)}}function Wr(e,t,n){let r=n.map(([n,r])=>{let i=r===void 0?void 0:e.types[r]?.pathName;if(i===void 0)return null;let[a,...o]=i.split(`.`),s=t.register(n,a);for(let e of o)s=Hr(s)?s[e]??null:null;return{text:typeof s==`string`?s:null}});return r.map((e,t)=>{if(e===null)return null;let n=e.text,i=n!==null&&n!==``&&!/[/.#@]/.test(n)&&!/^\d+$/.test(n)?n:null;if(i===null)return`#${t+1}`;let a=r.slice(0,t).filter(e=>e!==null&&e.text===i).length;return a===0?i:`${i}#${a+1}`})}function Gr(e){return typeof e==`number`&&Number.isInteger(e)?String(e):typeof e==`string`&&e!==``?e:null}let Kr=e=>e.split(/[:/]/).pop()??e;function qr(e){let t=e.map(e=>`\`${e}\``);return t.length<=1?t.join(``):`${t.slice(0,-1).join(`, `)} and ${t[t.length-1]}`}var V=class e{constructor(e,t,n,r){this.active=e,this.own=t,this.named=n,this.owners=r}static none(){return new e(!1,/* @__PURE__ */ new Map,/* @__PURE__ */ new Map,/* @__PURE__ */ new Map)}static ofReplica(t){return e.build(t.schema,Vr(t))}static ofState(t,n){return e.build(t,Ur(n))}static build(t,n){let r=R(t.types).flatMap(e=>{let n=t.types[e].toolId;return n?[[e,n]]:[]});if(r.length===0)return e.none();let i=/* @__PURE__ */ new Map;for(let[e,t]of r)for(let r of n.objectsOfType(e)){let a=Gr(n.register(r,t.field));a!==null&&i.set(r,{type:e,tool:t,value:a})}let a=/* @__PURE__ */ new Map;for(let[e,i]of r){let r=i.parent;if(r===void 0||!Object.hasOwn(t.types,r))continue;let o=t.types[r];for(let t of R(o.fields)){let i=o.fields[t];if(!(i.kind!==`list`&&i.kind!==`tree`&&i.kind!==`ref`||i.of!==e))for(let e of n.objectsOfType(r))for(let r of n.held(e,t,i.kind)){let t=a.get(r)??[];t.includes(e)||t.push(e),a.set(r,t)}}}let o=/* @__PURE__ */ new Map,s=(e,t)=>{let n=o.get(e);if(n!==void 0)return n;let r=i.get(e),c=null;if(r!==void 0){let n=`${r.tool.prefix}${r.value}`;if(r.tool.parent===void 0)c=n;else{let r=a.get(e);if(r!==void 0&&r.length===1&&t<=64){let e=s(r[0],t+1);e!==null&&(c=`${e}/${n}`)}}}return o.set(e,c),c};for(let e of[...i.keys()].sort(I))s(e,0);let c=/* @__PURE__ */ new Map;for(let[e,t]of o){let n=i.get(e);if(t===null||n===void 0)continue;let r=c.get(t)??[];r.push({id:e,type:n.type,value:n.value}),c.set(t,r)}let l=/* @__PURE__ */ new Map,u=/* @__PURE__ */ new Map;for(let e of[...c.keys()].sort(I)){let t=c.get(e).sort((e,t)=>I(e.type,t.type)||I(e.id,t.id));u.set(e,t),t.length===1&&l.set(t[0].id,e)}let d=/* @__PURE__ */ new Map,f=Object.values(t.types).some(e=>e.pathName!==void 0),p=/* @__PURE__ */ new Map;if(f)for(let e of R(t.types))for(let t of n.objectsOfType(e))p.set(t,e);for(let e of R(t.types)){let r=t.types[e].fields;for(let i of R(r)){let a=r[i];if(fr(a))for(let r of n.objectsOfType(e)){let e=a.kind===`map`?pr(n.map(r,i),e=>n.exists(e)):n.held(r,i,a.kind).map((e,t)=>[e,a.kind===`ref`?null:String(t)]),o=f&&a.kind===`list`?Wr(t,n,e.map(([e])=>[e,p.get(e)])):[];e.forEach(([e,t],n)=>{let a=d.get(e)??[];a.some(e=>e.owner===r&&e.field===i)||a.push({owner:r,field:i,at:t,name:o[n]??null}),d.set(e,a)})}}}let m=e=>{let t=l.get(e);if(t!==void 0)return t;let n=d.get(e);return n!==void 0&&n.length===1&&n[0].at!==null&&/^\d+$/.test(n[0].at)?n[0].at:null},h=e=>{let t=e.indexOf(`.`),n=t<0?e:e.slice(0,t),r=t<0?null:e.slice(t+1),i=m(n);if(i===null){let e=n.split(`:`).map(m);if(e.some(e=>e===null))return null;i=e.join(`:`)}return r===null?i:`${i}.${r}`},g=[];for(let e of d.values())for(let t of e){let e=t.at===null?null:h(t.at);e!==null&&g.push([t,e])}for(let[e,t]of g)e.at=t;return new e(!0,l,u,d)}attributed(e){if(!this.active||this.own.has(e))return null;let t=[],n=e;for(let e=0;e<64;e+=1){let e=this.owners.get(n);if(e===void 0||e.length!==1)return null;let[r]=e;r.at!==null&&t.push(r.at),t.push(r.field);let i=this.own.get(r.owner);if(i!==void 0)return{holder:i,path:t.reverse().join(`.`)};n=r.owner}return null}namedPath(e){if(!this.active||this.own.has(e))return null;let t=[],n=[],r=e;for(let e=0;e<64;e+=1){let e=this.owners.get(r);if(e===void 0||e.length!==1)return null;let[i]=e;if(i.name===null?t.length===0&&(i.at!==null&&n.push(i.at),n.push(i.field)):t.push(i.name),this.own.has(i.owner)){if(t.length===0)return null;let e=t.reverse().join(`/`);return n.length===0?e:`${e}.${n.reverse().join(`.`)}`}r=i.owner}return null}typeOf(e){let t=this.named.get(e);return t!==void 0&&t.length===1?t[0].type:null}isActive(){return this.active}toolId(e){return this.own.get(e)??null}showJson(e){return typeof e==`string`?this.show(e):Array.isArray(e)?e.map(e=>this.showJson(e)):Hr(e)?Object.fromEntries(Object.entries(e).map(([e,t])=>[this.toolId(e)??e.split(`:`).map(e=>this.show(e)).join(`:`),this.showJson(t)])):e}show(e){return this.own.get(e)??e}showText(e){return Array.isArray(e)?e.map(e=>Hr(e)&&Object.hasOwn(e,`attributes`)?{...e,attributes:this.showJson(e.attributes)}:e):e}resolve(e,t){if(!this.active)return e;let n=this.named.get(e);if(n!==void 0&&n.length===1)return n[0].id;if(n!==void 0&&n.length>1){let t=n.map(e=>`the \`${e.type}\` \`${e.id}\``),r=`${t.slice(0,-1).join(`, `)} and ${t[t.length-1]}`;return{reason:`ambiguous`,message:`\`${e}\` names ${n.length} objects (${r}), so it names none of them: give one of their object ids, which they answer until the kit gives each an id of its own`}}return t(e)?e:{reason:`not-found`,message:this.notFound(e)}}notFound(e){let t=Kr(e),n=[...this.named.entries()].filter(([,e])=>e.some(e=>e.value===t)).map(([e])=>e);if(n.length>0){let r=n.slice(0,8),i=n.length>r.length?`, and ${n.length-r.length} more`:``;return`no object has the id \`${e}\`; these end in \`${t}\`: ${qr(r)}${i} (an id is written whole, as query and load answer it)`}let r=e.slice(0,e.length-t.length);if(r!==``){let t=[...this.named.keys()].filter(e=>e.startsWith(r));if(t.length>0&&t.length<=8)return`no object has the id \`${e}\`; the ids starting \`${r}\` are ${qr(t)}`;if(t.length>0)return`no object has the id \`${e}\`; ${t.length} ids start \`${r}\`: query or load answers them`}return`no object has the id \`${e}\`: give an id that query or load answers, \`root\`, or the \`$name\` of an object created earlier in the batch`}};let Jr=e=>typeof e!=`string`,Yr=e=>Object.values(e.types).some(e=>e.projected),H=e=>new U(`invalid-value`,null,e),Xr=(e,t,n)=>Object.defineProperty(e,t,{value:n,writable:!0,enumerable:!0,configurable:!0}),Zr=(e,t)=>(t.kind===`list`||t.kind===`tree`||t.kind===`ref`&&t.owns)&&!!t.of&&!!e.types[t.of]?.projected;var Qr=class{state;objects=/* @__PURE__ */ new Map;constructor(e,t){this.state=V.ofReplica(e).showJson(e.stateJson());let n=this.state.objects,r=e.schema;for(let e of t){if(!e.id||e.id===`root`||e.id.startsWith(`$`)||Object.hasOwn(n,e.id)||this.objects.has(e.id))throw H(`projected id \`${e.id}\` is reserved, duplicated or shadows a stored object`);let t=Object.hasOwn(r.types,e.type)?r.types[e.type]:void 0;if(!t?.projected)throw H(`\`${e.type}\` is not a projected type`);for(let[n,i]of Object.entries(e.fields)){let a=Object.hasOwn(t.fields,n)?t.fields[n]:void 0;if(!a)throw H(`unknown projected field \`${e.type}.${n}\``);let o;if(a.kind===`text`)o=typeof i==`string`||!!a.marks&&Object.keys(a.marks).length>0&&Array.isArray(i);else if(a.kind===`list`||a.kind===`tree`)o=Array.isArray(i);else if(a.kind===`map`)o=typeof i==`object`&&!!i&&!Array.isArray(i);else try{Nr(r,e.type,n,i),o=!0}catch{o=!1}if(!o)throw H(`invalid projected value \`${e.type}.${n}\``)}for(let[n,i]of Object.entries(t.fields)){let t=Zr(r,i);if(!i.optional&&!t&&!Object.hasOwn(e.fields,n))throw H(`missing projected field \`${e.type}.${n}\``)}Xr(n,e.id,{...structuredClone(e.fields),$type:e.type}),this.objects.set(e.id,structuredClone(e))}let i=/* @__PURE__ */ new Map,a=(e,t)=>JSON.stringify([e,t]);for(let[e,t]of Object.entries(n))for(let[n,o]of Object.entries(r.types[t.$type].fields))if(Zr(r,o)){if(t[n]!==null&&t[n]!==void 0&&(!Array.isArray(t[n])||t[n].length))throw H(`\`${e}.${n}\`: projected membership must come from object placements`);i.set(a(e,n),{parent:e,field:n,kind:o.kind,groups:/* @__PURE__ */ new Map})}for(let e of this.objects.values()){if(!e.at)continue;let t=e.at,o=i.get(a(t.parent,t.field));if(!o)throw H(`\`${e.id}\` has no projected container \`${t.parent}.${t.field}\``);if(t.node!==void 0){let e=this.objects.get(t.node)?.at;if(o.kind!==`tree`||e?.parent!==t.parent||e.field!==t.field)throw H(`projected tree parent belongs to another container`)}let s=n[t.parent];if(r.types[s.$type].fields[t.field].of!==e.type)throw H(`projected placement has the wrong type for \`${e.id}\``);let c=t.index??0;if(!Number.isSafeInteger(c)||c<0)throw H(`projected index is a nonnegative integer`);let l=o.groups.get(t.node)??[];l.push([c,e.id]),o.groups.set(t.node,l)}for(let{parent:e,field:t,kind:r,groups:a}of i.values()){for(let n of a.values())if(n.sort(([e,t],[n,r])=>e-n||t.localeCompare(r)),n.some(([e],t)=>e!==t))throw H(`\`${e}.${t}\`: projected indexes must be contiguous and unique`);let i=a.get(void 0)??[],o;if(r===`ref`){if(i.length>1)throw H(`a projected owning ref holds at most one object`);o=i[0]?.[1]??null}else if(r===`tree`){let e=/* @__PURE__ */ new Set,t=(n,r)=>{if(r>64)throw H(`projected tree exceeds depth limit`);return(a.get(n)??[]).map(([,n])=>{if(e.has(n))throw H(`projected placements contain a cycle`);return e.add(n),{object:n,children:t(n,r+1)}})};if(o=t(void 0,0),e.size!==[...a.values()].reduce((e,t)=>e+t.length,0))throw H(`projected placements contain an unreachable tree cycle`)}else o=i.map(([,e])=>e);Xr(n[e],t,o)}for(let e of this.objects.values()){let t=/* @__PURE__ */ new Set([e.id]),n=e;for(;n.at&&(n=this.objects.get(n.at.parent));){if(t.has(n.id))throw H(`projected placements contain a cycle`);t.add(n.id)}}}load(e,t,n){let r=t===`root`?e??``:t;if(!Object.hasOwn(this.state.objects,r))throw new U(`not-found`,null,`no object \`${t}\``);let i=this.state.objects[r],a=n??Object.keys(i).filter(e=>e!==`$type`);if(!Array.isArray(a)||a.some(e=>typeof e!=`string`))throw H(`fields lists names`);let o=Object.fromEntries(a.map(e=>{if(!Object.hasOwn(i,e))throw H(`unknown field \`${e}\``);return[e,structuredClone(i[e])]})),s=this.objects.get(r);return{id:r,type:i.$type,fields:o,...s?{projected:!0,...s.at?{at:{...s.at}}:{}}:{}}}};function $r(e,t,n,r){let i=structuredClone(r),a=i.op,o=i.target===`root`?n:typeof i.target==`string`?i.target:``,s=t.objects.get(o)?.type;if(a===`create`)s=typeof i.type==`string`&&e.types[i.type]?.projected?i.type:void 0;else if((!s||a===`set`||a.startsWith(`list.`)||a.startsWith(`tree.`))&&Object.hasOwn(t.state.objects,o)&&typeof i.field==`string`){let n=t.state.objects[o],r=e.types[n.$type].fields[i.field];if(r?.of&&e.types[r.of]?.projected){let t=e.types[n.$type];if(r.readOnly||t.readOnly||t.readOnlyWhen&&t.readOnlyWhen.values.includes(n[t.readOnlyWhen.field]))throw new U(`read-only`,null,`the projected container is read-only`);s=r.of;let c=a===`list.move`?i.from:i.index,l=n[i.field],u=i.item??i.node??(r.kind===`ref`?i.value:void 0)??(Array.isArray(l)&&typeof c==`number`?l[c]:void 0);u!==void 0&&(i.in=`${o}.${i.field}`,i.target=u,r.kind===`ref`&&a===`set`&&(a=`ref.set`,i.op=a,delete i.field))}}if(!s)return;if(typeof i.in==`string`){let r=i.in.lastIndexOf(`.`);if(r<1)throw H(`a projected destination names parent.field`);let a=i.in.slice(0,r),o=t.state.objects[a===`root`?n:a],c=o&&e.types[o.$type],l=c?.fields[i.in.slice(r+1)];if(!l||!Zr(e,l)||l.of!==s)throw H(`projected destination must own the object's type`);if(l.readOnly||c.readOnly||c.readOnlyWhen&&c.readOnlyWhen.values.includes(o[c.readOnlyWhen.field]))throw new U(`read-only`,null,`the projected container is read-only`)}let c=e.types[s],l=typeof i.field==`string`?i.field:void 0,u=t.state.objects[typeof i.target==`string`?i.target:o];if(c.readOnly||c.readOnlyWhen&&c.readOnlyWhen.values.includes(u?.[c.readOnlyWhen.field])||l&&c.fields[l]?.readOnly)throw new U(`read-only`,null,`projected \`${s}\` is read-only`);if(a===`set`){if(!l)throw H(`a projected set needs a field`);try{Nr(e,s,l,i.value)}catch(e){throw H(String(e))}}let d=(l?c.projectedOps?.[`${a}.${l}`]:void 0)??c.projectedOps?.[a];if(!d)throw new U(`read-only`,null,`no projected command mapping for \`${s}.${a}\``);let f={};for(let[e,t]of Object.entries(d.params??{})){let n=i;for(let e of t.split(`.`))n=typeof n==`object`&&n&&Object.hasOwn(n,e)?n[e]:void 0;n!==void 0&&Xr(f,e,n)}return{command:d.command,params:f,...a===`create`?{creates:s}:{}}}let ei=`KitObjectModel 1.0`;var U=class extends Error{code;index;constructor(e,t,n){super(n),this.name=`BatchError`,this.code=e,this.index=t}toJSON(){return{code:this.code,index:this.index,message:this.message}}};function ti(e){return{replica:()=>e,transact:(t,n)=>e.transact(t,n),command:e=>{throw Error(`unknown command \`${e}\`: this document runs no commands`)}}}let ni=/* @__PURE__ */ new Set([`create`,`set`,`delete`,`text.insert`,`text.delete`,`text.set`,`text.mark`,`text.unmark`,`list.insert`,`list.move`,`list.remove`,`map.set`,`map.delete`,`tree.insert`,`tree.move`,`command`]);function ri(e){if(e==null)return{kind:`extension`,principal:`script`};let t=()=>new U(`invalid-op`,null,`origin is { kind: person | agent | extension, principal, label? }`);try{er(e)}catch{throw t()}if(![`person`,`agent`,`extension`].includes(e.kind))throw t();return e.label===void 0?{kind:e.kind,principal:e.principal}:{kind:e.kind,principal:e.principal,label:e.label}}let ii=e=>typeof e==`object`&&!!e&&!Array.isArray(e);function ai(e,t){if(typeof e==`string`)return t.get(e)??e;if(Array.isArray(e))return e.map(e=>ai(e,t));if(ii(e)){let n={};for(let[r,i]of Object.entries(e))n[r]=ai(i,t);return n}return e}let oi=e=>e.objectsOfType(e.schema.root)[0]??null;function si(e,t){return t instanceof U?t:t instanceof M?new U(t.reason===`not-found`?`not-found`:`invalid-value`,e,t.message):new U(`invalid-value`,e,t instanceof Error?t.message:String(t))}let ci=(e,t)=>new U(t.reason===`not-found`?`not-found`:`invalid-op`,e,t.message);function li(e,t){let n=[`target`,`item`,`node`,`parent`,`id`].includes(e);return t==null?`\`${e}\` is missing: give a string${n?` naming an object by its id`:``}`:typeof t==`number`&&n&&Number.isSafeInteger(t)&&t>=0?`\`${e}\` names an object by its id as a string: give "${t}" for the number ${t}`:typeof t==`number`?`\`${e}\` is a string; got the number ${t}`:typeof t==`boolean`?`\`${e}\` is a string; got ${t}`:Array.isArray(t)?`\`${e}\` is a string; got an array`:typeof t==`object`?`\`${e}\` is a string; got an object`:`\`${e}\` is a string`}function ui(e,t,n,r,i,a,o=`target`){if(typeof e!=`string`)throw new U(`invalid-op`,a,li(o,e));if(e===`root`){let e=i();if(e===null)throw new U(`not-found`,a,`the document has no root object`);return e}if(e.startsWith(`$`)){let n=t.get(e);if(n===void 0)throw new U(`not-found`,a,`\`${e}\` is not bound in this batch`);return n}if(e===``)throw new U(`invalid-op`,a,`a target is not empty`);let s=n.resolve(e,r);if(Jr(s))throw ci(a,s);return s}function di(e,t,n,r,i,a,o){if(typeof o!=`string`||!n.isActive()||o===``)return o;let s=t.types[i]?.fields[a];if(!s||!(s.kind===`ref`||s.kind===`map`&&s.of!==void 0))return o;let c=n.resolve(o,t=>e.exists(t));if(!Jr(c))return c;if(c.reason===`not-found`)return o;throw ci(r,c)}function fi(e,t,n){if(!e.isActive())return n;let r=t=>typeof t==`string`?e.show(t):t;return t===`ref`?r(n):t===`text`?e.showText(n):t===`list`&&Array.isArray(n)?n.map(r):t===`tree`&&Array.isArray(n)?n.map(e=>({...e,object:r(e.object),parent:r(e.parent)})):t===`map`||t===`json`?e.showJson(n):n}function pi(e,t){let n=ii(t)?t.op:void 0;if(typeof n!=`string`)throw new U(`invalid-op`,e,"an operation is an object with an `op`");if(!ni.has(n))throw new U(`invalid-op`,e,`unknown operation \`${n}\``);return n}let W=(e,t,n)=>{let r=t[n];if(typeof r!=`string`)throw new U(`invalid-op`,e,li(n,r));return r},mi=(e,t,n)=>{let r=t[n];if(typeof r!=`number`||!Number.isSafeInteger(r)||r<0)throw new U(`invalid-op`,e,`\`${n}\` is a whole number`);return r};function hi(e,t,n){if(t==null||t===`end`)return n;if(typeof t!=`number`||!Number.isSafeInteger(t)||t<0)throw new U(`invalid-op`,e,'`at` is "end" or a whole number');return t}function gi(e,t){let n=0,r=0;for(let i of e){if(r===t)return n;n+=i.length,r+=1}return n+(t-r)}async function _i(e,t){if(!ii(t))throw new U(`invalid-op`,null,`a batch is an object`);if(t.objects!==void 0&&typeof t.objects!=`boolean`)throw new U(`invalid-op`,null,"`objects` is a boolean");let n=ri(t.origin),r=(e,t)=>{if(e==null)return[];if(!Array.isArray(e))throw new U(`invalid-op`,null,`\`${t}\` is an array`);return e},i=r(t.ops,`ops`),a=r(t.load,`load`),o=!1;if(i.forEach((e,t)=>{o=ni.has(pi(t,e))||o}),o&&e.readOnly?.())throw new U(`read-only`,null,`the document is read-only`);let s=(await e.replica()).schema,c=hr(s),l=async()=>c?V.ofReplica(await e.replica()):V.none(),u=/* @__PURE__ */ new Map,d=i.map(()=>null),f=Yr(s),p=async()=>{if(!e.projectedObjects)throw new U(`invalid-value`,null,`this adapter cannot read declared projected objects`);let t=await e.projectedObjects();return new Qr(await e.replica(),t)},m=async t=>{if(!f||t.op===`command`)return;let n=await p(),r=await e.replica(),i=oi(r);return $r(s,n,i?(await l()).show(i):``,ai(t,u))},h=0;for(;h<i.length;){let t=i[h],r=await m(t);if(r){if(r.creates&&t.ref!==void 0&&(typeof t.ref!=`string`||!t.ref.startsWith(`$`)||u.has(t.ref)))throw new U(`invalid-op`,h,`a projected create ref starts with $ and is unique`);let i;try{i=await e.command(r.command,r.params,n)}catch(e){throw new U(`command-failed`,h,String(e))}if(r.creates){let e=typeof i==`string`?i:ii(i)?i.id:void 0;if(typeof e!=`string`)throw new U(`invalid-op`,h,`a projected create command must return its id`);if((await p()).objects.get(e)?.type!==r.creates)throw new U(`invalid-op`,h,`a projected create command returned no live object of its type`);typeof t.ref==`string`&&u.set(t.ref,e)}d[h++]=i??null;continue}if(t.op===`command`){if(typeof t.id!=`string`)throw new U(`invalid-op`,h,"a command needs an `id`");let r={};if(t.params!==void 0&&c&&u.size>0){let e=await l();r=ai(t.params,new Map([...u].map(([t,n])=>[t,e.show(n)])))}else t.params!==void 0&&(r=ai(t.params,u));try{let i=t.at,a=u;if(c&&u.size>0){let e=await l();a=new Map([...u].map(([t,n])=>[t,e.show(n)]))}let o=Object.fromEntries(Object.entries({on:t.on,part:i?.part,time:i?.time}).filter(([,e])=>e!==void 0).map(([e,t])=>[e,ai(t,a)]));if(Object.keys(o).length>0&&!e.commandAt)throw Error(`this batch adapter cannot apply explicit command context`);if(d[h]=await(e.commandAt?e.commandAt(t.id,r,o,n):e.command(t.id,r,n))??null,typeof t.ref==`string`){let n=d[h],r=typeof n==`string`?n:ii(n)&&typeof n.id==`string`?n.id:void 0;if(!r)throw Error(`a bound command must return its object id`);let i=await e.replica(),a=V.ofReplica(i).resolve(r,e=>i.exists(e));if(typeof a!=`string`)throw Error(`a bound command returned no live object`);u.set(t.ref,a)}}catch(e){throw new U(`command-failed`,h,e instanceof Error?e.message:String(e))}h+=1;continue}let a=h;for(;h<i.length&&i[h].op!==`command`&&!await m(i[h]);)h+=1;let o=h,f=await l(),g=u,_=[];await e.transact(n,e=>{g=new Map(u),_=[];let t,n=()=>t===void 0?t=e.objectsOfType(s.root)[0]??null:t;for(let t=a;t<o;t+=1)_.push(Si(e,s,f,t,i[t],g,n))}),u=g,_.forEach((e,t)=>{d[a+t]=e})}let g=await l(),_=await e.replica(),ee=f?await p():void 0,te=a.map((e,t)=>{try{if(ee){if(!ii(e)||typeof e.target!=`string`)throw new U(`invalid-op`,null,li(`target`,ii(e)?e.target:void 0));return ee.load(oi(_)?g.show(oi(_)):null,u.get(e.target)??e.target,e.fields)}return Ti(_,g,e,u)}catch(e){let n=si(null,e);throw new U(n.code,null,`load ${t}: ${n.message}`)}});g.isActive()&&d.forEach((e,t)=>{d[t]=g.showJson(e)});let v={refs:Object.fromEntries([...u].map(([e,t])=>[e,g.show(t)])),results:d,loaded:te};return t.objects&&(v.objects=[...Object.keys(s.types).flatMap(e=>_.objectsOfType(e).map(t=>({id:g.show(t),type:e}))),...[...ee?.objects.values()??[]].map(e=>({id:e.id,type:e.type,projected:!0,at:e.at}))].sort((e,t)=>e.id<t.id?-1:+(e.id>t.id))),v}function vi(e,t,n,r,i,a){let o=e=>new U(`invalid-op`,r,`\`field\` \`${a}\`: ${e}`),s=i,c=a,l=a.split(`.`)[0];if(l.includes(`/`)||l.includes(`#`)){let r=null,l=[i],u=/* @__PURE__ */ new Set;for(;l.length>0;){let i=l.pop();if(u.has(i)||u.size>1e5)continue;u.add(i);let o=n.namedPath(i);if(o!==null&&a.startsWith(o)){let e=a.slice(o.length);(e===``||e.startsWith(`.`))&&(r===null||o.length>r[1])&&(r=[i,o.length])}let s;try{s=e.objectType(i)}catch{continue}if(Object.hasOwn(t.types,s))for(let n of R(t.types[s].fields)){let r=t.types[s].fields[n];if(r.kind===`list`)l.push(...e.listItems(i,n));else if(r.kind===`ref`&&r.owns===!0){let t=e.get(i,n);typeof t==`string`&&t!==``&&l.push(t)}}}if(r===null)throw o(`no object the target holds has that name in the kit's words`);s=r[0],c=a.slice(r[1]).replace(/^\.+/,``)}let u=c.split(`.`),d=0;for(;d<u.length;){let r=u[d],i=e.objectType(s);if(!Object.hasOwn(t.types,i)||!Object.hasOwn(t.types[i].fields,r))throw o(`\`${i}\` has no field \`${r}\``);let a=t.types[i].fields[r];if(d+1===u.length||![`ref`,`list`,`map`,`tree`].includes(a.kind)){let e=u.slice(d+1);if(e.length>0&&a.kind!==`json`)throw o(`\`${i}.${r}\` is no json value to go into`);return[s,r,e]}let c=null;if(a.kind===`ref`&&a.owns===!0){let t=e.get(s,r);c=typeof t==`string`&&t!==``?t:null}else if(a.kind===`list`){d+=1;let t=u[d];if(t===void 0||!/^\d+$/.test(t))throw o(`\`${r}\` is a list: give an index after it`);let n=Number(t),i=e.listItems(s,r);if(n>=i.length)throw o(`\`${r}\` has ${i.length} items, not ${n+1}`);c=i[n]}else if(a.kind===`map`&&a.owns===!0){d+=1;let t=u[d];if(t===void 0)throw o(`\`${r}\` is a map: give a key after it`);let n=e.mapGet(s,r,t);c=typeof n==`string`&&n!==``?n:null}else throw a.kind===`ref`||a.kind===`map`?o(`\`${i}.${r}\` does not own what it names: a path goes through owning fields only`):o(`\`${i}.${r}\` is a tree: a path does not go through trees`);if(c===null)throw o(`\`${i}.${r}\` holds nothing there`);if(n.toolId(c)!==null)throw o(`it reaches \`${n.show(c)}\`, which has a tool id of its own: target it by that id`);s=c,d+=1}throw o(`it names no field`)}function yi(e,t,n){if(t.length===0)return n;let r=e===void 0?null:JSON.parse(JSON.stringify(e)),i=r;for(let[e,r]of t.entries()){let a=e===t.length-1;if(Array.isArray(i)&&/^\d+$/.test(r)){let e=Number(r);if(e>=i.length)throw Error(a?`no item ${e}`:`no item ${e} at \`${r}\``);a?i[e]=n:i=i[e]}else{if(typeof i!=`object`||!i||Array.isArray(i))throw Error(`\`${r}\` is in no object`);let e=i;a?e[r]=n:(Object.hasOwn(e,r)||(e[r]=null),i=e[r])}}return r}function G(e,t,n,r,i,a){let o;try{o=e.objectType(i)}catch{return}if(!Object.hasOwn(t.types,o))return;let s=t.types[o];if(gr(s,t=>{try{return e.get(i,t)}catch{return}}))throw new U(`read-only`,r,`\`${n.show(i)}\` is a read-only \`${o}\`: the kit keeps it as it is`);if(a!==null&&Object.hasOwn(s.fields,a)&&s.fields[a].readOnly)throw new U(`read-only`,r,`\`${o}.${a}\` is read-only: the kit keeps it as it is`)}function bi(e,t,n,r){if(!Object.hasOwn(e.types,n))return;let i=e.types[n];if(gr(i,e=>Object.hasOwn(r,e)?r[e]:void 0))throw new U(`read-only`,t,`a new \`${n}\` would be read-only: only the kit makes one`)}function xi(e,t,n,r,i){if(!Object.hasOwn(t.types,r))return;let a=t.types[r];for(let[t,o]of i){if(!Object.hasOwn(a.fields,t)||typeof o!=`string`)continue;let i=a.fields[t];if(!(i.kind===`ref`||i.kind===`map`&&i.of!==void 0)||e.exists(o))continue;let s=i.of??`object`;throw new U(`not-found`,n,`\`${r}.${t}\` holds the id of a \`${s}\`, and no object has the id \`${o}\`: give the id of an existing \`${s}\` (a load or query answers them), or create one earlier in the batch and use its \`$name\``)}}function Si(e,t,n,r,i,a,o){let s=pi(r,i),c=t=>e.exists(t)||s===`delete`&&e.wasDeleted(t),l=e=>ui(i[e],a,n,c,o,r,e),u=(i,a)=>{let[o,s,c]=vi(e,t,n,r,i,a);if(c.length)throw new U(`invalid-op`,r,`a container operation requires a container field, not a JSON member`);return[o,s]},d=(i,a)=>{if(!a.includes(`.`)&&!a.includes(`/`))return[i,a];let[o,c,l]=vi(e,t,n,r,i,a);if(l.length)throw new U(`invalid-op`,r,`\`field\` \`${a}\`: \`${s}\` ${s===`text.set`?`replaces`:`edits`} a text field, not a path inside a value`);return[o,c]};try{switch(s){case`create`:{let s=W(r,i,`type`);if(i.fields!==void 0&&i.fields!==null&&!ii(i.fields))throw new U(`invalid-op`,r,"`fields` is an object");let l=ai(i.fields??{},a)??{},d={};for(let i of R(l))d[i]=di(e,t,n,r,s,i,l[i]);let f=i.ref;if(f!=null&&!(typeof f==`string`&&f.length>1&&f.startsWith(`$`)))throw new U(`invalid-op`,r,"`ref` is a name starting with `$`");let p=null;if(i.in!==void 0&&i.in!==null){let e=i.in,t=typeof e==`string`?e.indexOf(`.`):-1;if(typeof e!=`string`||t<0)throw new U(`invalid-op`,r,'`in` is "<target>.<field>"');p=u(ui(e.slice(0,t),a,n,c,o,r),e.slice(t+1))}if(i.id!==void 0&&i.id!==null&&typeof i.id!=`string`)throw new U(`invalid-op`,r,li(`id`,i.id));bi(t,r,s,d),xi(e,t,r,s,R(d).map(e=>[e,d[e]]));let m=e.create(s,d,typeof i.id==`string`?{id:i.id}:void 0);if(p){let[s,c]=p,l=e.objectType(s),u=Mr(t,l,c).kind;if(G(e,t,n,r,s,c),u===`list`)e.listInsert(s,c,hi(r,i.at,e.listItems(s,c).length),m);else if(u===`tree`){if(i.at!==void 0&&i.at!==null&&i.at!==`end`)throw new U(`invalid-op`,r,"`at` places items in lists; a tree adds a node last among its siblings");e.treeCreate(s,c,wi(e,a,n,o,r,i,s,c),m)}else throw new U(`invalid-op`,r,`\`${l}.${c}\` is not a list or a tree; create objects in lists and trees`)}else if(i.parent!==void 0&&i.parent!==null)throw new U(`invalid-op`,r,"`parent` places an object in a tree: give `in` too");return typeof f==`string`&&a.set(f,m),m}case`set`:{let o=l(`target`),s=W(r,i,`field`),c=i.value===void 0?null:ai(i.value,a);if(s.includes(`.`)||s.includes(`/`)){let i=s,[a,l,u]=vi(e,t,n,r,o,i);if(u.length>0){let t=null;try{t=e.get(a,l)}catch{t=null}try{c=yi(t,u,c)}catch(e){throw new U(`invalid-op`,r,`\`field\` \`${i}\`: ${e.message}`)}}o=a,s=l}let u=c;try{u=di(e,t,n,r,e.objectType(o),s,c)}catch(e){if(e instanceof U)throw e}G(e,t,n,r,o,s);let d=null;try{d=e.objectType(o)}catch{d=null}if(d!==null&&Object.hasOwn(t.types,d)){let i=t.types[d];if(i.readOnlyWhen?.field===s&&gr(i,t=>{if(t===s)return u;try{return e.get(o,t)}catch{return}}))throw new U(`read-only`,r,`\`${d}.${s}\` would make \`${n.show(o)}\` read-only: only the kit does`)}return d!==null&&xi(e,t,r,d,[[s,u]]),e.set(o,s,u),null}case`delete`:{let i=l(`target`);if(!e.exists(i)&&!e.wasDeleted(i))throw new U(`not-found`,r,`object \`${i}\` not found`);let a=[[i,!1]],o=/* @__PURE__ */ new Set,s=[];for(;a.length;){let[n,r]=a.pop();if(r){s.push(n);continue}if(!e.exists(n)||o.has(n))continue;o.add(n),a.push([n,!0]);for(let r of R(t.types))for(let[i,o]of Object.entries(jr(t,r).fields))if(o.cascade)for(let t of e.objectsOfType(r)){let r=e.tree(t,i),o=r.filter(e=>e.object===n).map(e=>e.node);a.push(...r.filter(e=>e.parent!==null&&o.some(t=>t.equals(e.parent))).map(e=>[e.object,!1]))}let i=jr(t,e.objectType(n));for(let t of R(i.fields)){let r=i.fields[t];if(!fr(r))continue;let o=[];switch(r.kind){case`list`:o=e.listItems(n,t);break;case`tree`:o=e.tree(n,t).map(e=>e.object);break;case`ref`:{let r=e.get(n,t);typeof r==`string`&&(o=[r]);break}case`map`:o=pr(Object.fromEntries(e.mapEntries(n,t)),t=>e.exists(t)).map(([e])=>e)}a.push(...o.map(e=>[e,!1]))}}for(let i of s){let a=e.objectType(i);G(e,t,n,r,i,null);for(let o of R(t.types)){let s=jr(t,o);for(let c of R(s.fields)){let l=s.fields[c];if(l.kind===`list`&&l.of===a)for(let a of e.objectsOfType(o)){let o=e.listItems(a,c);o.includes(i)&&G(e,t,n,r,a,c);for(let t=o.length-1;t>=0;--t)o[t]===i&&e.listRemove(a,c,t)}}}for(let o of R(t.types)){let s=jr(t,o);for(let c of R(s.fields)){let l=s.fields[c];if(!(l.kind!==`tree`||l.of!==void 0&&l.of!==a))for(let a of e.objectsOfType(o)){let o=e.tree(a,c),s=o.filter(e=>e.object===i).map(e=>e.node);if(s.length&&G(e,t,n,r,a,c),o.some(t=>t.parent!==null&&s.some(e=>e.equals(t.parent))&&e.exists(t.object)))throw new U(`invalid-value`,r,`\`${n.show(i)}\` has children in \`${n.show(a)}.${c}\`: delete them child-first or move them, before deleting this node in the same batch`)}}}e.delete(i)}return null}case`tree.insert`:{let[s,c]=u(l(`target`),W(r,i,`field`));G(e,t,n,r,s,c);let d=l(`item`);if(Ci(e,s,c,d))throw new U(`invalid-value`,r,`\`${n.show(d)}\` is already in \`${n.show(s)}.${c}\`: move it with tree.move`);return e.treeCreate(s,c,wi(e,a,n,o,r,i,s,c),d),null}case`tree.move`:{let[s,c]=u(l(`target`),W(r,i,`field`));G(e,t,n,r,s,c);let d=l(`item`),f=Ci(e,s,c,d);if(!f)throw new U(`not-found`,r,`\`${n.show(d)}\` is not in \`${n.show(s)}.${c}\``);return e.treeMove(s,c,f,wi(e,a,n,o,r,i,s,c)),null}case`text.insert`:{let[a,o]=d(l(`target`),W(r,i,`field`));return G(e,t,n,r,a,o),e.textInsert(a,o,gi(e.text(a,o),mi(r,i,`index`)),W(r,i,`text`)),null}case`text.delete`:{let[a,o]=d(l(`target`),W(r,i,`field`));G(e,t,n,r,a,o);let s=e.text(a,o),c=mi(r,i,`index`),u=gi(s,c);return e.textDelete(a,o,u,gi(s,c+mi(r,i,`count`))-u),null}case`text.set`:{let[a,o]=d(l(`target`),W(r,i,`field`));G(e,t,n,r,a,o);let s=W(r,i,`text`),c=e.textLength(a,o);return c>0&&e.textDelete(a,o,0,c),s!==``&&e.textInsert(a,o,0,s),null}case`text.mark`:case`text.unmark`:{let a=l(`target`),o=W(r,i,`field`);G(e,t,n,r,a,o);let c=e.text(a,o),u=gi(c,mi(r,i,`start`)),d=gi(c,mi(r,i,`end`));return s===`text.mark`?e.textMark(a,o,u,d,W(r,i,`key`),i.value??null):e.textUnmark(a,o,u,d,W(r,i,`key`)),null}case`list.insert`:{let[a,o]=u(l(`target`),W(r,i,`field`));return G(e,t,n,r,a,o),e.listInsert(a,o,hi(r,i.index,e.listItems(a,o).length),l(`item`)),null}case`list.move`:case`list.remove`:case`map.set`:case`map.delete`:{let[o,c]=s.startsWith(`list.`)?u(l(`target`),W(r,i,`field`)):[l(`target`),W(r,i,`field`)];if(G(e,t,n,r,o,c),s===`list.move`){let t=mi(r,i,`from`),n=mi(r,i,`to`);e.listMove(o,c,t,n>t?n-1:n)}else if(s===`list.remove`)e.listRemove(o,c,mi(r,i,`index`));else if(s===`map.set`){let s=e.objectType(o),l=di(e,t,n,r,s,c,i.value===void 0?null:ai(i.value,a));xi(e,t,r,s,[[c,l]]),e.mapSet(o,c,W(r,i,`key`),l)}else e.mapDelete(o,c,W(r,i,`key`));return null}default:throw new U(`invalid-op`,r,`\`${s}\` is not a model operation`)}}catch(e){throw si(r,e)}}function Ci(e,t,n,r){return e.exists(r)?e.tree(t,n).find(e=>e.object===r)?.node??null:null}function wi(e,t,n,r,i,a,o,s){if(a.parent===void 0||a.parent===null)return null;if(typeof a.parent!=`string`)throw new U(`invalid-op`,i,"`parent` is an object or null");let c=ui(a.parent,t,n,t=>e.exists(t),r,i,`parent`),l=Ci(e,o,s,c);if(!l)throw new U(`not-found`,i,`the parent \`${n.show(c)}\` is not in \`${n.show(o)}.${s}\``);return l}function Ti(e,t,n,r){if(!ii(n)||typeof n.target!=`string`)throw new U(`invalid-op`,null,li(`target`,ii(n)?n.target:void 0));let i=ui(n.target,r,t,t=>e.exists(t),()=>oi(e),null),a=e.objectType(i),o=jr(e.schema,a),s;if(n.fields===void 0||n.fields===null)s=R(o.fields);else if(Array.isArray(n.fields)&&n.fields.every(e=>typeof e==`string`))s=n.fields;else throw new U(`invalid-op`,null,"`fields` lists names");let c={};for(let n of s)c[n]=fi(t,Object.hasOwn(o.fields,n)?o.fields[n].kind:`json`,Ei(e,i,a,n));return{id:t.show(i),type:a,fields:c}}function Ei(e,t,n,r){let i=Mr(e.schema,n,r);switch(i.kind){case`text`:return Tr(i)?e.textDelta(t,r):e.text(t,r);case`list`:return e.list(t,r);case`map`:return Object.fromEntries(e.mapEntries(t,r));case`tree`:{let n=[];for(let i of e.tree(t,r))(i.parent===null||n.some(e=>e.node.equals(i.parent)))&&e.exists(i.object)&&n.push(i);return n.map(e=>({object:e.object,parent:n.find(t=>e.parent!==null&&t.node.equals(e.parent))?.object??null}))}default:return e.get(t,r)}}function Di(e){let t=e.schema,n={};for(let e of R(t.types)){let r={};for(let[n,i]of Object.entries(t.types[e].fields)){let e={kind:i.kind};i.label!==void 0&&(e.label=i.label);for(let t of[`minimum`,`maximum`,`multipleOf`])i[t]!==void 0&&(e[t]=i[t]);i.members!==void 0&&Object.keys(i.members).length>0&&(e.members=i.members),i.of!==void 0&&(e.of=i.of),i.values?.length&&(e.values=i.values),i.optional&&(e.optional=!0),i.marks&&Object.keys(i.marks).length>0&&(e.marks=R(i.marks)),i.readOnly&&(e.readOnly=!0),i.owns&&(e.owns=!0),i.cascade&&(e.cascade=!0),i.position&&(e.position=!0),i.display!==void 0&&(e.display={...i.display}),i.sample!==void 0&&(e.sample=i.sample),r[n]=e}let i=t.types[e],a={fields:r};i.projected&&(a.projected=!0),i.projectedOps&&Object.keys(i.projectedOps).length&&(a.projectedOps=structuredClone(i.projectedOps)),i.readOnly&&(a.readOnly=!0),i.readOnlyWhen&&(a.readOnlyWhen={field:i.readOnlyWhen.field,values:[...i.readOnlyWhen.values]}),i.toolId&&(a.toolId={field:i.toolId.field,prefix:i.toolId.prefix,...i.toolId.parent===void 0?{}:{parent:i.toolId.parent}}),i.pathName!==void 0&&(a.pathName=i.pathName),i.label!==void 0&&(a.label=i.label),i.derived&&(a.derived=Object.fromEntries(R(i.derived).map(e=>[e,{...i.derived[e]}]))),i.text&&(a.text=i.text),i.batchOps&&Object.assign(a,{batchOps:structuredClone(i.batchOps)}),i.creation&&Object.assign(a,{creation:structuredClone(i.creation)}),i.batchIndependent&&(a.batchIndependent=!0),i.moveConsequences?.length&&(a.moveConsequences=[...i.moveConsequences]),i.structuralConsequences?.length&&(a.structuralConsequences=i.structuralConsequences.map(e=>({container:e.container,on:[...e.on],fields:[...e.fields]}))),n[e]=a}let r=oi(e),i=r!==null&&hr(t)?V.ofReplica(e).show(r):r;return{capability:ei,kit:t.kit,schemaVersion:t.schemaVersion,root:t.root,rootId:i,types:n}}function Oi(e){let t=e;for(let e of[`kit`,`schemaVersion`,`root`,`types`])if(t[e]===void 0)throw N.schema(`the kit's description has no \`${e}\``);let n={};for(let[e,r]of Object.entries(t.types)){let t={};for(let[e,n]of Object.entries(r.fields??{}))t[e]=Array.isArray(n.marks)?{...n,marks:Object.fromEntries(n.marks.map(e=>[e,{expand:`after`}]))}:n;n[e]={...r,fields:t}}return Ar({kdm:1,kit:t.kit,schemaVersion:t.schemaVersion,root:t.root,types:n})}let ki=`CommentThread`,Ai=`Comment`,ji={fields:{anchor:{kind:`json`},resolved:{kind:`boolean`,optional:!0},comments:{kind:`list`,of:Ai}},batchOps:{"*":{direct:!0}}},Mi={fields:{author:{kind:`string`},name:{kind:`string`,optional:!0},body:{kind:`text`},created:{kind:`string`}},batchOps:{"*":{direct:!0}}};function Ni(e){return e===ki||e===Ai}function Pi(e,t){let n={author:t.author,created:t.created};t.name!==void 0&&(n.name=t.name);let r=e.create(Ai,n);return t.body&&e.textInsert(r,`body`,0,t.body),r}function Fi(e,t){if(e.objectType(t)!==ki)throw new M(`invalid-value`,`${t} is not a ${ki}`)}function Ii(e,t,n){let r=e.create(ki,{anchor:t,resolved:!1}),i=Pi(e,n);return e.listInsert(r,`comments`,0,i),{thread:r,comment:i}}function Li(e,t,n){Fi(e,t);let r=Pi(e,n);return e.listInsert(t,`comments`,e.listItems(t,`comments`).length,r),r}function Ri(e,t,n){Fi(e,t),e.set(t,`resolved`,n)}function zi(e,t,n){Fi(e,t);let r=e.listItems(t,`comments`),i=r.indexOf(n);if(i<0)throw new M(`not-found`,n);e.listRemove(t,`comments`,i),e.delete(n),r.length===1&&e.delete(t)}function Bi(e,t){Fi(e,t);for(let n of e.listItems(t,`comments`))e.exists(n)&&e.delete(n);e.delete(t)}function Vi(e){return e&&typeof e==`object`&&!Array.isArray(e)&&typeof e.object==`string`?e:null}let Hi=e=>typeof e==`string`?e:void 0;function Ui(e){let t=e.schema.types;if(!Object.hasOwn(t,ki)||!Object.hasOwn(t,Ai))return[];let n=[];for(let t of e.objectsOfType(ki)){let r=[];for(let n of e.list(t,`comments`)){if(!e.exists(n)||e.objectType(n)!==Ai)continue;let t=Hi(e.get(n,`name`));r.push({id:n,author:Hi(e.get(n,`author`))??``,...t===void 0?{}:{name:t},body:e.text(n,`body`),created:Hi(e.get(n,`created`))??``})}n.push({id:t,anchor:Vi(e.get(t,`anchor`)),resolved:e.get(t,`resolved`)===!0,comments:r})}let r=e=>e.comments[0]?.created??``;return n.sort((e,t)=>r(e)<r(t)?-1:r(e)>r(t)?1:e.id<t.id?-1:+(e.id>t.id))}function Wi(e,t){let n=e.range;if(!n)return null;let r=e.quote;if(!r)return n.end<=t.length?n:null;if(t.slice(n.start,n.end)===r)return n;let i=null;for(let e=t.indexOf(r);e>=0;e=t.indexOf(r,e+1))(i===null||Math.abs(e-n.start)<Math.abs(i-n.start))&&(i=e);return i===null?null:{start:i,end:i+r.length}}var K=class e{counters;constructor(e){this.counters=e.sort((e,t)=>I(e[0],t[0]))}static empty(){return new e([])}static decode(t){let n;try{n=We.decode(t)}catch(e){throw N.encoding(String(e))}return e.fromEngine(n)}static fromEngine(t){return new e([...t.toJSON().entries()])}toEngine(){return We.parseJSON(new Map(this.counters))}encode(){return this.toEngine().encode()}includes(t){if(!(t instanceof e))throw new M(`invalid-value`,"a version must be a `Version`");let n=new Map(this.counters);return t.counters.every(([e,t])=>t<=(n.get(e)??0))}equals(e){return this.counters.length===e.counters.length&&this.counters.every(([t,n],r)=>t===e.counters[r][0]&&n===e.counters[r][1])}};let Gi=[75,68,77,1];function Ki(e,t){let n=new Uint8Array(Gi.length+e.length+t.length);return n.set(Gi),n.set(e,Gi.length),n.set(t,Gi.length+e.length),n}function qi(e){if(!(e instanceof Uint8Array))throw N.encoding(`the bytes are not a Uint8Array`);if(e.length<20||Gi.some((t,n)=>e[n]!==t))throw N.encoding(`the bytes are not a KDM export`);return{docId:e.subarray(4,20),engine:e.subarray(20)}}let Ji=(e,t)=>e.length===t.length&&e.every((e,n)=>e===t[n]);function Yi(e,t){let n={};for(let[e,r]of Er(t))P(n,e,{expand:r});e.configTextStyle(n)}let Xi=(e,t)=>{let n=e?Object.keys(e).sort(I):[],r=t?Object.keys(t).sort(I):[];return n.length===r.length&&n.every((n,i)=>n===r[i]&&JSON.stringify(F(e[n]))===JSON.stringify(F(t[n])))};function Zi(e,t,n){if(t===``)return;let r=e[e.length-1];r&&Xi(r.attributes,n)?r.insert+=t:e.push(n&&Object.keys(n).length>0?{insert:t,attributes:n}:{insert:t})}function Qi(e,t){let n=t.marks??{},r=[];for(let t of e.toDelta()){let e=t.insert;if(typeof e!=`string`||e===``)continue;let i=t.attributes??{},a;for(let e of Object.keys(i).sort(I)){if(!Object.hasOwn(n,e))continue;let t=i[e];t!=null&&(a??={},P(a,e,F(t)))}Zi(r,e,a)}return r}let $i=`meta`,q=`objects`,ea=`$type`,ta=`object`,na=`schemaVersion`,ra=`schemaRaises`,ia=`birth`,aa=`root`,oa=e=>typeof e==`number`&&Number.isSafeInteger(e)&&e>=0;function sa(e){let t=e.getMap($i).get(na);if(!oa(t))return null;let n=t;for(let t of e.getMap(ra).keys()){let e=Number(t);oa(e)&&String(e)===t&&e>n&&(n=e)}return n}let ca={text:Ie,list:Pe,tree:Re,map:Me};function J(e,t){let n=e.getMap(q).get(t);if(!(n instanceof Me))throw N.notFound(t);return n}function la(e,t){let n=e.get(ea);if(typeof n!=`string`)throw N.notFound(t);return n}let ua=(e,t)=>la(J(e,t),t),da=(e,t)=>N.engine(`${e}.${t} holds the wrong container`),fa=(e,t,n)=>N.invalidValue(e,t,`is not a ${n} field`);function pa(e,t,n,r){if(e===void 0)return null;if(e instanceof ca[t])return e;throw da(n,r)}function Y(e,t,n,r,i){let a=J(e,n),o=la(a,n);if(Mr(t,o,r).kind!==i)throw fa(o,r,i);return pa(a.get(r),i,n,r)}function ma(e,t,n,r){let i=Y(e,t,n,r,`text`);if(!i)return[];let a=Mr(t,ua(e,n),r);if(Tr(a))return Qi(i,a);let o=[];return Zi(o,i.toString(),void 0),o}function ha(e,t,n,r){let i=J(e,n),a=la(i,n);if(xr(Mr(t,a,r).kind))throw N.invalidValue(a,r,`is a container; read it with its accessor`);return ga(i.get(r))}let ga=e=>e===void 0||et(e)?null:F(e);function _a(e){return e.keys().sort(I).map(t=>[t,ga(e.get(t))])}function va(e,t){if(Object.values(t.types).some(e=>e.projected))for(let n of Ca(e)){let r=la(J(e,n),n),i=t.types[r];if(i){if(i.projected)throw N.invalidValue(r,`$type`,`projected objects are computed by the kit, never stored`);for(let[a,o]of Object.entries(i.fields)){if(!o.of||!t.types[o.of]?.projected)continue;let i=J(e,n).get(a),s=et(i)?i.toJSON():i;if(s!=null&&(!Array.isArray(s)||s.length)&&(typeof s!=`object`||Object.keys(s).length))throw N.invalidValue(r,a,`projected membership is computed by the kit, never stored`)}}}}function ya(e,t){let n=[];for(let[r,i]of e.getMap(q).entries())i instanceof Me&&i.get(ea)===t&&n.push(r);return n.sort(I)}function ba(e){return e.forEach((e,t)=>{if(typeof e!=`string`)throw N.encoding(`list item ${t} is ${xa(e)}, not an object id`)}),e}let xa=e=>et(e)?`a container`:JSON.stringify(e)??String(e);function Sa(e){let t=e.data.get(ta);return typeof t==`string`?t:null}function Ca(e){return e.getMap(q).keys().sort(I)}let wa=e=>typeof e==`object`&&!!e&&!Array.isArray(e),Ta=`__proto__`,Ea=e=>et(e)?e.toJSON():e;function Da(e){let t=[];for(let n of e??[]){let e=n.meta?.[ta];typeof e==`string`&&t.push({children:Da(n.children),object:e})}return t}function Oa(e){let t=[];for(let n of e??[]){let e=Sa(n);e!==null&&t.push({children:Oa(n.children()),object:e})}return t}function ka(e){let t={};for(let n of e.keys())P(t,n,Ea(e.get(n)));return t}function Aa(e,t,n){let r=la(J(e,n),n),i={[ea]:r};if(!Object.hasOwn(t.types,r))return i;let a=t.types[r].fields;for(let r of R(a)){let o=a[r].kind,s;if(o===`text`&&Tr(a[r])){let i=Y(e,t,n,r,`text`);s=i?Qi(i,a[r]):[]}else if(o===`text`)s=Y(e,t,n,r,`text`)?.toString()??``;else if(o===`list`)s=ba(Y(e,t,n,r,`list`)?.toArray()??[]);else if(o===`tree`)s=Oa(Y(e,t,n,r,`tree`)?.roots());else if(o===`map`){let i=Y(e,t,n,r,`map`);s=i?ka(i):{}}else s=ha(e,t,n,r);P(i,r,s)}return i}let ja=/* @__PURE__ */ new WeakMap;function Ma(e,t){let n=ja.get(e);n||ja.set(e,n=/* @__PURE__ */ new Map);let r=n.get(t);return r||n.set(t,r=[ea,...Object.keys(e.types[t].fields)].sort(I)),r}let Na=e=>typeof e==`object`&&e?F(e):e;function Pa(e,t){let n=e.toJSON(),r=wa(n[q])?n[q]:{},i=e.getMap(q),a=Object.keys(r);i.get(Ta)!==void 0&&a.push(Ta),a.sort(I);let o={};for(let n of a){let i=r[n],a=wa(i)?i[ea]:void 0,s=typeof a==`string`&&Object.hasOwn(t.types,a);if(n===Ta||s&&Object.hasOwn(t.types[a].fields,Ta)){P(o,n,F(Aa(e,t,n)));continue}if(!wa(i)||typeof a!=`string`)throw N.notFound(n);if(!s){o[n]={[ea]:a};continue}let c={};o[n]=c;let l=t.types[a].fields;for(let r of Ma(t,a)){if(r===ea){c[r]=a;continue}let t=i[r],o=()=>da(n,r);switch(l[r].kind){case`text`:if(t!==void 0&&typeof t!=`string`)throw o();if(Tr(l[r])){let i=t===void 0?null:pa(J(e,n).get(r),`text`,n,r);c[r]=i?Qi(i,l[r]):[]}else c[r]=t??``;break;case`list`:if(t!==void 0&&!Array.isArray(t))throw o();c[r]=Array.isArray(t)?ba(t):[];break;case`tree`:if(t!==void 0&&!Array.isArray(t))throw o();c[r]=Da(t);break;case`map`:{if(t!==void 0&&!wa(t))throw o();let i=wa(t)?{...t}:{},a=J(e,n).get(r),s=a instanceof Me?a.get(Ta):void 0;s!==void 0&&P(i,Ta,Ea(s)),c[r]=F(i);break}default:c[r]=t===void 0?null:Na(t)}}}let s=wa(n[$i])?{...n[$i]}:{},c=sa(e);return c!==null&&(s[na]=c),{meta:F(s),objects:o}}function Fa(e){if(e.outside)return!1;for(let t of e.objects.values())if(t===null||!Ni(t))return!1;return!0}let Ia=e=>e instanceof Error?e.message:String(e);function La(e,t){let n=e.getMap(q).get(t);if(!(n instanceof Me))return null;let r=n.get(ea);return typeof r==`string`?r:null}var Ra=class e{constructor(e){this.doc=e}static load(t){let n=new Oe;try{n.import(qi(t).engine)}catch(e){throw N.encoding(Ia(e))}return new e(n)}version(){return K.fromEngine(this.doc.oplogVersion())}follow(e){try{this.doc.import(qi(e).engine)}catch(e){throw N.encoding(Ia(e))}}touched(e){let t=qi(e).engine,n=this.doc.oplogFrontiers(),r=/* @__PURE__ */ new Set,i=!1,a=this.doc.subscribe(e=>{for(let t of e.events)if(t.path[0]!==q)i=!0;else if(t.path.length>=2)r.add(String(t.path[1]));else if(t.diff.type===`map`)for(let e of Object.keys(t.diff.updated))r.add(e);else i=!0});try{this.doc.import(t)}catch(e){throw N.encoding(Ia(e))}finally{a()}let o=/* @__PURE__ */ new Map;if(!r.size)return{objects:o,outside:i};let s=new Map([...r].map(e=>[e,La(this.doc,e)]));this.doc.checkout(n);try{for(let e of r)o.set(e,La(this.doc,e)??s.get(e)??null)}finally{this.doc.checkoutToLatest()}for(let e of r){let t=this.doc.getMap(q).get(e);t!==void 0&&!et(t)&&o.set(e,null)}return{objects:o,outside:i}}};function za(e,t){let n=Array.from(e),r=Array.from(t);if(e===t||n.length<=80)return;let i=0,a=0;for(;i<Math.min(n.length,r.length)&&n[i]===r[i];)i++;for(;a<Math.min(n.length,r.length)-i&&n[n.length-1-a]===r[r.length-1-a];)a++;let o=n.length-i-a,s=r.length-i-a;return{offset:i,removed:n.slice(i,i+Math.min(o,80)).join(``),inserted:r.slice(i,i+Math.min(s,80)).join(``),removedLength:o,insertedLength:s,before:n.slice(Math.max(0,i-20),i).join(``),after:n.slice(n.length-a,n.length-a+20).join(``),complete:o<=80&&s<=80}}function Ba(e,t,n){let r=0,i=(e,t,n)=>n.split(`.`).reduce((e,t)=>X(e)?e[t]:Array.isArray(e)?e[Number(t)]:void 0,Ha(e)[t]),a={...e,changed:e.changed.map(e=>({...e,edits:e.edits?.map(a=>{let o=i(t,e.id,a.field),s=i(n,e.id,a.field),c=o==null?``:Ka(o),l=s==null?``:Ka(s),u=c===null||l===null?void 0:za(c,l);if(!u)return a;let d=new TextEncoder().encode(JSON.stringify(u)).length;return r+d>65536?{...a,spansTruncated:!0}:(r+=d,{...a,span:u})})}))},o=so.get(e);return o&&so.set(a,o),a}let Va=`$type`,X=e=>typeof e==`object`&&!!e&&!Array.isArray(e);function Ha(e){let t=X(e)?e.objects:void 0;return X(t)?t:{}}let Ua=e=>X(e)&&typeof e[Va]==`string`?e[Va]:``;function Wa(e){let t=X(e)?{...e}:{};return delete t[Va],t}function Ga(e){return e==null?!1:typeof e==`string`||Array.isArray(e)?e.length>0:!X(e)||Object.keys(e).length>0}function Ka(e){return typeof e==`string`?e:Array.isArray(e)&&e.length>0&&e.every(e=>X(e)&&typeof e.insert==`string`)?e.map(e=>e.insert).join(``):null}let qa=(e,t)=>JSON.stringify(e)===JSON.stringify(t),Ja=(e,t)=>L(e)===L(t),Ya=(...e)=>[...new Set(e.flatMap(e=>Object.keys(e)))].sort(I),Xa=e=>Array.isArray(e)&&e.length>0&&e.every(e=>X(e)&&typeof e.object==`string`&&Array.isArray(e.children)),Za=(e,t)=>Array.isArray(e)&&e.length>0&&e.every(e=>typeof e==`string`&&t(e)),Qa=e=>e==null||Array.isArray(e)&&e.length===0;function $a(e,t,n){let r=n=>(n(e)||n(t))&&(n(e)||Qa(e))&&(n(t)||Qa(t));return r(Xa)?`tree`:r(e=>Za(e,n))?`list`:`value`}function eo(e){return!X(e)||typeof e.hash!=`string`||typeof e.mime!=`string`?null:typeof e.size==`number`&&Number.isInteger(e.size)&&e.size>=0?e.size:null}function to(e,t){let n=Array.from(e);if(n.length<=80)return e;let r=Math.min(Math.max(t-20,0),n.length),i=80,a=``;r>0&&(a+=`…`,--i);let o=n.slice(r);return a+=o.length<=i?o.join(``):o.slice(0,i-1).join(``)+`…`,{excerpt:a,length:n.length}}function no(e,t){let n=eo(e);if(n!==null)return{size:n};let r=Ka(e);return r===null?e:to(r,t)}function ro(e,t){return eo(e)===null?no(e,t):e}function io(e,t){let[n,r]=[Array.from(e),Array.from(t)],i=0;for(;i<n.length&&i<r.length&&n[i]===r[i];)i+=1;return i}let ao=e=>JSON.stringify(e);var oo=class{at=/* @__PURE__ */ new Map;add(e,t,n,r){if(r===`list`){let r=[e,t,e];n.filter(e=>typeof e==`string`).forEach((e,t)=>{this.at.has(e)||this.at.set(e,{group:r,index:t})})}else r===`tree`&&this.addNodes(e,t,e,n)}addNodes(e,t,n,r){let i=[e,t,n];(Array.isArray(r)?r:[]).forEach((n,r)=>{!X(n)||typeof n.object!=`string`||(this.at.has(n.object)||this.at.set(n.object,{group:i,index:r}),this.addNodes(e,t,n.object,n.children))})}position(e){let t=this.at.get(e);return t?{parent:t.group[2],field:t.group[1],index:t.index}:null}};let so=/* @__PURE__ */ new WeakMap;function co(e,t){let n=so.get(e),r={...e,deleted:e.deleted.map(e=>{let r=n?.get(e.id),i=t.types[e.type]?.fields,a=Object.fromEntries(Object.entries(r??{}).filter(([e])=>e.startsWith(`~`)||i&&Object.hasOwn(i,e)));return Object.keys(a).length?{...e,$was:a}:{...e}})};return n&&so.set(r,n),r}function lo(e){let t=[],n=e.map(()=>null);e.forEach((r,i)=>{let a=0,o=t.length;for(;a<o;){let n=a+o>>1;e[t[n]]<r?a=n+1:o=n}a>0&&(n[i]=t[a-1]),a===t.length?t.push(i):t[a]=i});let r=/* @__PURE__ */ new Set,i=t.length>0?t[t.length-1]:null;for(;i!==null;)r.add(i),i=n[i];return r}function uo(e){if(eo(e)!==null)return!0;if(!X(e))return!1;let t=Object.keys(e).sort(I).join(`,`);return t===`excerpt,length`||t===`size`}let fo=(e,t)=>L(e)===L(t);function po(e,t,n,r){if(X(t)&&X(n)&&!uo(t)&&!uo(n)){let i=[.../* @__PURE__ */ new Set([...Object.keys(t),...Object.keys(n)])].sort(I);for(let a of i){let[i,o]=[t[a]??null,n[a]??null];fo(i,o)||po(`${e}.${a}`,i,o,r)}}else Array.isArray(t)&&Array.isArray(n)&&t.length===n.length?t.forEach((t,i)=>{fo(t,n[i])||po(`${e}.${i}`,t,n[i],r)}):r.push({field:e,before:ro(t,0),after:no(n,0)})}let mo=e=>e.reduce((e,t)=>e+t.field.length+L(t.before).length+L(t.after).length,0);function ho(e,t,n){let r=[{field:e,before:ro(t,0),after:no(n,0)}],i=[];return po(e,t,n,i),i.length<=16&&mo(i)<mo(r)?i:r}function go(e,t,n){let r=t===null?{}:t,i=n===null?{}:n;if(!X(r)||!X(i))return[{field:e,before:t,after:n}];let a=[];for(let t of[.../* @__PURE__ */ new Set([...Object.keys(r),...Object.keys(i)])].sort(I)){let[n,o]=[r[t]??null,i[t]??null];fo(n,o)||a.push(...ho(`${e}.${t}`,n,o))}return a}function _o(e,t){for(let n of e.changed){if(!n.edits)continue;let e=/* @__PURE__ */ new Map;for(let r of n.edits){let i;try{i=Mr(t,n.type,r.field)}catch{continue}let a=i.kind===`map`&&!i.owns?go(r.field,r.before,r.after):i.kind===`json`?ho(r.field,r.before,r.after):null;a&&(a.length!==1||a[0].field!==r.field)&&e.set(r.field,a)}if(e.size===0)continue;let r=[],i=[...n.fields];for(let t of n.edits){let n=e.get(t.field);n?(i=i.filter(e=>e!==t.field),i.push(...n.map(e=>e.field)),r.push(...n)):r.push(t)}n.edits=r.sort((e,t)=>I(e.field,t.field)),n.fields=[...new Set(i)].sort(I)}return e}function vo(e,t,n,r,i){let a=e=>r(e)!==null,o={created:[],changed:[],moved:[],deleted:[]},s=[],[c,l]=[new oo,new oo],u=/* @__PURE__ */ new Set,d=/* @__PURE__ */ new Set,f=/* @__PURE__ */ new Map;so.set(o,f);for(let r of e){let e=Object.hasOwn(t,r)?t[r]:void 0,p=Object.hasOwn(n,r)?n[r]:void 0;if(e===void 0&&p!==void 0){u.add(r);let e=Wa(p),t=Ya(e).filter(t=>Ga(e[t])),n={};for(let o of t){let t=o.startsWith(`~`)?`value`:$a(null,e[o],a);t===`value`?n[o]=no(e[o],0):l.add(r,o,e[o],t);let c=Ka(e[o]);i&&c&&s.push({id:r,field:o,before:``,after:c})}o.created.push({id:r,type:Ua(p),fields:t,values:n})}else if(e!==void 0&&p===void 0){d.add(r);let t=Wa(e),n={};for(let e of Ya(t)){let i=e.startsWith(`~`)?`value`:$a(t[e],null,a);i===`value`&&Ga(t[e])&&(n[e]=ro(t[e],0)),c.add(r,e,t[e],i)}f.set(r,n),o.deleted.push({id:r,type:Ua(e),fields:[]})}else if(e!==void 0&&p!==void 0&&!qa(e,p)){let[t,n]=[Wa(e),Wa(p)],u=[],d=[];Ua(e)!==Ua(p)&&(u.push(`$type`),d.push({field:`$type`,before:Ua(e),after:Ua(p)}));for(let e of Ya(t,n)){let o=t[e]??null,f=n[e]??null;if(qa(o,f))continue;let p=e.startsWith(`~`)?`value`:$a(o,f,a);if(p!==`tree`&&u.push(e),p!==`value`){c.add(r,e,o,p),l.add(r,e,f,p);continue}let m=e=>e===null?``:Ka(e),[h,g]=[m(o),m(f)],_=0;h!==null&&g!==null&&(_=io(h,g),i&&h!==g&&s.push({id:r,field:e,before:h,after:g})),d.push({field:e,before:ro(o,_),after:no(f,_)})}u.length&&o.changed.push({id:r,type:Ua(p)||Ua(e),fields:u,edits:d})}}for(let e of o.created){let t=l.position(e.id);t&&(e.at=t)}for(let e of o.deleted){let t=c.position(e.id);t&&(e.at=t)}let p=/* @__PURE__ */ new Map;for(let[e,{group:t,index:n}]of l.at){let r=c.at.get(e);if(r&&ao(r.group)===ao(t)){let i=ao(t);p.has(i)||p.set(i,[]),p.get(i).push({id:e,was:r.index,now:n})}}let m=/* @__PURE__ */ new Set;for(let e of p.values()){e.sort((e,t)=>e.now-t.now);for(let t of lo(e.map(e=>e.was)))m.add(e[t].id)}let h=[.../* @__PURE__ */ new Set([...c.at.keys(),...l.at.keys()])].sort(I);for(let e of h)u.has(e)||d.has(e)||m.has(e)||o.moved.push({id:e,type:r(e)??``,from:c.position(e),to:l.position(e)});return i&&(o.texts=s),o}function yo(e,t){let[n,r]=[Ha(e),Ha(t)];return vo(Ya(n,r),n,r,e=>Object.hasOwn(r,e)?Ua(r[e]):Object.hasOwn(n,e)?Ua(n[e]):null,!0)}function bo(e,t,n,r){e=Ba(e,n,r);let i=Ha(n),a=Ha(r),o=V.ofState(t,r),s=V.ofState(t,n),c={},l=0;for(let e of Ya(i,a)){if(!Object.hasOwn(i,e)||!Object.hasOwn(a,e))continue;let t=Wa(i[e]),n=Wa(a[e]);for(let r of Ya(t,n)){if(qa(t[r],n[r])||Ka(t[r])===null||Ka(n[r])===null)continue;let i=new TextEncoder().encode(JSON.stringify(t[r])).length;if(i>1048576||l+i>4194304)continue;let a=o.attributed(e),u=a?.holder??o.show(e),d=a?`${a.path}.${r}`:r;(c[u]??={})[d]=s.showText(t[r]),l+=i}}if(!Object.keys(c).length)return e;let u={...e,$text:c},d=so.get(e);return d&&so.set(u,d),u}let xo=e=>!e.created.length&&!e.changed.length&&!e.moved.length&&!e.deleted.length;function So(e,t,n,r){if(!n.isActive()&&!r.isActive())return e;let i=so.get(e)??/* @__PURE__ */ new Map,a=(e,t)=>{let n=e.toolId(t)===null?e.attributed(t):null;return n===null?e.show(t):`${n.holder}.${n.path}`},o=(e,t,n)=>typeof t!=`string`||t===``?t:e.toolId(t)===null&&e.attributed(t)===null?{unnamed:n??`object`}:a(e,t),s=(e,n)=>Object.hasOwn(t.types,e)&&Object.hasOwn(t.types[e].fields,n)?t.types[e].fields[n].of??void 0:void 0,c=(e,t)=>{let n=e.toolId(t.parent)===null?e.attributed(t.parent):null;return n===null?{...t,parent:e.show(t.parent)}:{parent:n.holder,field:`${n.path}.${t.field}`,index:t.index}},l=new Map([...e.created,...e.changed].map(e=>[e.id,e.type])),u=(e,n)=>e!==void 0&&Object.hasOwn(t.types,e)&&Object.hasOwn(t.types[e].fields,n)&&t.types[e].fields[n].kind===`ref`,d=(e,t)=>u(l.get(e),t),f=(e,n)=>[`json`,`map`].includes(t.types[e]?.fields[n.split(`.`)[0]]?.kind??``),p=e=>e.sort((e,t)=>I(e.id,t.id)),m=e.created.map(e=>{let t={...e};return e.values&&(t.values=Object.fromEntries(Object.entries(e.values).map(([t,n])=>[t,u(e.type,t)?o(r,n,s(e.type,t)):f(e.type,t)?r.showJson(n):n]))),e.at&&(t.at=c(r,e.at)),t}),h=e.changed.map(e=>{let t={...e};if(e.edits){t.edits=e.edits.map(t=>u(e.type,t.field)?{...t,before:o(n,t.before,s(e.type,t.field)),after:o(r,t.after,s(e.type,t.field))}:f(e.type,t.field)?{...t,before:n.showJson(t.before),after:r.showJson(t.after)}:t);let i=new Set(t.edits.filter(t=>u(e.type,t.field)&&typeof t.before==`string`&&Ja(t.before,t.after)).map(e=>e.field));i.size>0&&(t.edits=t.edits.filter(e=>!i.has(e.field)),t.fields=t.fields.filter(e=>!i.has(e)))}return t}).filter(e=>e.fields.length>0),g=/* @__PURE__ */ new Map,_=(e,t)=>{let n=g.get(e)??{type:``,fields:[],edits:[],values:{}};return n.type=t.typeOf(e)??``,g.set(e,n),n},ee=new Set(e.created.map(e=>r.show(e.id))),te=new Set(e.deleted.map(e=>n.show(e.id))),v=[];for(let e of h){let t=r.attributed(e.id);if(t===null){v.push(e);continue}let{holder:n,path:i}=t,a=_(n,r);if(a.fields.push(...e.fields.map(e=>`${i}.${e}`)),ee.has(n)){for(let t of e.edits??[])a.values[`${i}.${t.field}`]=t.after;continue}let o=r.namedPath(e.id);a.edits.push(...(e.edits??[]).map(e=>({...e,field:`${i}.${e.field}`,...o===null?{}:{named:`${o}.${e.field}`}})))}let ne=[];for(let e of m){let t=r.attributed(e.id);if(t===null){ne.push(e);continue}let{holder:n,path:i}=t,a=_(n,r),o=e.values??{};if(ee.has(n)){a.fields.push(...e.fields.map(e=>`${i}.${e}`));for(let[e,t]of Object.entries(o))a.values[`${i}.${e}`]=t;continue}a.fields.push(i);let s=r.namedPath(e.id);a.edits.push({field:i,before:null,after:o,...s===null?{}:{named:s}})}let y=[];for(let t of e.deleted){let e=n.attributed(t.id);if(e===null){y.push({...t});continue}if(te.has(e.holder))continue;let r=_(e.holder,n);r.fields.push(e.path);let a=n.namedPath(t.id);r.edits.push({field:e.path,before:i.get(t.id)??{},after:null,...a===null?{}:{named:a}})}let re=e=>{let t=e.lastIndexOf(`.`);return t>=0&&/^\d+$/.test(e.slice(t+1))?e.slice(0,t):e},b=[];for(let t of e.moved){let[e,i]=[n.attributed(t.id),r.attributed(t.id)];if(e===null&&i===null)b.push(t);else if(e!==null&&i!==null&&e.holder===i.holder){if(!ee.has(i.holder)){let t=_(i.holder,r);t.fields.push(re(i.path)),t.edits.push({field:re(i.path),before:e.path,after:i.path})}}else{if(e!==null&&!te.has(e.holder)){let t=_(e.holder,n);t.fields.push(re(e.path)),t.edits.push({field:re(e.path),before:e.path,after:null})}if(i!==null&&!ee.has(i.holder)){let e=_(i.holder,r);e.fields.push(re(i.path)),e.edits.push({field:re(i.path),before:null,after:i.path})}}}for(let e of g.values())Co(e);let x={created:ne.map(e=>({...e,id:r.show(e.id)})),changed:v.map(e=>({...e,id:r.show(e.id)})),moved:b.map(e=>({...e,id:r.show(e.id),from:e.from?c(n,e.from):null,to:e.to?c(r,e.to):null})),deleted:y.map(e=>{let t={...e,id:n.show(e.id)};return e.at&&(t.at=c(n,e.at)),t})},ie=e=>[...new Set(e)].sort(I);for(let e of[...g.keys()].sort(I)){let t=g.get(e),n=x.created.find(t=>t.id===e);if(n!==void 0){n.fields=ie([...n.fields,...t.fields]),n.values={...n.values??{},...t.values};continue}if(t.fields.length===0&&t.edits.length===0)continue;let r=x.changed.find(t=>t.id===e);r===void 0&&(r={id:e,type:t.type,fields:[],edits:[]},x.changed.push(r)),r.fields=ie([...r.fields,...t.fields]),r.edits=[...r.edits??[],...t.edits].sort((e,t)=>I(e.field,t.field))}return p(x.created),p(x.changed),p(x.deleted),p(x.moved),e.texts&&(x.texts=e.texts.map(e=>{let t=d(e.id,e.field),i=(e,n)=>t&&n!==``?a(e,n):n,o=r.attributed(e.id),[s,c]=o===null?[a(r,e.id),e.field]:[o.holder,`${o.path}.${e.field}`];return{...e,id:s,field:c,before:i(n,e.before),after:i(r,e.after)}}).sort((e,t)=>I(e.id,t.id)||I(e.field,t.field))),e.footprint!==void 0&&(x.footprint=e.footprint),so.set(x,new Map([...i].map(([e,t])=>{let r=n.typeOf(e)??``;return[n.show(e),Object.fromEntries(Object.entries(t).map(([e,t])=>[e,u(r,e)?o(n,t,s(r,e)):f(r,e)?n.showJson(t):t]))]}))),x}function Co(e){let t=/* @__PURE__ */ new Map,n=/* @__PURE__ */ new Map;for(let r of e.edits)r.before===null&&X(r.after)&&t.set(r.field,r.after),r.after===null&&X(r.before)&&n.set(r.field,r.before);let r=new Set([...t.keys()].filter(e=>n.has(e)));if(r.size===0)return;let i=new Map(e.edits.filter(e=>r.has(e.field)&&e.named!==void 0).map(e=>[e.field,e.named]));e.edits=e.edits.filter(e=>!r.has(e.field)),e.fields=e.fields.filter(e=>!r.has(e));for(let a of[...r].sort(I)){let r=n.get(a),o=t.get(a);for(let t of[.../* @__PURE__ */ new Set([...Object.keys(r),...Object.keys(o)])].sort(I)){let n=Object.hasOwn(r,t)?r[t]:null,s=Object.hasOwn(o,t)?o[t]:null;if(!Ja(n,s)){e.fields.push(`${a}.${t}`);let r=i.get(a);e.edits.push({field:`${a}.${t}`,before:n,after:s,...r===void 0?{}:{named:`${r}.${t}`}})}}}}let wo=e=>typeof e==`object`&&!!e&&!Array.isArray(e),To=(e,t)=>Array.isArray(e[t])?e[t].filter(wo):[],Eo=e=>wo(e)&&typeof e.parent==`string`&&typeof e.field==`string`?[e.parent,e.field]:null,Do=(e,t)=>JSON.stringify([e,t]);function Oo(e){let t=/* @__PURE__ */ new Map,n=e=>{e&&t.set(Do(...e),e)};for(let t of[...To(e,`created`),...To(e,`deleted`)])n(Eo(t.at));for(let t of To(e,`moved`))n(Eo(t.from)),n(Eo(t.to));for(let t of To(e,`changed`))if(!(!Array.isArray(t.edits)||t.edits.length>0||typeof t.id!=`string`))for(let e of Array.isArray(t.fields)?t.fields:[])typeof e==`string`&&n([t.id,e]);return[...t.values()].sort((e,t)=>e[0]===t[0]?e[1]<t[1]?-1:+(e[1]>t[1]):e[0]<t[0]?-1:1)}function ko(e,t){if(typeof e==`number`&&typeof t==`number`){let n=t-e;return Number.isFinite(n)?n:null}let n=e=>typeof e==`string`&&/^-?\d+$/.test(e)?BigInt(e):null,[r,i]=[n(e),n(t)];return r===null||i===null?null:(i-r).toString()}function Ao(e,t){let n=JSON.parse(JSON.stringify(e)),r=new Set([`created`,`deleted`,`moved`].flatMap(t=>To(e,t)).map(e=>e.id)),i=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map,o=[];for(let t of To(e,`changed`)){let e=Array.isArray(t.edits)?t.edits:[];if(typeof t.id!=`string`||r.has(t.id)||e.length===0)continue;let n=[];for(let t of e){if(!wo(t)||typeof t.field!=`string`)break;let e=ko(t.before,t.after);if(e===null)break;n.push([t.field,e])}let s=Array.isArray(t.fields)?t.fields:[];n.length!==e.length||s.length!==n.length||!n.every(([e])=>s.includes(e))||(n.sort((e,t)=>e[0]<t[0]?-1:+(e[0]>t[0])),n.length===1&&(i.set(t.id,JSON.stringify(n[0])),a.set(t.id,n[0])),o.push([t.id,n]))}let s=[],c=/* @__PURE__ */ new Set,l=Oo(e);if(i.size>=4)for(let[e,n]of l){let r=t(e,n);if(!r)continue;let o=0;for(;o<r.length;){let t=i.get(r[o]);if(t===void 0){o+=1;continue}let l=o+1;for(;l<r.length&&i.get(r[l])===t;)l+=1;if(l-o>=4){let[t,i]=a.get(r[o]);s.push({parent:e,field:n,from:o,count:l-o,shift:{field:t,by:i}});for(let e of r.slice(o,l))c.add(e)}o=l}}let u=[];for(let[e,t]of o){if(c.has(e))continue;let n=JSON.stringify(t),r=u.find(([e])=>e===n);r?r[2].push(e):u.push([n,t,[e]])}for(let[,e,t]of u)if(!(t.length<4)){s.push({ids:t,edits:e.map(([e,t])=>({field:e,by:t}))});for(let e of t)c.add(e)}let d=/* @__PURE__ */ new Map;for(let t of To(e,`changed`)){if(typeof t.id!=`string`||c.has(t.id)||r.has(t.id)||!Array.isArray(t.edits)||t.edits.length===0||!Array.isArray(t.fields))continue;let e=[];for(let n of t.edits){if(!wo(n)||typeof n.field!=`string`||!Object.hasOwn(n,`after`))break;let t=n.after;if(wo(t)&&(`excerpt`in t||Object.keys(t).length===1&&`size`in t))break;e.push({field:n.field,after:t})}if(e.length!==t.edits.length||t.fields.length!==e.length||!e.every(e=>t.fields.includes(e.field)))continue;e.sort((e,t)=>I(e.field,t.field));let n=L(e),i=d.get(n);i?i.ids.push(t.id):d.set(n,{edits:e,ids:[t.id]})}for(let{edits:e,ids:t}of d.values())if(!(t.length<4)){s.push({ids:t,edits:e});for(let e of t)c.add(e)}let f=new Set(s.filter(e=>typeof e.parent==`string`).map(e=>Do(e.parent,e.field))),p=e=>{e&&f.add(Do(...e))};for(let t of[...To(e,`created`),...To(e,`deleted`)])p(Eo(t.at));for(let t of To(e,`moved`))p(Eo(t.from)),p(Eo(t.to));return Array.isArray(n.changed)&&(n.changed=n.changed.filter(e=>{if(!wo(e)||typeof e.id!=`string`)return!0;if(c.has(e.id))return!1;if(!Array.isArray(e.edits)||e.edits.length>0||!Array.isArray(e.fields))return!0;let t=e.id;return e.fields=e.fields.filter(e=>typeof e!=`string`||!f.has(Do(t,e))),e.fields.length>0})),s.length>0&&(n.footprint=s),Mo(n)}function jo(e,t,n,r,i){if(JSON.stringify(t)===JSON.stringify(n))return!0;if(wo(t)&&wo(n)){let a=Object.keys(t).sort(I);return a.some(e=>!e||e.includes(`.`)||e.includes(`*`))||JSON.stringify(a)!==JSON.stringify(Object.keys(n).sort(I))?!1:(i.add(e),a.every(a=>jo(`${e}.${a}`,t[a],n[a],r,i)))}return Array.isArray(t)&&Array.isArray(n)?t.length===n.length&&t.every((t,a)=>jo(`${e}.${a}`,t,n[a],r,i)):ko(t,n)!==null&&(r.push({field:e,before:t,after:n}),!0)}function Mo(e){for(let t of To(e,`changed`)){let e=Array.isArray(t.edits)?t.edits.filter(wo):[],n=[],r=/* @__PURE__ */ new Set;for(let t of e){let e=[];typeof t.field==`string`&&(wo(t.before)||Array.isArray(t.before))&&jo(t.field,t.before,t.after,e,r)?n.push(...e):n.push(t)}let i=/* @__PURE__ */ new Map;n.forEach((e,t)=>{let n=ko(e.before,e.after);if(n===null||typeof e.field!=`string`)return;let a=e.field.split(`.`);for(let e=1;e<a.length-1;e++){if(/^\d+$/.test(a[e])&&!r.has(a.slice(0,e).join(`.`)))continue;let o=a.map((t,n)=>n===e?`*`:t).join(`.`),s=JSON.stringify([o,n]),c=i.get(s)??{field:o,by:n,members:[]};c.members.push([t,a[e]]),i.set(s,c)}});let a=/* @__PURE__ */ new Set,o=[];for(let e of[...i.values()].sort((e,t)=>e.field<t.field?-1:e.field>t.field?1:JSON.stringify(e.by)<JSON.stringify(t.by)?-1:1)){let t=e.members.filter(([e])=>!a.has(e)),n=[...new Set(t.map(([,e])=>e))].sort(I);n.length<4||n.length!==t.length||(o.push({field:e.field,count:n.length,keys:n,shift:{by:e.by}}),t.forEach(([e])=>a.add(e)))}o.length&&(t.edits=[...n.filter((e,t)=>!a.has(t)),...o],t.fields=[...new Set(t.edits.map(e=>e.field))].sort())}return e}function No(e){return Array.from(e,e=>e.toString(16).padStart(2,`0`)).join(``)}function Po(e){let t=No(e);return`${t.slice(0,8)}-${t.slice(8,12)}-${t.slice(12,16)}-${t.slice(16,20)}-${t.slice(20)}`}let Fo=[/^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i,/^\{([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}$/i,/^urn:uuid:([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})$/i,/^([0-9a-f]{32})$/i];function Io(e){if(typeof e!=`string`)throw N.encoding(`a UUID is a string`);for(let t of Fo){let n=t.exec(e);if(!n)continue;let r=n.slice(1).join(``),i=/* @__PURE__ */ new Uint8Array(16);for(let e=0;e<16;e+=1)i[e]=parseInt(r.slice(e*2,e*2+2),16);return i}throw N.encoding(`\`${e}\` is not a UUID`)}function Lo(e=Date.now()){let t=/* @__PURE__ */ new Uint8Array(16);crypto.getRandomValues(t);let n=BigInt(e);for(let e=5;e>=0;--e)t[e]=Number(n&255n),n>>=8n;return t[6]=t[6]&15|112,t[8]=t[8]&63|128,Po(t)}let Ro=()=>Lo(),zo=()=>Lo(),Bo=(1n<<64n)-1n;function Vo(e){if(typeof e!=`bigint`)throw N.encoding(`an actor id is a bigint`);if(e===Bo)throw N.encoding(`actor id u64::MAX is reserved`);if(e<0n||e>Bo)throw N.encoding(`actor id ${e} is not an unsigned 64-bit integer`);return e}function Ho(){let e=/* @__PURE__ */ new Uint8Array(8);return crypto.getRandomValues(e),BigInt(`0x${No(e)}`)>>1n}let Uo=/* @__PURE__ */ new WeakMap,Wo=Symbol(`NodeRef`);var Go=class{constructor(e,t){if(e!==Wo)throw TypeError(`NodeRef handles come from Tx.treeCreate and Replica.tree`);Uo.set(this,t),Object.defineProperty(this,"id",{value:t.id,enumerable:!0,writable:!1,configurable:t.id===null})}equals(e){return this.id!==null||e.id!==null?this.id===e.id:this===e}};let Ko=e=>new Go(Wo,{id:e}),qo=(e,t)=>new Go(Wo,{id:null,pending:{tx:e,index:t}}),Jo=e=>Uo.get(e);function Yo(e,t){let n=Uo.get(e);!n||n.id!==null||(n.id=t,Object.defineProperty(e,"id",{value:t,enumerable:!0,writable:!1,configurable:!1}))}function Xo(e){return typeof e==`string`?JSON.stringify(e):e==null||typeof e==`number`||typeof e==`boolean`?String(e):typeof e==`bigint`?`${e}n`:Array.isArray(e)?`an array`:`a ${typeof e}`}function Z(e,t){if(typeof t!=`string`)throw new M(`invalid-value`,`${e} must be a string, not ${Xo(t)}`)}function Zo(e,t){if(typeof t!=`number`||!Number.isSafeInteger(t)||t<0)throw new M(`out-of-range`,`${e} ${Xo(t)} is out of range: it must be a whole number from 0`)}function Qo(e,t){let n=typeof t==`object`&&t?Object.getPrototypeOf(t):void 0;if(n!==Object.prototype&&n!==null||Object.getOwnPropertySymbols(t).length>0)throw new M(`invalid-value`,`${e} must be a plain object, not ${Xo(t)}`)}let $o=`[A-Za-z0-9][A-Za-z0-9:_-]{0,127}`,es=RegExp(`^${$o}$`);function ts(e,t){if(!es.test(t))throw N.invalidValue(e,`$id`,`cannot be \`${t}\`: an object id matches \`${$o}\``)}let ns=e=>`c:${e}`,rs=e=>`p:${e}`,is=e=>e.startsWith(`p:`)?Number(e.slice(2)):null,as=e=>e.slice(2);var os=class{constructor(e,t){this.committed=e,this.len=t,this.after=t>0?[{text:null,start:0,length:t}]:[]}before=[];beforeLength=0;after;committedString=null;engineChecks=0;splits(e){if(e<=0||e>=this.len)return!1;this.seek(e);let t=this.after[this.after.length-1],n=t.start+e-this.beforeLength;return t.text===null?this.committedSplits(n):or(t.text.charCodeAt(n))}insert(e,t){t&&(this.cut(e),this.before.push({text:t,start:0,length:t.length}),this.beforeLength+=t.length,this.len+=t.length)}delete(e,t){if(t!==0){this.cut(e);for(let e=t;e>0;){let t=this.after.pop();if(t.length>e){this.after.push({text:t.text,start:t.start+e,length:t.length-e});break}e-=t.length}this.len-=t}}text(){let e=this.before.some(e=>e.text===null)||this.after.some(e=>e.text===null)?this.committedText():``,t=t=>(t.text??e).slice(t.start,t.start+t.length),n=``;for(let e of this.before)n+=t(e);for(let e=this.after.length-1;e>=0;--e)n+=t(this.after[e]);return n}committedText(){return this.committedString??=this.committed?.toString()??``,this.committedString}committedSplits(e){let t=this.committed;if(!t||e<=0||e>=t.length)return!1;if(this.committedString===null&&this.engineChecks<4){this.engineChecks+=1;let n=t.convertPos(e,`utf16`,`unicode`);return n===void 0||t.convertPos(n,`unicode`,`utf16`)!==e}return or(this.committedText().charCodeAt(e))}seek(e){let{before:t,after:n}=this;for(;this.beforeLength>e;){let e=t.pop();this.beforeLength-=e.length,n.push(e)}for(;n.length>0&&this.beforeLength+n[n.length-1].length<=e;){let e=n.pop();this.beforeLength+=e.length,t.push(e)}}cut(e){this.seek(e);let t=e-this.beforeLength;if(t===0)return;let n=this.after.pop();this.before.push({text:n.text,start:n.start,length:t}),this.after.push({text:n.text,start:n.start+t,length:n.length-t}),this.beforeLength=e}},ss=class{constructor(e){this.len=e}edits=[];replay(e){let t=[...e],n=()=>N.engine(`a staged list edit does not fit the list`);for(let e of this.edits)if(`insert`in e){if(e.insert>t.length)throw n();t.splice(e.insert,0,e.item)}else if(`move`in e){if(e.move>=t.length||e.to>=t.length)throw n();t.splice(e.to,0,...t.splice(e.move,1))}else{if(e.remove>=t.length)throw n();t.splice(e.remove,1)}return t}},cs=class{constructor(e){this.committed=e}parents=/* @__PURE__ */ new Map;deleted=/* @__PURE__ */ new Set;parent(e){if(this.deleted.has(e))return;if(this.parents.has(e))return this.parents.get(e);if(is(e)!==null||!this.committed)return;let t=this.committed.getNodeByID(as(e));if(!t||t.isDeleted())return;let n=t.parent();return n?ns(n.id):null}contains(e){return this.parent(e)!==void 0}wouldCycle(e,t){let n=/* @__PURE__ */ new Set,r=t;for(;r!=null;){if(r===e||n.has(r))return!0;n.add(r),r=this.parent(r)}return!1}};function ls(e,t,n,r,i){let a=Array(r.length);for(let r of n){r.op!==`raiseSchema`&&i(r.id);try{fs(e,t,r,a)}catch(e){throw e instanceof M?e:N.engine(e instanceof Error?e.message:String(e))}}r.forEach((e,t)=>{let n=a[t];n!==void 0&&Yo(e,n)})}let us=1024;function ds(e,t,n){if(n.length<=us){e.insert(t,n);return}let r=[];for(let e=0;e<n.length;){r.push(e);let t=e+us,i=n.charCodeAt(t);t<n.length&&i>=56320&&i<=57343&&(t+=1),e=t}let i=n.length;for(let a=r.length-1;a>=0;--a)e.insert(t,n.slice(r[a],i)),i=r[a]}function fs(e,t,n,r){switch(n.op){case`create`:{let r=e.getMap(q).setContainer(n.id,new Me);r.set(ea,n.ty);for(let e of R(n.fields))r.set(e,n.fields[e]);let i=t.types[n.ty].fields;for(let e of R(i)){let t=i[e].kind;t===`text`?r.setContainer(e,new Ie):t===`list`?r.setContainer(e,new Pe):t===`tree`?r.setContainer(e,new Re):t===`map`&&r.setContainer(e,new Me)}return}case`set`:return J(e,n.id).set(n.field,n.value);case`delete`:return e.getMap(q).delete(n.id);case`textInsert`:return ds(ps(e,n.id,n.field,`text`),n.index,n.content);case`textDelete`:return ps(e,n.id,n.field,`text`).delete(n.index,n.count);case`textMark`:return ps(e,n.id,n.field,`text`).mark({start:n.start,end:n.end},n.key,n.value);case`textUnmark`:return ps(e,n.id,n.field,`text`).unmark({start:n.start,end:n.end},n.key);case`listInsert`:return ps(e,n.id,n.field,`list`).insert(n.index,n.item);case`listMove`:return ps(e,n.id,n.field,`list`).move(n.from,n.to);case`listRemove`:return ps(e,n.id,n.field,`list`).delete(n.index,1);case`mapSet`:return ps(e,n.id,n.field,`map`).set(n.key,n.value);case`mapDelete`:return ps(e,n.id,n.field,`map`).delete(n.key);case`treeCreate`:{let t=ps(e,n.id,n.field,`tree`).createNode(ms(r,n.parent));t.data.set(ta,n.object),r[n.node]=t.id;return}case`treeMove`:{let t=ms(r,n.node);if(t===void 0)throw N.engine(`a staged tree move has no node`);return ps(e,n.id,n.field,`tree`).move(t,ms(r,n.parent))}case`treeDelete`:{let t=ms(r,n.node);if(t===void 0)throw N.engine(`a staged tree delete has no node`);return ps(e,n.id,n.field,`tree`).delete(t)}case`raiseSchema`:{let t=sa(e);if(t!==null&&t>=n.version)return;e.getMap($i).set(na,n.version),e.getMap(ra).set(String(n.version),!0);return}}}function ps(e,t,n,r){let i=pa(J(e,t).get(n),r,t,n);if(!i)throw N.notFound(`${t}.${n}`);return i}function ms(e,t){if(t===null)return;let n=is(t);if(n===null)return as(t);let r=e[n];if(r===void 0)throw N.engine(`tree node ${n} is used before it exists`);return r}function Q(e,t){Z(`an object id`,e),Z(`a field name`,t)}let hs=(e,t)=>JSON.stringify([e,t]),gs=1;var _s=class e{constructor(e,t){this.doc=e,this.schema=t}serial=gs++;ops=[];created=/* @__PURE__ */ new Map;deleted=/* @__PURE__ */ new Set;registers=/* @__PURE__ */ new Map;texts=/* @__PURE__ */ new Map;lists=/* @__PURE__ */ new Map;trees=/* @__PURE__ */ new Map;maps=/* @__PURE__ */ new Map;createdByType=/* @__PURE__ */ new Map;nodes=[];raised=null;ended=!1;static stage(t,n,r){let i=new e(t,n),a;try{a=r(i)}finally{i.ended=!0}if(typeof a?.then==`function`)throw N.engine(`a transaction runs synchronously; its function returned a promise`);let{ops:o,nodes:s}=i,c=new Set(o.flatMap(e=>e.op===`create`?[e.id]:[]));return{result:a,staged:{empty:o.length===0,raisesSchema:o.some(e=>e.op===`raiseSchema`),undoTouches:o.flatMap((e,r)=>{if(e.op===`raiseSchema`)return[];let a=[{id:e.id,fields:`field`in e?[e.field]:null}],o;if(e.op===`listInsert`)o=e.item;else if(e.op===`treeCreate`)o=e.object;else if(e.op===`listMove`||e.op===`listRemove`)o=i.undoListItem(e.id,e.field,e.op===`listMove`?e.from:e.index,r);else if((e.op===`treeMove`||e.op===`treeDelete`)&&is(e.node)===null){let r=Y(t,n,e.id,e.field,`tree`)?.getNodeByID(as(e.node));o=r?Sa(r)??void 0:void 0}return o&&a.push({id:o,fields:[]}),a}),apply:(e,t,n)=>ls(e,t,o,s,n),registerWrites:o.flatMap(e=>(e.op===`set`||e.op===`mapSet`||e.op===`mapDelete`)&&c.has(e.id)?[]:e.op===`set`?[{id:e.id,field:null,key:e.field,value:e.value}]:e.op===`mapSet`?[{id:e.id,field:e.field,key:e.key,value:e.value}]:e.op===`mapDelete`?[{id:e.id,field:e.field,key:e.key,value:void 0}]:[])}}}undoListItem(e,t,n,r){for(let i=r-1;i>=0;--i){let r=this.ops[i];if(`id`in r&&r.id===e&&`field`in r&&r.field===t){if(r.op===`listInsert`){if(n===r.index)return r.item;n>r.index&&--n}else r.op===`listRemove`?n>=r.index&&(n+=1):r.op===`listMove`&&(n===r.to?n=r.from:r.from<r.to&&n>=r.from&&n<r.to?n+=1:r.to<r.from&&n>r.to&&n<=r.from&&--n)}}if(this.created.has(e))return;let i=Y(this.doc,this.schema,e,t,`list`)?.get(n);return typeof i==`string`?i:void 0}raiseSchemaVersion(e){if(this.live(),!(typeof e==`number`&&Number.isInteger(e)&&e>=0))throw N.invalidValue(`$meta`,`schemaVersion`,`cannot be raised to a value that is not a non-negative integer`);if(e>this.schema.schemaVersion)throw N.invalidValue(`$meta`,`schemaVersion`,`cannot be raised to ${e}: this kit reads schema versions up to ${this.schema.schemaVersion}`);return e<=this.minSchemaVersion()?!1:(this.raised=e,this.ops.push({op:`raiseSchema`,version:e}),!0)}minSchemaVersion(){return Math.max(sa(this.doc)??0,this.raised??0)}create(e,t,n){this.live(),Z(`a type name`,e),Qo(`the fields`,t);let r;if(n!==void 0){Qo(`the options`,n);let e=Object.keys(n).find(e=>e!==`id`);if(e!==void 0)throw new M(`invalid-value`,`the options take only \`id\`, not \`${e}\``);r=n.id,r!==void 0&&Z(`an object id`,r)}if(!Object.hasOwn(this.schema.types,e))throw N.unknownType(e);let i=this.schema.types[e];if(i.projected)throw N.invalidValue(e,`$type`,`projected objects are computed by the kit, never stored`);let a=R(t),o={};for(let n of a){let r=Pr(this.schema,e,n,t[n]);typeof r==`string`&&this.checkOf(e,n,i.fields[n],r),P(o,n,r)}for(let n of R(i.fields)){let r=i.fields[n];if(!xr(r.kind)&&!r.optional&&!Object.hasOwn(t,n))throw N.invalidValue(e,n,`is required`)}let s=r===void 0?Ro():this.freeId(e,r);this.created.set(s,{ty:e,op:this.ops.length});let c=this.createdByType.get(e);return c?c.push(s):this.createdByType.set(e,[s]),this.ops.push({op:`create`,id:s,ty:e,fields:o}),s}set(e,t,n){this.live(),Q(e,t);let{ty:r,def:i}=this.field(e,t),a=Pr(this.schema,r,t,n);typeof a==`string`&&this.checkOf(r,t,i,a),this.registers.set(hs(e,t),this.ops.length),this.ops.push({op:`set`,id:e,field:t,value:a})}delete(e){if(!this.exists(e))throw N.notFound(e);this.deleted.add(e),this.ops.push({op:`delete`,id:e})}exists(e){return this.live(),Z(`an object id`,e),!this.deleted.has(e)&&(this.created.has(e)||this.doc.getMap(q).get(e)instanceof Me)}wasDeleted(e){return this.deleted.has(e)}objectType(e){return this.live(),Z(`an object id`,e),this.typeOf(e)}get(e,t){this.live(),Q(e,t);let{ty:n,def:r}=this.field(e,t);if(xr(r.kind))throw N.invalidValue(n,t,`is a container; read it with its accessor`);let i=this.pendingRegister(e,t);return i===void 0?ha(this.doc,this.schema,e,t):i}textInsert(e,t,n,r){this.live(),Q(e,t),Zo(`index`,n),Z(`the content`,r),this.containerField(e,t,`text`);let i=this.textState(e,t,!0);if(n>i.len)throw N.outOfRange(n,i.len);if(i.splits(n))throw ys(n);let a=sr(r);this.texts.set(hs(e,t),i),i.insert(n,a),this.ops.push({op:`textInsert`,id:e,field:t,index:n,content:a})}textDelete(e,t,n,r){this.live(),Q(e,t),Zo(`index`,n),Zo(`count`,r),this.containerField(e,t,`text`);let i=this.textState(e,t,!0),a=n+r;if(a>i.len)throw N.outOfRange(a,i.len);if(i.splits(n))throw ys(n);if(i.splits(a))throw ys(a);this.texts.set(hs(e,t),i),i.delete(n,r),this.ops.push({op:`textDelete`,id:e,field:t,index:n,count:r})}textLength(e,t){return this.live(),Q(e,t),this.containerField(e,t,`text`),this.textState(e,t,!1).len}text(e,t){return this.live(),Q(e,t),this.containerField(e,t,`text`),this.textState(e,t,!1).text()}textMark(e,t,n,r,i,a){this.live(),Q(e,t),Zo(`index`,n),Zo(`index`,r),Z(`a mark key`,i);let o=this.markKey(e,t,i);if(a==null)throw N.invalidValue(o,t,`mark \`${i}\` takes a value; remove a mark with textUnmark`);let s=lr(a,e=>N.invalidValue(o,t,`mark \`${i}\` ${e}`));this.markRange(e,t,n,r)&&this.ops.push({op:`textMark`,id:e,field:t,start:n,end:r,key:i,value:s})}textUnmark(e,t,n,r,i){this.live(),Q(e,t),Zo(`index`,n),Zo(`index`,r),Z(`a mark key`,i),this.markKey(e,t,i),this.markRange(e,t,n,r)&&this.ops.push({op:`textUnmark`,id:e,field:t,start:n,end:r,key:i})}listInsert(e,t,n,r){this.live(),Q(e,t),Zo(`index`,n),Z(`a list item`,r);let{ty:i,def:a}=this.containerField(e,t,`list`);this.checkOf(i,t,a,r);let o=this.listState(e,t);if(n>o.len)throw N.outOfRange(n,o.len);o.len+=1,o.edits.push({insert:n,item:r}),this.ops.push({op:`listInsert`,id:e,field:t,index:n,item:r})}listMove(e,t,n,r){this.live(),Q(e,t),Zo(`index`,n),Zo(`index`,r),this.containerField(e,t,`list`);let i=this.listState(e,t);if(n>=i.len||r>=i.len)throw N.outOfRange(Math.max(n,r),i.len);i.edits.push({move:n,to:r}),this.ops.push({op:`listMove`,id:e,field:t,from:n,to:r})}listRemove(e,t,n){this.live(),Q(e,t),Zo(`index`,n),this.containerField(e,t,`list`);let r=this.listState(e,t);if(n>=r.len)throw N.outOfRange(n,r.len);--r.len,r.edits.push({remove:n}),this.ops.push({op:`listRemove`,id:e,field:t,index:n})}listItems(e,t){this.live(),Q(e,t),this.containerField(e,t,`list`);let n=pa(this.stored(e,t,!1),`list`,e,t),r=n?ba(n.toArray()):[];return this.lists.get(hs(e,t))?.replay(r)??r}list(e,t){return this.listItems(e,t)}mapSet(e,t,n,r){this.live(),Q(e,t),Z(`a map key`,n);let i=this.writableMap(e,t),a=lr(r,e=>N.invalidValue(i,t,e));Lr(this.schema,i,t,n,a),typeof a==`string`&&this.checkOf(i,t,this.schema.types[i].fields[t],a);let o=sr(n);this.mapWrites(e,t).set(o,this.ops.length),this.ops.push({op:`mapSet`,id:e,field:t,key:o,value:a})}mapDelete(e,t,n){this.live(),Q(e,t),Z(`a map key`,n),this.writableMap(e,t);let r=sr(n);this.mapWrites(e,t).set(r,null),this.ops.push({op:`mapDelete`,id:e,field:t,key:r})}mapGet(e,t,n){this.live(),Q(e,t),Z(`a map key`,n),this.containerField(e,t,`map`);let r=sr(n),i=this.maps.get(hs(e,t))?.get(r);if(i!==void 0)return this.pendingMapValue(i);let a=pa(this.stored(e,t,!1),`map`,e,t);return a?ga(a.get(r)):null}mapEntries(e,t){this.live(),Q(e,t),this.containerField(e,t,`map`);let n=pa(this.stored(e,t,!1),`map`,e,t),r=n?_a(n):[],i=this.maps.get(hs(e,t));if(!i)return r;let a=new Map(r);for(let[e,t]of i)t===null?a.delete(e):a.set(e,this.pendingMapValue(t));return[...a].sort((e,t)=>I(e[0],t[0]))}objectsOfType(e){this.live(),Z(`a type name`,e);let t=e=>!this.deleted.has(e),n=ya(this.doc,e);this.deleted.size>0&&(n=n.filter(t));let r=this.createdByType.get(e);return r?n.concat(r.filter(t)).sort(I):n}treeCreate(e,t,n,r){this.live(),Q(e,t),Z(`a tree node object`,r);let{ty:i,def:a}=this.containerField(e,t,`tree`);this.checkOf(i,t,a,r);let o=n==null?null:this.nodeKey(n),s=this.nodes.length,c=this.treeState(e,t);if(o!==null&&!c.contains(o))throw N.notFound(`parent node in ${e}.${t}`);c.parents.set(rs(s),o);let l=qo(this.serial,s);return this.nodes.push(l),this.ops.push({op:`treeCreate`,id:e,field:t,parent:o,object:r,node:s}),l}treeMove(e,t,n,r){this.live(),Q(e,t),this.containerField(e,t,`tree`);let i=this.nodeKey(n),a=r==null?null:this.nodeKey(r),o=this.treeState(e,t);if(!o.contains(i)||a!==null&&!o.contains(a))throw N.notFound(`node in ${e}.${t}`);if(a!==null&&o.wouldCycle(i,a))throw N.cycle();o.parents.set(i,a),this.ops.push({op:`treeMove`,id:e,field:t,node:i,parent:a})}treeDedupe(e,t){this.live(),Q(e,t),this.containerField(e,t,`tree`);let n=0;for(let r=this.treeDuplicate(e,t);r;r=this.treeDuplicate(e,t)){let[i,a]=r;for(let n of this.tree(e,t))n.parent?.id===a.id&&this.treeMove(e,t,n.node,i);let o=this.nodeKey(a),s=this.treeState(e,t);s.parents.delete(o),s.deleted.add(o),this.ops.push({op:`treeDelete`,id:e,field:t,node:o}),n+=1}return n}treeDuplicate(e,t){let n=this.tree(e,t).filter(e=>e.node.id!==null),r=/* @__PURE__ */ new Map,i=/* @__PURE__ */ new Map,a=null;for(let e of n){r.set(e.node.id,e.parent?.id??null);let t=i.get(e.object)??[];t.push(e.node),i.set(e.object,t),t.length===2&&a===null&&(a=e.object)}let o=a===null?void 0:i.get(a);if(!o)return null;let s=new Set(o.map(e=>e.id)),c=o.filter(e=>{for(let t=r.get(e.id)??null;t!==null;t=r.get(t)??null)if(s.has(t))return!1;return!0}).sort((e,t)=>vs(e.id,t.id))[0],l=c&&o.find(e=>e.id!==c.id);return c&&l?[c,l]:null}tree(e,t){this.live(),Q(e,t),this.containerField(e,t,`tree`);let n=pa(this.stored(e,t,!1),`tree`,e,t),r=/* @__PURE__ */ new Map,i=/* @__PURE__ */ new Map,a=/* @__PURE__ */ new Map,o=(e,t)=>{let n=r.get(e);n?n.push(t):r.set(e,[t]),i.set(t,e)},s=(e,t)=>{for(let n of e??[]){let e=ns(n.id);o(t,e),a.set(e,Sa(n)),s(n.children(),e)}};n&&s(n.roots(),null);for(let n of this.ops)if(n.op===`treeCreate`&&n.id===e&&n.field===t){let e=rs(n.node);o(n.parent,e),a.set(e,n.object)}else if(n.op===`treeMove`&&n.id===e&&n.field===t){let e=i.get(n.node);e!==void 0&&r.set(e,(r.get(e)??[]).filter(e=>e!==n.node)),o(n.parent,n.node)}else if(n.op===`treeDelete`&&n.id===e&&n.field===t){let e=i.get(n.node);e!==void 0&&r.set(e,(r.get(e)??[]).filter(e=>e!==n.node)),i.delete(n.node)}let c=e=>{let t=is(e);if(t===null)return Ko(as(e));let n=this.nodes[t];if(!n)throw N.engine(`tree node ${t} was not staged`);return n},l=[],u=(e,t)=>{for(let n of r.get(e)??[]){let e=a.get(n);if(e==null)continue;let r=c(n);l.push({node:r,object:e,parent:t}),u(n,r)}};return u(null,null),l}freeId(e,t){if(ts(e,t),this.created.has(t)||this.deleted.has(t)||this.doc.getMap(q).get(t)!==void 0)throw N.invalidValue(e,`$id`,`cannot be \`${t}\`: that object id is taken`);return t}live(){if(this.ended)throw N.engine(`the transaction has ended`)}typeOf(e){if(this.deleted.has(e))throw N.notFound(e);let t=this.created.get(e);if(t)return t.ty;let n=ua(this.doc,e);if(!Object.hasOwn(this.schema.types,n))throw N.unknownType(n);return n}field(e,t){let n=this.typeOf(e),r=this.schema.types[n].fields;if(!Object.hasOwn(r,t))throw N.unknownField(n,t,Object.keys(r).sort());return{ty:n,def:r[t]}}containerField(e,t,n){let r=this.field(e,t);if(r.def.kind!==n)throw fa(r.ty,t,n);return r}checkOf(e,t,n,r){let i=n?.of;if(i===void 0)return;let a=null;if(!this.deleted.has(r)&&(a=this.created.get(r)?.ty??null,a===null))try{a=ua(this.doc,r)}catch{a=null}if(a!==null&&a!==i)throw N.invalidValue(e,t,`expects an object of type \`${i}\`, and \`${r}\` is a \`${a}\``)}stored(e,t,n){if(this.created.has(e))return;let r=J(this.doc,e).get(t);if(r===void 0&&n)throw N.notFound(`${e}.${t}`);return r}mapWrites(e,t){let n=hs(e,t),r=this.maps.get(n);return r||this.maps.set(n,r=/* @__PURE__ */ new Map),r}pendingMapValue(e){if(e===null)return null;let t=this.ops[e];return t.op===`mapSet`?F(t.value):null}pendingRegister(e,t){let n=this.registers.get(hs(e,t));if(n!==void 0){let e=this.ops[n];if(e.op===`set`)return F(e.value)}let r=this.created.get(e);if(!r)return;let i=this.ops[r.op];return i.op===`create`?F(i.fields[t]??null):void 0}textState(e,t,n){let r=this.texts.get(hs(e,t));if(r)return r;let i=pa(this.stored(e,t,n),`text`,e,t);return new os(i,i?.length??0)}markKey(e,t,n){let{ty:r,def:i}=this.containerField(e,t,`text`);if(!i.marks||!Object.hasOwn(i.marks,n))throw N.invalidValue(r,t,`declares no mark \`${n}\``);return r}markRange(e,t,n,r){let i=this.textState(e,t,!0);if(r>i.len)throw N.outOfRange(r,i.len);if(n>r)throw N.outOfRange(n,r);if(i.splits(n))throw ys(n);if(i.splits(r))throw ys(r);return n<r}listState(e,t){let n=hs(e,t),r=this.lists.get(n);return r||(r=new ss(pa(this.stored(e,t,!0),`list`,e,t)?.length??0),this.lists.set(n,r)),r}writableMap(e,t){let{ty:n}=this.containerField(e,t,`map`);return pa(this.stored(e,t,!0),`map`,e,t),n}treeState(e,t){let n=hs(e,t),r=this.trees.get(n);return r||(r=new cs(pa(this.stored(e,t,!0),`tree`,e,t)),this.trees.set(n,r)),r}nodeKey(e){let t=Jo(e);if(!t)throw N.notFound(`a tree node the KDM did not make`);if(t.id!==null)return ns(t.id);if(t.pending?.tx===this.serial)return rs(t.pending.index);throw N.notFound(`a tree node from a transaction that did not commit`)}};function vs(e,t){let n=e=>{let t=e.indexOf(`@`);return[BigInt(e.slice(t+1)),Number(e.slice(0,t))]},[r,i]=n(e),[a,o]=n(t);return r===a?i-o:r<a?-1:1}let ys=e=>new M(`out-of-range`,`index ${e} splits a character`),bs=`kdm-rollback`,xs=`system`,Ss=e=>N.engine(`applying a validated transaction failed: ${e instanceof Error?e.message:String(e)}`),$=e=>e instanceof Error?e.message:String(e),Cs=e=>$(e).includes(`not included in the shallow history`)?N.historyMissing(`the changes depend on history before this shallow replica's root; rebase it on a newer snapshot`):N.encoding($(e));function ws(e,t){let n=/* @__PURE__ */ new Set,r=[],i=e.subscribe(e=>{for(let t of e.events)if(t.path[0]===q){if(t.path.length>=2){if(n.add(String(t.path[1])),t.diff.type===`map`)for(let[e,n]of Object.entries(t.diff.updated))et(n)||r.push(Ts(t.target,e))}else if(t.diff.type===`map`)for(let e of Object.keys(t.diff.updated))n.add(e)}});try{return{value:t(),changed:n,registers:r}}finally{i()}}let Ts=(e,t)=>`${e}\0${t}`,Es=e=>{let t=e.indexOf(`\0`);return[e.slice(0,t),e.slice(t+1)]},Ds=e=>new Set([...e].sort(I));function Os(e,t){Z(`an object id`,e),Z(`a field name`,t)}function ks(e){if(typeof e!=`function`)throw new M(`invalid-value`,`a transaction needs a function`)}function As(e){if(!e.isShallow())return!0;let t=K.fromEngine(e.shallowSinceVV()),n=e.fork();try{n.checkout(e.shallowSinceFrontiers());let r=n.getMap(ia).get(aa);return r instanceof Uint8Array&&K.decode(r).equals(t)}catch{return!1}finally{n.free()}}var js=class e{constructor(e,t,n,r){this.doc=e,this.schema=n,this.actor=r,va(e,n),this.docIdBytes=t.slice(),e.setPeerId(r),this.undoManagers=Rs(e,this.runs)}docIdBytes;undoManagers;changed=/* @__PURE__ */ new Set;busy=!1;grouping=!1;runs={current:null,next:1,replaying:!1,popped:null,pending:[],groups:/* @__PURE__ */ new Map};closedBy=null;pending=/* @__PURE__ */ new Map;importedRegisters=/* @__PURE__ */ new Set;keptRegisters=/* @__PURE__ */ new Map;ownRegisters=/* @__PURE__ */ new Map;localWrites=/* @__PURE__ */ new Map;static create(t,n,r){return e.createWith(t,n,r,Zn(`create`),()=>{}).replica}static createWith(t,n,r,i,a){er(i),ks(a);let o=Io(n);Vo(r);let s=new Oe;s.setPeerId(r),Yi(s,t);let c=s.getMap($i);c.set(`docId`,Po(o)),c.set(`kit`,t.kit),c.set(na,t.schemaVersion),c.set(`kdm`,1);let{result:l,staged:u}=_s.stage(s,t,a);try{u.apply(s,t,()=>{})}catch(e){throw Ss(e)}return s.commit({origin:Ns(i.kind),message:tr(i,t.schemaVersion)}),{replica:new e(s,o,t,r),result:l}}static load(t,n,r){Vo(r);let{docId:i,engine:a}=qi(n),o=new Oe;Yi(o,t);let s;try{s=o.import(a)}catch(e){throw N.encoding($(e))}let c=o.getMap($i),l=e=>{let t=c.get(e);return t===void 0||et(t)?null:t},u=l(`kit`);if(u===null)throw N.encoding(`not a snapshot: meta.kit is missing (load a snapshot or a full export, not changes only)`);if(u!==t.kit)throw N.schema(`the document belongs to ${L(u)}, not \`${t.kit}\``);let d=l(`kdm`);if(d!==1)throw N.schema(`the document uses kdm ${L(d)}; this build reads 1`);let f=sa(o);if(f===null)throw N.schema(`the document has schema version ${L(l(na))}; this kit reads schema versions up to ${t.schemaVersion}`);if(f>t.schemaVersion)throw N.newerSchema(f,t.schemaVersion);let p=l(`docId`),m=null;try{m=typeof p==`string`?Io(p):null}catch{m=null}if(!m||!Ji(m,i))throw N.encoding(`the bytes are labelled as document ${Po(i)}, but hold another`);let h=new e(o,i,t,r);return h.notePending(s),h}get docId(){return Po(this.docIdBytes)}version(){return K.fromEngine(this.doc.oplogVersion())}minSchemaVersion(){return sa(this.doc)??0}authorSchemaVersion(){let e=null;for(let t of this.doc.getAllChanges().values())for(let n of t){let t=n.message?nr(n.message)?.schemaVersion??null:null;t!==null&&(e===null||t>e)&&(e=t)}return e}transact(e,t){this.idle(),this.checkOpen(),er(e),ks(t),this.busy=!0;let n;try{n=_s.stage(this.doc,this.schema,t)}finally{this.busy=!1}let{result:r,staged:i}=n;if(i.empty)return r;if(i.raisesSchema&&e.kind!==xs)throw N.invalidValue(`$meta`,`schemaVersion`,`is raised only in a \`system\` transaction, not \`${e.kind}\``);let a=this.doc.frontiers(),o=/* @__PURE__ */ new Set;try{i.apply(this.doc,this.schema,e=>o.add(e))}catch(e){if(this.doc.commit({origin:bs}),!Vs(this.doc.frontiers(),a)){try{this.doc.revertTo(a)}catch(e){throw N.engine($(e))}this.doc.commit({origin:bs,message:tr(Zn(`rollback`),this.schema.schemaVersion)})}throw Ss(e)}this.runs.pending=i.undoTouches;let s=Jn(e.kind);if(this.runs.current!==null&&s!==null){let e=`${s}:${this.runs.current}`;this.runs.groups.set(e,Is(this.runs.groups.get(e)??[],i.undoTouches))}this.doc.commit({origin:Ns(e.kind),message:tr(e,this.schema.schemaVersion)});for(let e of o)this.changed.add(e);return this.noteOwnWrites(i.registerWrites,Jn(e.kind)),r}exists(e){Z(`an object id`,e);try{return J(this.doc,e),!0}catch{return!1}}objectType(e){return Z(`an object id`,e),ua(this.doc,e)}get(e,t){return Os(e,t),ha(this.doc,this.schema,e,t)}text(e,t){return Os(e,t),Y(this.doc,this.schema,e,t,`text`)?.toString()??``}textDelta(e,t){return Os(e,t),ma(this.doc,this.schema,e,t)}textLength(e,t){return Os(e,t),Y(this.doc,this.schema,e,t,`text`)?.length??0}list(e,t){Os(e,t);let n=Y(this.doc,this.schema,e,t,`list`);return n?ba(n.toArray()):[]}mapGet(e,t,n){Os(e,t),Z(`a map key`,n);let r=Y(this.doc,this.schema,e,t,`map`);return r?ga(r.get(n)):null}mapEntries(e,t){Os(e,t);let n=Y(this.doc,this.schema,e,t,`map`);return n?_a(n):[]}tree(e,t){Os(e,t);let n=[],r=Y(this.doc,this.schema,e,t,`tree`),i=(e,t)=>{for(let r of e??[]){let e=Sa(r);if(e===null)continue;let a=Ko(r.id);n.push({node:a,object:e,parent:t}),i(r.children(),a)}};return r&&i(r.roots(),null),n}objectsOfType(e){return Z(`a type name`,e),ya(this.doc,e)}exportChanges(e=K.empty()){if(this.checkOpen(),!(e instanceof K))throw new M(`invalid-value`,"a version must be a `Version`");if(!this.reaches(e))return this.snapshot();let t;try{t=this.doc.export({mode:`update`,from:e.toEngine()})}catch(e){throw N.encoding($(e))}return Ki(this.docIdBytes,t)}importChanges(e){this.idle(),this.checkOpen();let{docId:t,engine:n}=qi(e);if(!Ji(t,this.docIdBytes))throw N.wrongDocument(this.docId,Po(t));let r=this.doc.oplogFrontiers(),i=null,{value:a,changed:o,registers:s}=ws(this.doc,()=>{try{return this.doc.import(n)}catch(e){return i=e,null}});try{va(this.doc,this.schema)}catch(e){throw this.doc.isShallow()?this.closedBy=e:this.restore(r),e}let c=sa(this.doc);if(c!==null&&c>this.schema.schemaVersion){let e=N.newerSchema(c,this.schema.schemaVersion);throw this.doc.isShallow()?(this.closedBy=e,e):(this.restore(r),e)}if(a===null)throw Cs(i);if([`person`,`agent`].some(e=>this.undoManagers[e].canUndo()||this.undoManagers[e].canRedo()))for(let e of s)this.importedRegisters.add(e);this.notePending(a);for(let e of o)this.changed.add(e);return this.reopenUndoGroup(),Ds(o)}hasPendingChanges(){let e=this.doc.oplogVersion();for(let[t,n]of this.pending)if((e.get(t)??0)<n)return!0;return!1}takeChanged(){let e=Ds(this.changed);return this.changed=/* @__PURE__ */ new Set,e}snapshot(){this.checkOpen();let e;try{e=this.doc.export({mode:`snapshot`})}catch(e){throw N.encoding($(e))}return Ki(this.docIdBytes,e)}shallowSnapshot(e){if(this.checkOpen(),!(e instanceof K))throw new M(`invalid-value`,"a version must be a `Version`");if(!this.version().includes(e))throw N.invalidValue(`$shallow`,`version`,`names changes this replica does not have`);let t=this.reaches(e)?this.doc.vvToFrontiers(e.toEngine()):this.doc.shallowSinceFrontiers(),n;try{n=this.doc.export({mode:`shallow-snapshot`,frontiers:t})}catch(e){throw N.encoding($(e))}return Ki(this.docIdBytes,n)}isShallow(){return this.doc.isShallow()}shallowRoot(){return this.doc.isShallow()?K.fromEngine(this.doc.shallowSinceVV()):null}carriesWholeHistory(){return this.checkOpen(),As(this.doc)}static snapshotCarriesWholeHistory(e){let{engine:t}=qi(e),n=new Oe;try{if(de(t,!0).mode!==`shallow-snapshot`)return!0;n.import(t)}catch(e){throw n.free(),N.encoding($(e))}try{return As(n)}finally{n.free()}}reaches(e){let t=this.shallowRoot();return t===null||e.includes(t)}rebase(t){this.idle(),this.checkOpen();let{docId:n}=qi(t);if(!Ji(n,this.docIdBytes))throw N.wrongDocument(this.docId,Po(n));let r=e.load(this.schema,t,this.actor);if(!r.version().includes(this.version())&&(r.importChanges(this.exportChanges(r.version())),!r.version().includes(this.version())))throw N.historyMissing(`the snapshot is older than this shallow replica's root; rebase it on a newer one`);let i=e=>e.objects??{},a=i(this.stateJson()),o=i(r.stateJson()),s=/* @__PURE__ */ new Set;for(let e of/* @__PURE__ */ new Set([...Object.keys(a),...Object.keys(o)]))(L(a[e]??null)!==L(o[e]??null)||e in a!=e in o)&&s.add(e);zs(this.undoManagers),this.doc=r.doc,this.undoManagers=r.undoManagers,this.importedRegisters.clear(),this.keptRegisters.clear(),this.ownRegisters.clear(),this.localWrites.clear();for(let[e,t]of r.pending)this.pending.set(e,Math.max(t,this.pending.get(e)??0));this.prunePending(),this.reopenUndoGroup();for(let e of s)this.changed.add(e);return Ds(s)}undo(){return this.undoIn(`person`)}redo(){return this.redoIn(`person`)}undoIn(e){let t=()=>this.undoManagers[Bs(e)];return this.history(e,()=>this.replayRun(()=>t().topUndoValue(),()=>t().undo()))}redoIn(e){let t=()=>this.undoManagers[Bs(e)];return this.history(e,()=>this.replayRun(()=>t().topRedoValue(),()=>t().redo()))}replayRun(e,t){this.runs.replaying=!0;try{let n=Fs(e()),r=t(),i=r;for(;i&&n!==null&&Fs(e())===n;)i=t();return r}finally{this.runs.replaying=!1,this.runs.popped=null}}canUndo(){return this.canUndoIn(`person`)}canRedo(){return this.canRedoIn(`person`)}canUndoIn(e){return this.idle(),this.undoManagers[Bs(e)].canUndo()}canRedoIn(e){return this.idle(),this.undoManagers[Bs(e)].canRedo()}undoTouchesIn(e){return this.idle(),this.historyTouches(e,this.undoManagers[Bs(e)].topUndoValue())}redoTouchesIn(e){return this.idle(),this.historyTouches(e,this.undoManagers[Bs(e)].topRedoValue())}historyTouches(e,t){let n=Ps(t);return structuredClone(n?.run==null?n?.touches??[]:this.runs.groups.get(`${e}:${n.run}`)??n.touches)}beginUndoGroup(){if(this.idle(),this.grouping)throw N.invalidValue(`$undo`,`group`,`is already open`);try{this.undoManagers.person.groupStart()}catch(e){throw N.engine($(e))}try{this.undoManagers.agent.groupStart()}catch(e){throw this.undoManagers.person.groupEnd(),N.engine($(e))}this.grouping=!0,this.runs.current=this.runs.next,this.runs.next+=1}endUndoGroup(){this.idle(),this.closeUndoGroup()}clearRedo(){this.idle(),this.undoManagers.person.clearRedo()}subscribeText(e,t,n){Os(e,t);let r=Y(this.doc,this.schema,e,t,`text`);if(!r)throw N.notFound(`${e}.${t}`);return r.subscribe(e=>{for(let t of e.events)t.diff.type===`text`&&n(t.diff.diff,e.by===`local`)})}fork(e){return this.forkAt(this.version(),e)}forkAt(t,n){if(this.idle(),this.checkOpen(),Vo(n),!(t instanceof K))throw new M(`invalid-value`,"a version must be a `Version`");if(!this.version().includes(t))throw N.invalidValue(`$draft`,`version`,`names changes this replica does not have`);if(this.doc.isShallow()&&!t.equals(this.version()))throw N.historyMissing(`a shallow replica forks only at its current version; read earlier versions from the server`);let r;try{r=this.doc.isShallow()?this.doc.fork():this.doc.forkAt(this.doc.vvToFrontiers(t.toEngine()))}catch(e){throw N.engine($(e))}return Yi(r,this.schema),new e(r,this.docIdBytes,this.schema,n)}copyAsNewDocument(t,n){this.idle(),this.checkOpen();let r=Io(t);if(Vo(n),Ji(r,this.docIdBytes))throw N.invalidValue(`$copy`,`docId`,`is this document's; a copy needs a new one`);let i;try{let e=new Oe;Yi(e,this.schema),e.import(this.doc.export({mode:`shallow-snapshot`,frontiers:this.doc.oplogFrontiers()})),e.setPeerId(n);let t=new Map(e.oplogVersion().toJSON()),a=n.toString();t.set(a,(t.get(a)??0)+1),e.getMap($i).set(`docId`,Po(r)),e.getMap(ia).set(aa,We.parseJSON(t).encode()),e.commit({origin:xs,message:tr(Zn(`new-document`),this.schema.schemaVersion)}),i=e.export({mode:`shallow-snapshot`,frontiers:e.oplogFrontiers()})}catch(e){throw N.encoding($(e))}let a=e.load(this.schema,Ki(r,i),n);if(!a.carriesWholeHistory())throw N.engine(`the copy is not rooted at the birth it records`);return a}mergedState(e){return this.idle(),this.checkOpen(),Pa(this.mergedDoc(e),this.schema)}mergeChanges(e,t){this.idle(),this.checkOpen(),er(t);let n=Jn(t.kind),r=this.mergedDoc(e),i=this.doc.oplogFrontiers(),a=r.oplogFrontiers();if(Vs(i,a))return/* @__PURE__ */ new Set;let o;try{o=r.diff(i,a)}catch(e){throw N.engine($(e))}let s=null,{changed:c,registers:l}=ws(this.doc,()=>{try{this.doc.applyDiff(o)}catch(e){s=e;return}this.doc.commit({origin:n===null?t.kind:`person`,message:tr(t,this.schema.schemaVersion)})});if(s!==null)throw this.doc.commit({origin:bs}),Vs(this.doc.oplogFrontiers(),i)||(this.doc.revertTo(i),this.doc.commit({origin:bs,message:tr(Zn(`rollback`),this.schema.schemaVersion)})),N.engine($(s));for(let e of l){let t=this.registerValue(e);t.container||(this.ownRegisters.set(e,t.value),this.localWrites.set(e,{by:n===null?null:`person`,value:t.value}))}for(let e of c)this.changed.add(e);return Ds(c)}mergedDoc(e){let{docId:t,engine:n}=qi(e);if(!Ji(t,this.docIdBytes))throw N.wrongDocument(this.docId,Po(t));let r=this.doc.fork();Yi(r,this.schema);let i;try{i=r.import(n)}catch(e){throw Cs(e)}if(i.pending&&i.pending.size>0)throw N.invalidValue(`$draft`,`changes`,`depend on changes this replica does not have`);va(r,this.schema);let a=sa(r);if(a!==null&&a>this.schema.schemaVersion)throw N.newerSchema(a,this.schema.schemaVersion);return r}changesSince(e,t={}){if(this.idle(),this.checkOpen(),!(e instanceof K))throw new M(`invalid-value`,"a version must be a `Version`");if(!this.version().includes(e))throw N.historyMissing(`the version is not in this replica's history (another document's, or the document was replaced since)`);if(e.equals(this.version()))return vo([],{},{},()=>null,!1);let n=this.forkAt(e,this.actor),r=[...this.forkAt(e,this.actor).importChanges(this.exportChanges(e))].sort(I),i=(e,t)=>{try{return ua(e,t)}catch{return null}},a=e=>{let t={};for(let n of r)i(e,n)!==null&&(t[n]=Aa(e,this.schema,n));return t},o=e=>i(this.doc,e)??i(n.doc,e),s={objects:a(n.doc)},c={objects:a(this.doc)},l=_o(vo(r,s.objects,c.objects,o,!1),this.schema);return t.textSpans&&(l=Ba(l,s,c)),So(l,this.schema,V.ofReplica(n),V.ofReplica(this))}stateJson(){return Pa(this.doc,this.schema)}stateText(){return L(this.stateJson())}restore(e){let t;try{t=this.doc.forkAt(e)}catch(e){throw N.engine($(e))}Yi(t,this.schema),t.setPeerId(this.actor),zs(this.undoManagers),this.doc=t,this.undoManagers=Rs(t,this.runs),this.importedRegisters.clear(),this.keptRegisters.clear(),this.ownRegisters.clear(),this.localWrites.clear(),this.reopenUndoGroup()}notePending(e){for(let[t,{end:n}]of e.pending??[])this.pending.set(t,Math.max(n,this.pending.get(t)??0));this.prunePending()}prunePending(){if(this.pending.size===0)return;let e=this.doc.oplogVersion();for(let[t,n]of this.pending)(e.get(t)??0)>=n&&this.pending.delete(t)}checkOpen(){if(this.closedBy)throw this.closedBy}idle(){if(this.busy)throw N.engine(`the document cannot change while a transaction runs; transactions do not nest`)}closeUndoGroup(){this.grouping&&(this.undoManagers.person.groupEnd(),this.undoManagers.agent.groupEnd(),this.grouping=!1,this.runs.current=null)}reopenUndoGroup(){if(!this.grouping)return;let e=e=>{try{return e.groupStart(),!0}catch(e){return $(e).includes(`already an active undo group`)}};e(this.undoManagers.person)&&e(this.undoManagers.agent)||(this.undoManagers.person.groupEnd(),this.undoManagers.agent.groupEnd(),this.grouping=!1)}history(e,t){this.idle(),this.checkOpen(),this.closeUndoGroup();let n=this.othersRegisters().map(([e,t])=>[e,t,void 0]);for(let[t,r,i]of this.otherScopesRegisters(e))n.push([t,r,i]);let{value:r,changed:i,registers:a}=ws(this.doc,()=>{try{return t()}catch(e){throw N.engine($(e))}});for(let e of i)this.changed.add(e);for(let t of a){let n=this.registerValue(t);n.container||(this.ownRegisters.set(t,n.value),this.localWrites.set(t,{by:e,value:n.value}))}let o=new Set(a),s=!1;for(let[e,t,r]of n){if(!o.has(e))continue;let n=this.registerValue(e);if(n.container||Ms(n.value,t))continue;let[i,a]=Es(e),c=this.doc.getContainerById(i);t===void 0?c.delete(a):c.set(a,t),r===void 0?(this.localWrites.delete(e),this.keptRegisters.set(e,t)):this.localWrites.set(e,{by:r,value:t}),s=!0}return s&&this.doc.commit({origin:xs,message:tr(Zn(`keep-later-changes`),this.schema.schemaVersion)}),r}otherScopesRegisters(e){let t=[],n=String(this.actor);for(let[r,i]of this.localWrites){let a=this.registerValue(r);a.container||!Ms(a.value,i.value)||a.lastEditor!==n?this.localWrites.delete(r):i.by!==e&&t.push([r,a.value,i.by])}return t}registerValue(e){let[t,n]=Es(e),r=this.doc.getContainerById(t);if(!r)return{value:void 0,container:!1,lastEditor:void 0};let i=r.get(n);return{value:i,container:et(i),lastEditor:r.getLastEditor(n)}}othersRegisters(){let e=[],t=String(this.actor);for(let[t,n]of this.keptRegisters){let r=this.registerValue(t);!r.container&&Ms(r.value,n)?e.push([t,n]):this.keptRegisters.delete(t)}for(let n of this.importedRegisters){if(this.keptRegisters.has(n))continue;let r=this.registerValue(n);r.container||r.lastEditor===void 0||r.lastEditor===t?this.importedRegisters.delete(n):this.ownRegisters.has(n)&&Ms(this.ownRegisters.get(n),r.value)||e.push([n,r.value])}return e}noteOwnWrites(e,t){for(let{id:n,field:r,key:i,value:a}of e){let e;try{let t=J(this.doc,n);if(r===null)e=t.id;else{let n=t.get(r);if(!et(n)||n.kind()!==`Map`)continue;e=n.id}}catch{continue}this.ownRegisters.set(Ts(e,i),a),this.localWrites.set(Ts(e,i),{by:t,value:a})}}};let Ms=(e,t)=>e===t||e!==void 0&&t!==void 0&&L(e)===L(t),Ns=e=>Jn(e)??e,Ps=e=>e&&typeof e==`object`&&`touches`in e?e:null,Fs=e=>Ps(e)?.run??null;function Is(e,t){let n=/* @__PURE__ */ new Map;for(let r of[...e,...t]){let e=n.get(r.id);n.set(r.id,e===null||r.fields===null?null:/* @__PURE__ */ new Set([...e??[],...r.fields]))}return[...n].sort(([e],[t])=>e<t?-1:+(e>t)).map(([e,t])=>({id:e,fields:t===null?null:[...t].sort()}))}let Ls=(e,t,n)=>new He(e,{mergeInterval:0,excludeOriginPrefixes:[bs,xs,`import`,t===`person`?`agent`:`person`,`undo`],onPush:()=>{if(n.replaying)return{value:n.popped,cursors:[]};let e=n.current===null?Is([],n.pending):n.groups.get(`${t}:${n.current}`)??n.pending;return{value:{run:n.current,touches:e},cursors:[]}},onPop:(e,t)=>{n.popped=Ps(t?.value)}}),Rs=(e,t)=>({person:Ls(e,`person`,t),agent:Ls(e,`agent`,t)}),zs=e=>{e.person.free(),e.agent.free()};function Bs(e){if(e!==`person`&&e!==`agent`)throw new M(`invalid-value`,`an undo scope is "person" or "agent"`);return e}let Vs=(e,t)=>{let n=e=>e.map(e=>`${e.counter}@${e.peer}`).sort().join(`,`);return n(e)===n(t)},Hs=(1n<<64n)-1n;var Us=class{state;constructor(e){this.state=BigInt(e)*11400714819323198485n&Hs|1n}nextU64(){return this.state^=this.state>>12n,this.state=(this.state^this.state<<25n)&Hs,this.state^=this.state>>27n,this.state*2685821657736338717n&Hs}below(e){return e<=0?0:Number(this.nextU64()%BigInt(e))}shuffle(e){for(let t=e.length-1;t>0;--t){let n=this.below(t+1);[e[t],e[n]]=[e[n],e[t]]}}},Ws=/* @__PURE__ */ Object.freeze({__proto__:null,BatchError:U,COMMENT_THREAD_TYPE:ki,COMMENT_THREAD_TYPE_DEF:ji,COMMENT_TYPE:Ai,COMMENT_TYPE_DEF:Mi,ChangeAudit:Ra,EXCERPT_CHARS:80,FOOTPRINT_MIN_RUN:4,KDM_VERSION:1,KdmError:M,LEAF_EDITS:16,Network:class{replicas;rng;constructor(e,t,n=1){if(!Number.isSafeInteger(t)||t<1)throw N.outOfRange(0,0);let r=js.create(e,zo(),1n),i=r.snapshot();this.replicas=[r];for(let n=2;n<=t;n+=1)this.replicas.push(js.load(e,i,BigInt(n)));this.rng=new Us(n)}deliver(e,t){let n=this.replica(t);n.importChanges(this.replica(e).exportChanges(n.version()))}syncAll(){let e=this.replicas.length,t=[];for(let n=0;n<e;n+=1)for(let r=0;r<e;r+=1)n!==r&&t.push([n,r]);this.rng.shuffle(t);for(let[e,n]of t)this.deliver(e,n);for(let[e,n]of t)this.replica(n).importChanges(this.replica(e).exportChanges(K.empty()))}converged(){let[e,...t]=this.replicas;if(!e)throw Error(`the network has no replicas`);let n=e.stateText();return t.forEach((e,t)=>{let r=e.stateText();if(r!==n)throw Error(`replica 0 and ${t+1} differ:
5
+ ${n}
6
+ ---
7
+ ${r}`)}),n}replica(e){if(!Number.isSafeInteger(e)||e<0||e>=this.replicas.length)throw N.outOfRange(e,this.replicas.length);return this.replicas[e]}},NodeRef:Go,OBJECT_MODEL_CAPABILITY:ei,ProjectionView:Qr,Replica:js,Rng:Us,ToolIds:V,Tx:_s,Version:K,addThread:Ii,agent:Xn,batchOrigin:ri,canonicalText:L,checkValue:Nr,commentThreads:Ui,decodeCommit:nr,decodeOrigin:rr,deleteComment:zi,deleteThread:Bi,describeModel:Di,diffIsEmpty:xo,diffStates:yo,diffWithToolIds:So,encodeCommit:tr,encodeOrigin:Qn,fieldDef:Mr,footprintLists:Oo,hasMarks:Tr,hasProjections:Yr,hasToolIds:hr,isCommentType:Ni,isContainer:xr,newDocId:zo,newObjectId:Ro,objectReadOnly:gr,openEnvelope:qi,parseSchema:Ar,person:Yn,randomActor:Ho,readField:Ei,relocateAnchorRange:Wi,replicaTarget:ti,reply:Li,resolveValues:ai,rootId:oi,runBatch:_i,schemaOfDescription:Oi,setResolved:Ri,system:Zn,textSpan:za,touchesOnlyComments:Fa,typeDef:jr,undoScopeOf:Jn,uuidFromBytes:Po,uuidToBytes:Io,uuidV7:Lo,withDeletedBasis:co,withFootprint:Ao,withLeafEdits:_o,withTextBasis:bo,withTextSpans:Ba,wrapEnvelope:Ki});let Gs=`the kit refused the changes`;function Ks(e){let t=3735928559,n=1103547991;for(let r=0;r<e.length;r+=1){let i=e.charCodeAt(r);t=Math.imul(t^i,2654435761),n=Math.imul(n^i,1597334677)}return t=Math.imul(t^t>>>16,2246822507)^Math.imul(n^n>>>13,3266489909),n=Math.imul(n^n>>>16,2246822507)^Math.imul(t^t>>>13,3266489909),`${(n>>>0).toString(16)}${(t>>>0).toString(16)}:${e.length}`}let qs=e=>e instanceof Error?e.message:String(e);function Js(e){return e instanceof U?{code:e.code,index:e.index,message:e.message}:{code:`invalid-value`,index:null,message:qs(e)}}function Ys(e,t,n,r){let i=JSON.stringify(n??{}),a;try{a=e.executeAs?e.executeAs(t,i,Qn(r)):e.execute(t,i)}catch(e){let t=qs(e).indexOf(``);throw t<0?e:Error(qs(e).slice(0,t))}if(a===``)return null;try{return JSON.parse(a)}catch{return a}}let Xs=/* @__PURE__ */ new Set([`set`,`delete`,`textInsert`,`textDelete`,`textMark`,`textUnmark`,`listInsert`,`listMove`,`listRemove`,`mapSet`,`mapDelete`]);function Zs(e,t,n,r){if(r==null)return null;let i=e.tree(t,n).find(e=>e.node.equals(r));if(!i)throw new U(`not-found`,null,`a node of ${t}.${n} that the tree does not hold`);return i.object}function Qs(e,t,n,r){if(r===null)return null;let i=e.tree(t,n).find(t=>t.object===r&&e.exists(r));if(!i)throw new U(`not-found`,null,`\`${r}\` is not in ${t}.${n}`);return i.node}function $s(e,t){return new Proxy(e,{get(e,n){let r=Reflect.get(e,n,e);return typeof r==`function`?n===`create`?(n,r,i)=>{let a=e.create(n,r,i);return t.writes.push({call:`create`,type:n,fields:r,id:a}),t.created.add(a),a}:n===`treeCreate`?(n,r,i,a)=>{let o=Zs(e,n,r,i),s=e.treeCreate(n,r,i,a);return t.writes.push({call:`treeCreate`,id:n,field:r,parent:o,object:a}),t.touched.add(n),s}:n===`treeMove`?(n,r,i,a)=>{let o=Zs(e,n,r,i),s=Zs(e,n,r,a);e.treeMove(n,r,i,a),t.writes.push({call:`treeMove`,id:n,field:r,object:o,parent:s}),t.touched.add(n)}:typeof n==`string`&&Xs.has(n)?(...i)=>{let a=r.apply(e,i);return t.writes.push({call:`write`,name:n,args:i}),t.touched.add(i[0]),a}:r.bind(e):r}})}function ec(e,t){for(let n of t.writes)switch(n.call){case`create`:e.create(n.type,n.fields,{id:n.id});break;case`treeCreate`:e.treeCreate(n.id,n.field,Qs(e,n.id,n.field,n.parent),n.object);break;case`treeMove`:e.treeMove(n.id,n.field,Qs(e,n.id,n.field,n.object),Qs(e,n.id,n.field,n.parent));break;default:e[n.name].apply(e,n.args)}}var tc=/* @__PURE__ */ Object.freeze({__proto__:null,ReplicaModel:class e{constructor(e){this.open=e}scratch=null;schema=null;schemaText=null;undoSteps=[];redoSteps=[];reply(e){if(!this.schema&&this.open.modelDescribe){try{this.schema=Oi(JSON.parse(this.open.modelDescribe()))}catch(e){throw new U(`invalid-op`,null,`the schema does not parse: ${qs(e)}`)}this.forget()}return this.schema?V.ofReplica(this.catchUp()).showJson(e):e}canUndo(){return this.undoSteps.length>0}canRedo(){return this.redoSteps.length>0}dropRedo(){this.redoSteps.length&&(this.redoSteps=[],this.scratch?.clearRedo())}forkForPremises(t){if(this.reply(null),!this.schema)throw Error(`premise planning requires the object model schema`);let n=new e(t);return n.schema=this.schema,n.schemaText=this.schemaText,n}describe(e){return this.useSchema(e),{...Di(this.catchUp()),commands:JSON.parse(this.open.commands())}}async batch(e,t,n){this.useSchema(t);try{return await _i({replica:()=>this.catchUp(),projectedObjects:()=>{if(!this.open.premiseRead)throw new U(`invalid-value`,null,`this engine cannot read declared projected objects`);return JSON.parse(this.open.premiseRead(`projected`,`{}`))},transact:(e,t)=>this.step(e,t),command:(e,t,n)=>{let r=Ys(this.open,e,t,n);return this.dropRedo(),r},readOnly:()=>n},e)}catch(e){throw e instanceof U&&e.message.startsWith(Gs)&&this.forget(),e}}merge(e,t,n){this.useSchema(n);let r=this.catchUp(),i=r.version(),a=r.stateJson();try{r.mergeChanges(e,t)}catch(e){throw new U(`invalid-value`,null,`the draft does not merge: ${qs(e)}`)}if(r.version().equals(i))return yo(a,a);let o=yo(a,r.stateJson());try{this.open.importChanges(r.exportChanges(i))}catch(e){throw this.forget(),new U(`invalid-value`,null,`${Gs}: ${qs(e)}`)}return this.redoSteps=[],this.undoSteps.push({state:this.state(),step:{origin:t,writes:[],existing:[],stale:!1,merge:e.slice()}}),o}changesSince(e,t){this.useSchema(t);let n=this.catchUp();try{return{changes:n.changesSince(K.decode(e))}}catch(e){if(e instanceof M)return{changes:null,unavailable:e.message};throw e}}checkpointForPremises(){let e=this.catchUp();return{document:e.docId,version:e.version().encode(),state:e.stateJson()}}changesForPremises(e){let t=this.catchUp();if(t.docId===e.document)return t.changesSince(K.decode(e.version));let n=t.stateJson(),r=yo(e.state,n);return delete r.texts,So(_o(r,this.schema),this.schema,V.ofState(this.schema,e.state),V.ofReplica(t))}compare(e,t){this.useSchema(t);let n=this.catchUp();try{return yo(n.stateJson(),n.mergedState(e))}catch(e){throw new U(`invalid-value`,null,`the draft does not merge: ${qs(e)}`)}}undo(e){return this.travel(this.undoSteps,this.redoSteps,e,e=>e.undo())}redo(e){return this.travel(this.redoSteps,this.undoSteps,e,(e,t)=>{if(!t.stale&&e.canRedo())return e.redo();try{t.merge?e.mergeChanges(t.merge,t.origin):e.transact(t.origin,e=>ec(e,t))}catch{return!1}return e.clearRedo(),!0})}engineMoved(){if(!this.redoSteps.length||!this.scratch)return;let e=this.catchUp();for(let{step:t}of this.redoSteps)!t.stale&&t.existing.some(t=>!e.exists(t))&&(t.stale=!0)}travel(e,t,n,r){if(!e.length||!this.scratch)return!1;let i=this.catchUp(),{state:a,step:o}=e[e.length-1];if(n&&Ks(i.stateText())!==a)return!1;let s=i.version();if(!r(i,o))return e.length=0,i.clearRedo(),!1;if(e.pop(),!i.version().equals(s))try{this.open.importChanges(i.exportChanges(s))}catch(e){throw this.forget(),e}return o.stale=!1,t.push({state:this.state(),step:o}),!0}useSchema(e){if(e){let t=JSON.stringify(e);if(t!==this.schemaText){try{this.schema=Ar(e)}catch(e){throw new U(`invalid-op`,null,`the schema does not parse: ${qs(e)}`)}this.schemaText=t,this.forget()}}if(!this.schema)throw new U(`invalid-op`,null,"the kit's engine does not describe its schema: send `schema` with the request");return this.schema}forget(){this.scratch=null,this.undoSteps=[],this.redoSteps=[]}catchUp(){let e=this.open.exportChanges.bind(this.open);if(!this.scratch)this.scratch=js.load(this.schema,e(/* @__PURE__ */ new Uint8Array),Ho());else{let t=e(this.scratch.version().encode());try{t.byteLength>0&&this.scratch.importChanges(t)}catch(t){if(!(t instanceof M)||t.reason!==`wrong-document`)throw t;this.forget(),this.scratch=js.load(this.schema,e(/* @__PURE__ */ new Uint8Array),Ho())}}return this.scratch}state(){return Ks(this.catchUp().stateText())}step(e,t){let n=this.catchUp(),r=n.version(),i={writes:[],touched:/* @__PURE__ */ new Set,created:/* @__PURE__ */ new Set};if(n.transact(e,e=>{i.writes=[],i.touched.clear(),i.created.clear(),t($s(e,i))}),n.version().equals(r))return;try{this.open.importChanges(n.exportChanges(r))}catch(e){throw new U(`invalid-value`,null,`${Gs}: ${qs(e)}`)}this.redoSteps=[];let a=[...i.touched].filter(e=>!i.created.has(e));this.undoSteps.push({state:this.state(),step:{origin:e,writes:i.writes,existing:a,stale:!1}})}},modelErrorData:Js,runAs:Ys})}));export default e();