import { cell } from "@cmmn/cell";
import { CLazyMap, CRuntime, CValueList, CValueMap, CValueSet } from "@collabs/collabs";
import { IndexedDBDocStore } from "@collabs/indexeddb";
import { SyncObject } from "./sync-object.js";
import { SyncSet } from "./syncSet.js";
import { SyncArray } from "./syncArray.js";
export class SyncStore {
    constructor(name, replicaId = undefined) {
        this.name = name;
        this.replicaId = replicaId;
        // private doc = new Doc({
        //     autoLoad: true
        // });
        // public adapter = new DocAdapter(this.doc, new Awareness(this.doc));
        this.doc = new CRuntime({
            debugReplicaID: this.replicaId
        });
        this.objects = this.doc.registerCollab("objects", init => new CLazyMap(init, init => new CValueMap(init)));
        this.arrays = this.doc.registerCollab("lists", init => new CLazyMap(init, init => new CValueList(init)));
        this.sets = this.doc.registerCollab("sets", init => new CLazyMap(init, init => new CValueSet(init)));
        this.store = new IndexedDBDocStore({
            dbName: this.name
        });
        this.IsLoaded = new Promise(resolve => this.store.on('Load', resolve));
        this.IsSynced = false;
        this.addProvider();
    }
    addProvider() {
        this.store.subscribe(this.doc, this.name);
    }
    addSync(sync) {
        sync.listen(this.doc);
    }
    getObjectCell(name) {
        const map = this.objects.get(name);
        return new SyncObject(this.doc, map);
    }
    getArray(name) {
        const array = this.arrays.get(name);
        return new SyncArray(this.doc, array);
    }
    getSet(name) {
        const array = this.sets.get(name);
        return new SyncSet(this.doc, array);
    }
    dispose() {
        this.sets.finalize();
        this.objects.finalize();
        this.arrays.finalize();
    }
}
__decorate([
    cell,
    __metadata("design:type", Object)
], SyncStore.prototype, "IsSynced", void 0);
