import { ExtensionId, ExtensionName } from '../constants';
import { NodeApi } from '../../json-crdt/model/api/nodes';
import { ExtNode } from '../../json-crdt/extensions/ExtNode';
import { Extension } from '../../json-crdt/extensions/Extension';
import { NodeBuilder, s } from '../../json-crdt';
const MNEMONIC = ExtensionName[ExtensionId.cnt];
class CntNode extends ExtNode {
    extId = ExtensionId.cnt;
    name() {
        return MNEMONIC;
    }
    view() {
        const obj = this.data.view();
        let sum = 0;
        for (const key in obj)
            sum += Number(obj[key]);
        return sum;
    }
}
class CntApi extends NodeApi {
    inc(increment) {
        const { api, node } = this;
        const sid = api.model.clock.sid;
        const sidStr = sid.toString(36);
        const value = Number(node.data.get(sidStr)?.view() ?? 0);
        const newValue = value + increment;
        const obj = api.wrap(node.data);
        obj.set({
            [sidStr]: newValue,
        });
        return this;
    }
}
const create = (value, sid = 0) => new NodeBuilder((builder) => {
    if (!sid)
        sid = builder.clock.sid;
    const schema = value === undefined
        ? s.map({})
        : s.map({ [sid.toString(36)]: s.con(value ?? 0) });
    return schema.build(builder);
});
export const cnt = new Extension(ExtensionId.cnt, MNEMONIC, CntNode, CntApi, create);
//# sourceMappingURL=index.js.map