/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils";
import { mapCursorField } from "../core";
import { singleStackTreeCursor } from "./treeCursorUtils";
/**
 * @returns an ITreeCursorSynchronous for a single MapTree.
 */
export function singleMapTreeCursor(root) {
    return singleStackTreeCursor(root, adapter);
}
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.
 */
export function mapTreeFromCursor(cursor) {
    assert(cursor.mode === 0 /* Nodes */, 0x3b7 /* must start at node */);
    const fields = new Map();
    for (let inField = cursor.firstField(); inField; inField = cursor.nextField()) {
        const field = mapCursorField(cursor, mapTreeFromCursor);
        fields.set(cursor.getFieldKey(), field);
    }
    const node = {
        type: cursor.type,
        value: cursor.value,
        fields,
    };
    return node;
}
//# sourceMappingURL=mapTreeCursor.js.map