/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { aboveRootPlaceholder, detachedFieldAsKey, mapCursorField, rootField, rootFieldKey, } from "../core/index.js";
import { stackTreeFieldCursor, stackTreeNodeCursor, } from "./treeCursorUtils.js";
{
}
/**
 * Returns an {@link ITreeCursorSynchronous} in nodes mode for a single {@link MapTree}.
 */
export function cursorForMapTreeNode(root) {
    // There doesn't seem to be a clean way to get TypeScript to type check this without casting
    // without declaring the adapter inside this generic function and needlessly recreating it on every call.
    const adapterTyped = adapter;
    return stackTreeNodeCursor(adapterTyped, root);
}
/**
 * Creates an {@link ExclusiveMapTree} with a single field and no value.
 * @remarks
 * This handles ensuring the field is omitted if empty, and defaults to making the node above the root.
 */
export function mapTreeWithField(children, key = rootFieldKey, type = aboveRootPlaceholder) {
    return {
        type,
        fields: mapTreeFieldsWithField(children, key),
    };
}
/**
 * Creates a Map suitable for use as {@link MapTree.fields} with a single field.
 * @remarks
 * This handles ensuring the field is omitted if empty.
 */
export function mapTreeFieldsWithField(children, key) {
    return new Map(children.length === 0 ? [] : [[key, children]]);
}
/**
 * Returns an {@link ITreeCursorSynchronous} in fields mode for a MapTree field.
 */
export function cursorForMapTreeField(root, detachedField = rootField) {
    const key = detachedFieldAsKey(detachedField);
    const adapterTyped = adapter;
    const dummyRoot = {
        type: aboveRootPlaceholder,
        fields: mapTreeFieldsWithField(root, key),
    };
    return stackTreeFieldCursor(adapterTyped, dummyRoot, detachedField);
}
const adapter = {
    value: (node) => node.value,
    type: (node) => node.type,
    keysFromNode: (node) => Array.from(node.fields, ([key, field]) => key),
    getFieldFromNode: (node, key) => {
        const field = node.fields.get(key);
        if (field === undefined) {
            return [];
        }
        if (Array.isArray(field)) {
            return field;
        }
        // TODO: this copy could be avoided by using Array.at instead of indexing in `Field`, but that requires ES2022.
        return [...field];
    },
};
/**
 * Extract a MapTree from the contents of the given ITreeCursor's current node.
 */
export function mapTreeFromCursor(cursor) {
    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 = mapCursorField(cursor, mapTreeFromCursor);
        fields.set(cursor.getFieldKey(), field);
    }
    const node = {
        type: cursor.type,
        value: cursor.value,
        fields,
    };
    return node;
}
/**
 * Extract an array of MapTrees (a field) from the contents of the given ITreeCursor's current field.
 */
export function mapTreeFieldFromCursor(cursor) {
    assert(cursor.mode === 1 /* CursorLocationType.Fields */, 0xa03 /* must start at field */);
    return mapCursorField(cursor, mapTreeFromCursor);
}
//# sourceMappingURL=mapTreeCursor.js.map