/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { bufferToString } from "@fluid-internal/client-utils";
import { assert } from "@fluidframework/core-utils/internal";
import { createSingleBlobSummary } from "@fluidframework/shared-object-base/internal";
import { noopValidator } from "../../codec/index.js";
import { applyDelta, forEachField, makeDetachedFieldIndex, mapCursorField, } from "../../core/index.js";
import { idAllocatorFromMaxId } from "../../util/index.js";
// eslint-disable-next-line import/no-internal-modules
import { chunkField, defaultChunkPolicy } from "../chunked-forest/chunkTree.js";
import { makeForestSummarizerCodec } from "./codec.js";
/**
 * The storage key for the blob in the summary containing tree data
 */
const treeBlobKey = "ForestTree";
/**
 * Provides methods for summarizing and loading a forest.
 */
export class ForestSummarizer {
    /**
     * @param encoderContext - The schema if provided here must be mutated by the caller to keep it up to date.
     */
    constructor(forest, revisionTagCodec, fieldBatchCodec, encoderContext, options = { jsonValidator: noopValidator }, idCompressor) {
        this.forest = forest;
        this.revisionTagCodec = revisionTagCodec;
        this.encoderContext = encoderContext;
        this.idCompressor = idCompressor;
        this.key = "Forest";
        this.codec = makeForestSummarizerCodec(options, fieldBatchCodec);
    }
    /**
     * Synchronous monolithic summarization of tree content.
     *
     * TODO: when perf matters, this should be replaced with a chunked async version using a binary format.
     *
     * @returns a snapshot of the forest's tree as a string.
     */
    getTreeString(stringify) {
        const rootCursor = this.forest.getCursorAboveDetachedFields();
        const fieldMap = new Map();
        // TODO: Encode all detached fields in one operation for better performance and compression
        forEachField(rootCursor, (cursor) => {
            const key = cursor.getFieldKey();
            const innerCursor = this.forest.allocateCursor("getTreeString");
            assert(this.forest.tryMoveCursorToField({ fieldKey: key, parent: undefined }, innerCursor) ===
                1 /* TreeNavigationResult.Ok */, 0x892 /* failed to navigate to field */);
            fieldMap.set(key, innerCursor);
        });
        const encoded = this.codec.encode(fieldMap, this.encoderContext);
        fieldMap.forEach((value) => value.free());
        return stringify(encoded);
    }
    getAttachSummary(stringify, fullTree, trackState, telemetryContext) {
        return createSingleBlobSummary(treeBlobKey, this.getTreeString(stringify));
    }
    async summarize(stringify, fullTree, trackState, telemetryContext) {
        return createSingleBlobSummary(treeBlobKey, this.getTreeString(stringify));
    }
    getGCData(fullGC) {
        // TODO: Properly implement garbage collection. Right now, garbage collection is performed automatically
        // by the code in SharedObject (from which SharedTreeCore extends). The `runtime.uploadBlob` API delegates
        // to the `BlobManager`, which automatically populates the summary with ISummaryAttachment entries for each
        // blob.
        return {
            gcNodes: {},
        };
    }
    async load(services, parse) {
        if (await services.contains(treeBlobKey)) {
            const treeBuffer = await services.readBlob(treeBlobKey);
            const treeBufferString = bufferToString(treeBuffer, "utf8");
            // TODO: this code is parsing data without an optional validator, this should be defined in a typebox schema as part of the
            // forest summary format.
            const fields = this.codec.decode(parse(treeBufferString), this.encoderContext);
            const allocator = idAllocatorFromMaxId();
            const fieldChanges = [];
            const build = [];
            for (const [fieldKey, field] of fields) {
                const chunked = chunkField(field, defaultChunkPolicy);
                const nodeCursors = chunked.flatMap((chunk) => mapCursorField(chunk.cursor(), (cursor) => cursor.fork()));
                const buildId = { minor: allocator.allocate(nodeCursors.length) };
                build.push({
                    id: buildId,
                    trees: nodeCursors,
                });
                fieldChanges.push([
                    fieldKey,
                    {
                        local: [{ count: nodeCursors.length, attach: buildId }],
                    },
                ]);
            }
            assert(this.forest.isEmpty, 0x797 /* forest must be empty */);
            applyDelta({ build, fields: new Map(fieldChanges) }, this.forest, makeDetachedFieldIndex("init", this.revisionTagCodec, this.idCompressor));
        }
    }
}
//# sourceMappingURL=forestSummarizer.js.map