/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/loro-crdt@1.11.1/nodejs/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";var loroWasm=require("./loro_wasm");class Loro extends loroWasm.LoroDoc{}const CONTAINER_TYPES=["Map","Text","List","Tree","MovableList","Counter"];function isContainerId(e){return e.startsWith("cid:")}function isContainer(e){if("object"!=typeof e||null==e)return!1;const t=Object.getPrototypeOf(e);return null!=t&&"object"==typeof t&&"function"==typeof t.kind&&CONTAINER_TYPES.includes(e.kind())}function getType(e){return isContainer(e)?e.kind():"Json"}function newContainerID(e,t){return`cid:${e.counter}@${e.peer}:${t}`}function newRootContainerID(e,t){return`cid:root-${e}:${t}`}class Awareness{constructor(e,t=3e4){this.listeners=new Set,this.inner=new loroWasm.AwarenessWasm(e,t),this.peer=e,this.timeout=t}apply(e,t="remote"){const{updated:r,added:n}=this.inner.apply(e);this.listeners.forEach((e=>{e({updated:r,added:n,removed:[]},t)})),this.startTimerIfNotEmpty()}setLocalState(e){const t=null==this.inner.getState(this.peer);this.inner.setLocalState(e),t?this.listeners.forEach((e=>{e({updated:[],added:[this.inner.peer()],removed:[]},"local")})):this.listeners.forEach((e=>{e({updated:[this.inner.peer()],added:[],removed:[]},"local")})),this.startTimerIfNotEmpty()}getLocalState(){return this.inner.getState(this.peer)}getAllStates(){return this.inner.getAllStates()}encode(e){return this.inner.encode(e)}encodeAll(){return this.inner.encodeAll()}addListener(e){this.listeners.add(e)}removeListener(e){this.listeners.delete(e)}peers(){return this.inner.peers()}destroy(){clearInterval(this.timer),this.listeners.clear()}startTimerIfNotEmpty(){this.inner.isEmpty()||null!=this.timer||(this.timer=setInterval((()=>{const e=this.inner.removeOutdated();e.length>0&&this.listeners.forEach((t=>{t({updated:[],added:[],removed:e},"timeout")})),this.inner.isEmpty()&&(clearInterval(this.timer),this.timer=void 0)}),this.timeout/2))}}class EphemeralStore{constructor(e=3e4){this.inner=new loroWasm.EphemeralStoreWasm(e),this.timeout=e}apply(e){this.inner.apply(e),this.startTimerIfNotEmpty()}set(e,t){this.inner.set(e,t),this.startTimerIfNotEmpty()}delete(e){this.inner.delete(e)}get(e){return this.inner.get(e)}getAllStates(){return this.inner.getAllStates()}encode(e){return this.inner.encode(e)}encodeAll(){return this.inner.encodeAll()}keys(){return this.inner.keys()}destroy(){clearInterval(this.timer)}subscribe(e){return this.inner.subscribe(e)}subscribeLocalUpdates(e){return this.inner.subscribeLocalUpdates(e)}startTimerIfNotEmpty(){this.inner.isEmpty()||null!=this.timer||(this.timer=setInterval((()=>{this.inner.removeOutdated(),this.inner.isEmpty()&&(clearInterval(this.timer),this.timer=void 0)}),this.timeout/2))}}function idStrToId(e){const[t,r]=e.split("@");return{counter:parseInt(t),peer:r}}loroWasm.LoroDoc.prototype.toJsonWithReplacer=function(e){const t=new Set,r=this,n=(n,i)=>{if("string"==typeof i&&isContainerId(i)&&!t.has(i)){t.add(i);const s=r.getContainerById(i);if(null==s)throw new Error(`ContainerID not found: ${i}`);const a=e(n,s);if(a===s){const e=s.getShallowValue();return"object"==typeof e?o(e):e}if(isContainer(a))throw new Error("Using new container is not allowed in toJsonWithReplacer");return"object"==typeof a&&null!=a?o(a):a}if("object"==typeof i&&null!=i)return o(i);const s=e(n,i);if(isContainer(s))throw new Error("Using new container is not allowed in toJsonWithReplacer");return s},o=e=>{if(Array.isArray(e))return e.map(((e,t)=>n(t,e))).filter((e=>void 0!==e));const t={};for(const[r,o]of Object.entries(e)){const e=n(r,o);void 0!==e&&(t[r]=e)}return t},i=r.getShallowValue();return o(i)};const CALL_PENDING_EVENTS_WRAPPED=Symbol("loro.callPendingEventsWrapped");function decorateMethod(e,t){const r=Object.getOwnPropertyDescriptor(e,t);if(!r||"function"!=typeof r.value)return;const n=r.value;if(n[CALL_PENDING_EVENTS_WRAPPED])return;const o=function(...e){let t;try{return t=n.apply(this,e),t}finally{t&&"function"==typeof t.then?t.finally((()=>{loroWasm.callPendingEvents()})):loroWasm.callPendingEvents()}};o[CALL_PENDING_EVENTS_WRAPPED]=!0,Object.defineProperty(e,t,{...r,value:o})}function decorateMethods(e,t){for(const r of t)decorateMethod(e,r)}decorateMethods(loroWasm.LoroDoc.prototype,["setDetachedEditing","attach","detach","fork","forkAt","checkoutToLatest","checkout","commit","getCursorPos","revertTo","export","exportJsonUpdates","exportJsonInIdSpan","importJsonUpdates","import","importUpdateBatch","importBatch","travelChangeAncestors","getChangedContainersIn","diff","applyDiff","setPeerId"]),decorateMethods(loroWasm.EphemeralStoreWasm.prototype,["set","delete","apply","removeOutdated"]),decorateMethods(loroWasm.UndoManager.prototype,["undo","redo"]),exports.Awareness=Awareness,exports.EphemeralStore=EphemeralStore,exports.Loro=Loro,exports.getType=getType,exports.idStrToId=idStrToId,exports.isContainer=isContainer,exports.isContainerId=isContainerId,exports.newContainerID=newContainerID,exports.newRootContainerID=newRootContainerID,Object.keys(loroWasm).forEach((function(e){"default"===e||Object.prototype.hasOwnProperty.call(exports,e)||Object.defineProperty(exports,e,{enumerable:!0,get:function(){return loroWasm[e]}})}));
//# sourceMappingURL=/sm/dce74cfe30a9147149c8e02c01f0e10af4e81f18d3f4823d6b4eca04b972808c.map