/*!
 * 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, initializeForest, } 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";
/**
 * 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.
 * @alpha
 */
export function independentInitializedView(config, options, content) {
    const breaker = new Breakable("independentInitializedView");
    const idCompressor = content.idCompressor;
    const mintRevisionTag = () => idCompressor.generateCompressedId();
    const revisionTagCodec = new RevisionTagCodec(idCompressor);
    const fieldBatchCodec = makeFieldBatchCodec(options, 1);
    const schemaCodec = makeSchemaCodec(options, SchemaVersion.v1);
    const schema = new TreeStoredSchemaRepository(schemaCodec.decode(content.schema));
    const forest = buildConfiguredForest(breaker, 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
    initializeForest(forest, fieldCursors[0], revisionTagCodec, idCompressor, false);
    const checkout = createTreeCheckout(idCompressor, mintRevisionTag, revisionTagCodec, {
        forest,
        schema,
        breaker,
    });
    const out = new SchematizingSimpleTreeView(checkout, config, createNodeIdentifierManager(idCompressor));
    return out;
}
//# sourceMappingURL=independentView.js.map