/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { strict as assert } from "node:assert";
import { createMockNodeKeyManager, nodeKeyFieldKey, } from "../../../feature-libraries/index.js";
// eslint-disable-next-line import/no-internal-modules
import { getTreeContext } from "../../../feature-libraries/flex-tree/context.js";
import { rootFieldKey, } from "../../../core/index.js";
import { forestWithContent } from "../../utils.js";
import { brand } from "../../../util/index.js";
export function getReadonlyContext(forest, schema) {
    // This will error if someone tries to call mutation methods on it
    const dummyEditor = {};
    return getTreeContext(schema, forest, dummyEditor, createMockNodeKeyManager(), brand(nodeKeyFieldKey));
}
/**
 * Creates a context and its backing forest from the provided `content`.
 *
 * For creating mutable views use {@link viewWithContent}, but prefer this when possible as it has fewer dependencies and simpler setup.
 *
 * @returns The created context.
 */
export function contextWithContentReadonly(content) {
    const forest = forestWithContent(content);
    return getReadonlyContext(forest, content.schema);
}
/**
 * Creates a cursor from the provided `context` and moves it to the provided `anchor`.
 */
export function initializeCursor(context, anchor) {
    const cursor = context.forest.allocateCursor();
    assert.equal(context.forest.tryMoveCursorToField(anchor, cursor), 1 /* TreeNavigationResult.Ok */);
    return cursor;
}
export const rootFieldAnchor = { parent: undefined, fieldKey: rootFieldKey };
/**
 * Initializes a readonly test tree, context, and cursor, and moves the cursor to the tree's root.
 *
 * @returns The initialized context and cursor.
 */
export function readonlyTreeWithContent(treeContent) {
    const context = contextWithContentReadonly(treeContent);
    const cursor = initializeCursor(context, rootFieldAnchor);
    return {
        context,
        cursor,
    };
}
//# sourceMappingURL=utils.js.map