"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.independentInitializedViewInternal = exports.independentInitializedView = exports.independentView = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/id-compressor/internal");
const index_js_1 = require("../core/index.js");
const index_js_2 = require("../feature-libraries/index.js");
const index_js_3 = require("../util/index.js");
const sharedTree_js_1 = require("./sharedTree.js");
const treeCheckout_js_1 = require("./treeCheckout.js");
const schematizingTreeView_js_1 = require("./schematizingTreeView.js");
const schematizeTree_js_1 = require("./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
 */
function independentView(config, options) {
    const breaker = new index_js_3.Breakable("independentView");
    const idCompressor = options.idCompressor ?? (0, internal_2.createIdCompressor)();
    const mintRevisionTag = () => idCompressor.generateCompressedId();
    const revisionTagCodec = new index_js_1.RevisionTagCodec(idCompressor);
    const schema = new index_js_1.TreeStoredSchemaRepository();
    const forest = (0, sharedTree_js_1.buildConfiguredForest)(breaker, options.forest ?? sharedTree_js_1.defaultSharedTreeOptions.forest, schema, idCompressor);
    const checkout = (0, treeCheckout_js_1.createTreeCheckout)(idCompressor, mintRevisionTag, revisionTagCodec, {
        forest,
        schema,
        breaker,
    });
    const out = new schematizingTreeView_js_1.SchematizingSimpleTreeView(checkout, config, (0, index_js_2.createNodeIdentifierManager)(idCompressor));
    return out;
}
exports.independentView = independentView;
/**
 * 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
 */
function independentInitializedView(config, options, content) {
    const idCompressor = content.idCompressor;
    const fieldBatchCodec = (0, index_js_2.makeFieldBatchCodec)(options, 1);
    const schemaCodec = (0, index_js_2.makeSchemaCodec)(options, index_js_1.SchemaVersion.v1);
    const schema = schemaCodec.decode(content.schema);
    const context = {
        encodeType: index_js_2.TreeCompressionStrategy.Compressed,
        idCompressor,
        originatorId: idCompressor.localSessionId, // Is this right? If so, why is is needed?
        schema: { schema, policy: index_js_2.defaultSchemaPolicy },
    };
    const fieldCursors = fieldBatchCodec.decode(content.tree, context);
    (0, internal_1.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);
}
exports.independentInitializedView = independentInitializedView;
/**
 * {@link independentInitializedView} but using internal types instead of persisted data formats.
 */
function independentInitializedViewInternal(config, options, schema, rootFieldCursor, idCompressor) {
    const breaker = new index_js_3.Breakable("independentInitializedView");
    const revisionTagCodec = new index_js_1.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 index_js_1.TreeStoredSchemaRepository();
    const forest = (0, sharedTree_js_1.buildConfiguredForest)(breaker, options.forest ?? sharedTree_js_1.defaultSharedTreeOptions.forest, schemaRepository, idCompressor);
    const checkout = (0, treeCheckout_js_1.createTreeCheckout)(idCompressor, mintRevisionTag, revisionTagCodec, {
        forest,
        schema: schemaRepository,
        breaker,
    });
    (0, schematizeTree_js_1.initialize)(checkout, schema, (0, schematizeTree_js_1.initializerFromChunk)(checkout, () => checkout.forest.chunkField(rootFieldCursor)));
    return new schematizingTreeView_js_1.SchematizingSimpleTreeView(checkout, config, (0, index_js_2.createNodeIdentifierManager)(idCompressor));
}
exports.independentInitializedViewInternal = independentInitializedViewInternal;
//# sourceMappingURL=independentView.js.map