/*!
 * 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, SchemaVersion, TreeStoredSchemaRepository, } from "../core/index.js";
import { createNodeIdentifierManager, makeFieldBatchCodec, makeSchemaCodec, defaultSchemaPolicy, TreeCompressionStrategy, } from "../feature-libraries/index.js";
import { Breakable } from "../util/index.js";
import { buildConfiguredForest, defaultSharedTreeOptions, } from "./sharedTree.js";
import { createTreeCheckout } from "./treeCheckout.js";
import { SchematizingSimpleTreeView } from "./schematizingTreeView.js";
import { initialize, initializerFromChunk } from "./schematizeTree.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 breaker = new Breakable("independentView");
    const idCompressor = options.idCompressor ?? createIdCompressor();
    const mintRevisionTag = () => idCompressor.generateCompressedId();
    const revisionTagCodec = new RevisionTagCodec(idCompressor);
    const schema = new TreeStoredSchemaRepository();
    const forest = buildConfiguredForest(breaker, options.forest ?? defaultSharedTreeOptions.forest, schema, idCompressor);
    const checkout = createTreeCheckout(idCompressor, mintRevisionTag, revisionTagCodec, {
        forest,
        schema,
        breaker,
    });
    const out = new SchematizingSimpleTreeView(checkout, config, createNodeIdentifierManager(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.
 * @privateRemarks
 * TODO: Providing an API which generates a {@link ViewableTree} extended with export options from {@link ITreeAlpha} and maybe even branching APIs would likely be better that just exposing a {@link TreeViewAlpha}.
 * @alpha
 */
export function independentInitializedView(config, options, content) {
    const idCompressor = content.idCompressor;
    const fieldBatchCodec = makeFieldBatchCodec(options, 1);
    const schemaCodec = makeSchemaCodec(options, SchemaVersion.v1);
    const schema = schemaCodec.decode(content.schema);
    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 */);
    return independentInitializedViewInternal(config, options, schema, 
    // Checked above.
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    fieldCursors[0], idCompressor);
}
/**
 * {@link independentInitializedView} but using internal types instead of persisted data formats.
 */
export function independentInitializedViewInternal(config, options, schema, rootFieldCursor, idCompressor) {
    const breaker = new Breakable("independentInitializedView");
    const revisionTagCodec = new RevisionTagCodec(idCompressor);
    const mintRevisionTag = () => idCompressor.generateCompressedId();
    // To ensure the forest is in schema when constructed, start it with an empty schema and set the schema repository content later.
    const schemaRepository = new TreeStoredSchemaRepository();
    const forest = buildConfiguredForest(breaker, options.forest ?? defaultSharedTreeOptions.forest, schemaRepository, idCompressor);
    const checkout = createTreeCheckout(idCompressor, mintRevisionTag, revisionTagCodec, {
        forest,
        schema: schemaRepository,
        breaker,
    });
    initialize(checkout, schema, initializerFromChunk(checkout, () => checkout.forest.chunkField(rootFieldCursor)));
    return new SchematizingSimpleTreeView(checkout, config, createNodeIdentifierManager(idCompressor));
}
//# sourceMappingURL=independentView.js.map