"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapTreeFromCursor = exports.singleMapTreeCursor = void 0;
const core_utils_1 = require("@fluidframework/core-utils");
const core_1 = require("../core");
const treeCursorUtils_1 = require("./treeCursorUtils");
/**
 * @returns an ITreeCursorSynchronous for a single MapTree.
 */
function singleMapTreeCursor(root) {
    return (0, treeCursorUtils_1.singleStackTreeCursor)(root, adapter);
}
exports.singleMapTreeCursor = singleMapTreeCursor;
const adapter = {
    value: (node) => node.value,
    type: (node) => node.type,
    keysFromNode: (node) => [...node.fields.keys()],
    getFieldFromNode: (node, key) => node.fields.get(key) ?? [],
};
/**
 * Extract a MapTree from the contents of the given ITreeCursor's current node.
 */
function mapTreeFromCursor(cursor) {
    (0, core_utils_1.assert)(cursor.mode === 0 /* CursorLocationType.Nodes */, 0x3b7 /* must start at node */);
    const fields = new Map();
    for (let inField = cursor.firstField(); inField; inField = cursor.nextField()) {
        const field = (0, core_1.mapCursorField)(cursor, mapTreeFromCursor);
        fields.set(cursor.getFieldKey(), field);
    }
    const node = {
        type: cursor.type,
        value: cursor.value,
        fields,
    };
    return node;
}
exports.mapTreeFromCursor = mapTreeFromCursor;
//# sourceMappingURL=mapTreeCursor.js.map