/**
 * NodeAdd — adds a node to the graph with a causal dot.
 */
import PatchError from "../../errors/PatchError.js";
import { Dot } from "../../crdt/Dot.js";
import Op from "./Op.js";
import { assertNonEmptyString, assertNoReservedBytes } from "./validate.js";
import { OP_SCOPE_BOTH } from "./OpScope.js";
import ReceiptBuilder from "../../services/ReceiptBuilder.js";
export default class NodeAdd extends Op {
    receiptName = 'NodeAdd';
    node;
    dot;
    constructor(node, dot) {
        super('NodeAdd', OP_SCOPE_BOTH);
        assertNonEmptyString(node, 'NodeAdd', 'node');
        assertNoReservedBytes(node, 'NodeAdd', 'node');
        if (!(dot instanceof Dot)) {
            throw new PatchError('NodeAdd requires dot to be a Dot instance');
        }
        this.node = node;
        this.dot = dot;
        Object.freeze(this);
    }
    validate() { }
    mutate(state) {
        state.nodeAlive.add(this.node, this.dot);
    }
    outcome(state) {
        return ReceiptBuilder.nodeAddOutcome(state.nodeAlive, { node: this.node, dot: this.dot });
    }
    snapshot(state) {
        return { nodeWasAlive: state.nodeAlive.contains(this.node) };
    }
    accumulate(diff, state, before) {
        if (before.nodeWasAlive !== true && state.nodeAlive.contains(this.node)) {
            diff.nodesAdded.push(this.node);
        }
    }
}
