var _a;
import { EventEmitter } from "cellx";
import { ObservableList, ObservableMap } from "cellx-collections";
import { Fn } from "@cmmn/core";
export class ObservableYMap extends EventEmitter {
    constructor(yMap) {
        super();
        this.yMap = yMap;
        this[_a] = () => {
            return this.entries();
        };
    }
    get size() {
        return this.yMap.size;
    }
    get valueEquals() {
        throw new Error("Method not implemented.");
    }
    onChange(listener, context) {
        return this.on(ObservableMap.EVENT_CHANGE, listener, context);
    }
    offChange(listener, context) {
        return this.off(ObservableMap.EVENT_CHANGE, listener, context);
    }
    clear() {
        this.yMap.clear();
        return this;
    }
    equals(that) {
        if (!(that instanceof ObservableMap) ||
            !(that instanceof ObservableYMap)) {
            return false;
        }
        if (this.size != that.size) {
            return false;
        }
        for (let [key, value] of this) {
            const thisValue = this.get(key);
            const thatValue = that.get(key);
            if (!Fn.compare(thisValue, thatValue))
                return false;
        }
        return true;
    }
    clone(deep) {
        throw new Error("Method not implemented.");
    }
    absorbFrom(that) {
        throw new Error("Method not implemented.");
    }
    toData() {
        throw new Error("Method not implemented.");
    }
    emitChange(type, key, value, prev) {
        super.emit(ObservableMap.EVENT_CHANGE, {
            subtype: type,
            key,
            prevValue: prev,
            value
        });
    }
    has(key) {
        return this.yMap.has(key);
    }
    get(key) {
        return this.yMap.get(key);
    }
    delete(key) {
        const has = this.yMap.has(key);
        this.yMap.delete(key);
        return has;
    }
    set(key, value) {
        this.yMap.set(key, value);
        return this;
    }
    forEach(cb, context) {
        for (let x of this.yMap) {
            cb.call(context, x[1], x[0], this);
        }
    }
    keys() {
        return this.yMap.keys();
    }
    values() {
        return this.yMap.values();
    }
    entries() {
        return this.yMap.entries();
    }
    toList(id) {
        const list = new ObservableList(Array.from(this.values()));
        list.onChange(e => {
            switch (e.data.subtype) {
                case "add":
                case "update":
                    this.set(id(e.data.value), e.data.value);
                    break;
                case "delete":
                    this.delete(id(e.data.value));
                    break;
            }
        });
        this.onChange(e => {
            switch (e.data.subtype) {
                case "add":
                    list.add(e.data.value);
                    break;
                case "delete":
                    list.remove(e.data.value);
                    break;
                case "update":
                    list.set(list.indexOf(e.data.oldValue), e.data.value);
                    break;
            }
        });
        return list;
    }
    subscribe() {
        this.yMap.observe((event, transaction) => {
            // if (event.transaction.local)
            //     return;
            for (let [id, change] of event.changes.keys) {
                switch (change.action) {
                    case "add":
                        this.emitChange('add', id, this.yMap.get(id), change.oldValue);
                        break;
                    case "delete":
                        this.emitChange('delete', id, null, change.oldValue);
                        break;
                    case "update":
                        this.emitChange('update', id, this.yMap.get(id), change.oldValue);
                        break;
                }
            }
        });
        this.emit({
            type: 'change',
            target: this,
            data: {}
        });
    }
}
_a = Symbol.iterator;
//# sourceMappingURL=observable-y-map.js.map