"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
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 sharedTree_js_1 = require("./sharedTree.js");
const treeCheckout_js_1 = require("./treeCheckout.js");
const schematizingTreeView_js_1 = require("./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
 */
function independentView(config, options) {
    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)(options.forest ?? sharedTree_js_1.defaultSharedTreeOptions.forest, schema, idCompressor);
    const checkout = (0, treeCheckout_js_1.createTreeCheckout)(idCompressor, mintRevisionTag, revisionTagCodec, {
        forest,
        schema,
    });
    const out = new schematizingTreeView_js_1.SchematizingSimpleTreeView(checkout, config, (0, index_js_2.createNodeKeyManager)(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.
 * @alpha
 */
function independentInitializedView(config, options, content) {
    const idCompressor = content.idCompressor;
    const mintRevisionTag = () => idCompressor.generateCompressedId();
    const revisionTagCodec = new index_js_1.RevisionTagCodec(idCompressor);
    const fieldBatchCodec = (0, index_js_2.makeFieldBatchCodec)(options, 1);
    const schemaCodec = (0, index_js_2.makeSchemaCodec)(options);
    const schema = new index_js_1.TreeStoredSchemaRepository(schemaCodec.decode(content.schema));
    const forest = (0, sharedTree_js_1.buildConfiguredForest)(options.forest ?? sharedTree_js_1.defaultSharedTreeOptions.forest, schema, idCompressor);
    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 */);
    // Checked above.
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    const cursors = fieldCursorToNodesCursors(fieldCursors[0]);
    (0, index_js_1.initializeForest)(forest, cursors, revisionTagCodec, idCompressor, false);
    const checkout = (0, treeCheckout_js_1.createTreeCheckout)(idCompressor, mintRevisionTag, revisionTagCodec, {
        forest,
        schema,
    });
    const out = new schematizingTreeView_js_1.SchematizingSimpleTreeView(checkout, config, (0, index_js_2.createNodeKeyManager)(idCompressor));
    return out;
}
exports.independentInitializedView = independentInitializedView;
function fieldCursorToNodesCursors(fieldCursor) {
    return (0, index_js_1.mapCursorField)(fieldCursor, copyNodeCursor);
}
/**
 * TODO: avoid needing this, or optimize it.
 */
function copyNodeCursor(cursor) {
    const copy = (0, index_js_2.chunkTree)(cursor, {
        policy: index_js_2.defaultChunkPolicy,
        idCompressor: undefined,
    }).cursor();
    copy.enterNode(0);
    return copy;
}
//# sourceMappingURL=independentView.js.map