/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils/internal";
import { createIdCompressor, } from "@fluidframework/id-compressor/internal";
import { RevisionTagCodec, TreeStoredSchemaRepository, initializeForest, mapCursorField, } from "../core/index.js";
import { createNodeKeyManager, makeFieldBatchCodec, makeSchemaCodec, defaultSchemaPolicy, chunkTree, defaultChunkPolicy, TreeCompressionStrategy, } from "../feature-libraries/index.js";
import { buildConfiguredForest, defaultSharedTreeOptions, } from "./sharedTree.js";
import { createTreeCheckout } from "./treeCheckout.js";
import { SchematizingSimpleTreeView } from "./schematizingTreeView.js";
/**
 * Create an uninitialized {@link TreeView} that is not tied to any {@link ITree} instance.
 *
 * @remarks
 * Such a view can never experience collaboration or be persisted to to a Fluid Container.
 *
 * This can be useful for testing, as well as use-cases like working on local files instead of documents stored in some Fluid service.
 * @alpha
 */
export function independentView(config, options) {
    const idCompressor = options.idCompressor ?? createIdCompressor();
    const mintRevisionTag = () => idCompressor.generateCompressedId();
    const revisionTagCodec = new RevisionTagCodec(idCompressor);
    const schema = new TreeStoredSchemaRepository();
    const forest = buildConfiguredForest(options.forest ?? defaultSharedTreeOptions.forest, schema, idCompressor);
    const checkout = createTreeCheckout(idCompressor, mintRevisionTag, revisionTagCodec, {
        forest,
        schema,
    });
    const out = new SchematizingSimpleTreeView(checkout, config, createNodeKeyManager(idCompressor));
    return out;
}
/**
 * Create an initialized {@link TreeView} that is not tied to any {@link ITree} instance.
 *
 * @remarks
 * Such a view can never experience collaboration or be persisted to to a Fluid Container.
 *
 * This can be useful for testing, as well as use-cases like working on local files instead of documents stored in some Fluid service.
 * @alpha
 */
export function independentInitializedView(config, options, content) {
    const idCompressor = content.idCompressor;
    const mintRevisionTag = () => idCompressor.generateCompressedId();
    const revisionTagCodec = new RevisionTagCodec(idCompressor);
    const fieldBatchCodec = makeFieldBatchCodec(options, 1);
    const schemaCodec = makeSchemaCodec(options);
    const schema = new TreeStoredSchemaRepository(schemaCodec.decode(content.schema));
    const forest = buildConfiguredForest(options.forest ?? defaultSharedTreeOptions.forest, schema, idCompressor);
    const context = {
        encodeType: TreeCompressionStrategy.Compressed,
        idCompressor,
        originatorId: idCompressor.localSessionId, // Is this right? If so, why is is needed?
        schema: { schema, policy: defaultSchemaPolicy },
    };
    const fieldCursors = fieldBatchCodec.decode(content.tree, context);
    assert(fieldCursors.length === 1, 0xa5b /* must have exactly 1 field in batch */);
    // Checked above.
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    const cursors = fieldCursorToNodesCursors(fieldCursors[0]);
    initializeForest(forest, cursors, revisionTagCodec, idCompressor, false);
    const checkout = createTreeCheckout(idCompressor, mintRevisionTag, revisionTagCodec, {
        forest,
        schema,
    });
    const out = new SchematizingSimpleTreeView(checkout, config, createNodeKeyManager(idCompressor));
    return out;
}
function fieldCursorToNodesCursors(fieldCursor) {
    return mapCursorField(fieldCursor, copyNodeCursor);
}
/**
 * TODO: avoid needing this, or optimize it.
 */
function copyNodeCursor(cursor) {
    const copy = chunkTree(cursor, {
        policy: defaultChunkPolicy,
        idCompressor: undefined,
    }).cursor();
    copy.enterNode(0);
    return copy;
}
//# sourceMappingURL=independentView.js.map