"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.cursorForMapTreeField = exports.cursorForMapTreeNode = void 0;
const core_utils_1 = require("@fluidframework/core-utils");
const index_js_1 = require("../core/index.js");
const treeCursorUtils_js_1 = require("./treeCursorUtils.js");
/**
 * @returns An {@link ITreeCursorSynchronous} in nodes mode for a single {@link MapTree}.
 */
function cursorForMapTreeNode(root) {
    return (0, treeCursorUtils_js_1.stackTreeNodeCursor)(adapter, root);
}
exports.cursorForMapTreeNode = cursorForMapTreeNode;
/**
 * @returns an {@link ITreeCursorSynchronous} in fields mode for a MapTree field.
 */
function cursorForMapTreeField(root, detachedField = index_js_1.rootField) {
    const key = (0, index_js_1.detachedFieldAsKey)(detachedField);
    return (0, treeCursorUtils_js_1.stackTreeFieldCursor)(adapter, {
        type: index_js_1.aboveRootPlaceholder,
        fields: new Map([[key, root]]),
    }, detachedField);
}
exports.cursorForMapTreeField = cursorForMapTreeField;
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, index_js_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