import NodeId from "./NodeId.js";
import NodeTypeId, { DEFAULT_NODE_TYPE_ID } from "./NodeTypeId.js";
import WarpError from "../errors/WarpError.js";
/** Runtime-backed graph substrate node record. */
export default class NodeRecord {
    id;
    typeId;
    constructor(fields) {
        const checkedFields = requireFields(fields);
        this.id = requireNodeId(checkedFields.id);
        this.typeId = requireNodeTypeId(checkedFields.typeId);
        Object.freeze(this);
    }
    /** Builds a transitional node record from a legacy string node id. */
    static fromLegacyNodeId(nodeId) {
        return new NodeRecord({
            id: new NodeId(nodeId),
            typeId: new NodeTypeId(DEFAULT_NODE_TYPE_ID),
        });
    }
    /** Compares node records by graph identity and type. */
    equals(other) {
        return this.id.equals(other.id) && this.typeId.equals(other.typeId);
    }
}
/** Validates the node-record constructor envelope. */
function requireFields(fields) {
    if (fields === null || fields === undefined) {
        throw new WarpError('NodeRecord fields must be provided', 'E_VALIDATION');
    }
    return fields;
}
/** Requires a runtime-backed node id. */
function requireNodeId(value) {
    if (!(value instanceof NodeId)) {
        throw new WarpError('NodeRecord id must be a NodeId', 'E_VALIDATION');
    }
    return value;
}
/** Requires a runtime-backed node type id. */
function requireNodeTypeId(value) {
    if (!(value instanceof NodeTypeId)) {
        throw new WarpError('NodeRecord typeId must be a NodeTypeId', 'E_VALIDATION');
    }
    return value;
}
