"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.configuredSharedTree = exports.SharedTree = void 0;
const internal_1 = require("@fluidframework/shared-object-base/internal");
const internal_2 = require("@fluidframework/telemetry-utils/internal");
const index_js_1 = require("./shared-tree/index.js");
const sharedTreeAttributes_js_1 = require("./sharedTreeAttributes.js");
const index_js_2 = require("./util/index.js");
const index_js_3 = require("./codec/index.js");
/**
 * Creates a factory for shared tree kernels with the given options.
 * @remarks
 * Exposes {@link ITreePrivate} to allow access to internals in tests without a cast.
 * Code exposing this beyond this package will need to update to a more public type.
 */
function treeKernelFactory(options) {
    function treeFromKernelArgs(args) {
        if (args.idCompressor === undefined) {
            throw new internal_2.UsageError("IdCompressor must be enabled to use SharedTree");
        }
        const adjustedOptions = { ...options };
        // TODO: get default from runtime once something like runtime.oldestCompatibleClient exists.
        // Using default of 2.0 since that is the oldest version that supports SharedTree.
        adjustedOptions.oldestCompatibleClient ??= index_js_3.FluidClientVersion.v2_0;
        return new index_js_1.SharedTreeKernel(new index_js_2.Breakable("SharedTree"), args.sharedObject, args.serializer, args.submitLocalMessage, args.lastSequenceNumber, args.logger, args.idCompressor, adjustedOptions);
    }
    return {
        create: (args) => {
            const k = treeFromKernelArgs(args);
            return { kernel: k, view: k.view };
        },
        async loadCore(args, storage) {
            const k = treeFromKernelArgs(args);
            await k.loadCore(storage);
            return { kernel: k, view: k.view };
        },
    };
}
/**
 * SharedTree is a hierarchical data structure for collaboratively editing strongly typed JSON-like trees
 * of objects, arrays, and other data types.
 * @legacy
 * @alpha
 */
exports.SharedTree = configuredSharedTree({});
/**
 * {@link SharedTree} but allowing a non-default configuration.
 * @remarks
 * This is useful for debugging and testing to opt into extra validation or see if opting out of some optimizations fixes an issue.
 * @example
 * ```typescript
 * import {
 * 	ForestType,
 * 	TreeCompressionStrategy,
 * 	configuredSharedTree,
 * 	typeboxValidator,
 * 	// eslint-disable-next-line import/no-internal-modules
 * } from "@fluidframework/tree/internal";
 * const SharedTree = configuredSharedTree({
 * 	forest: ForestTypeReference,
 * 	jsonValidator: typeboxValidator,
 * 	treeEncodeType: TreeCompressionStrategy.Uncompressed,
 * });
 * ```
 * @privateRemarks
 * This should be legacy, but has to be internal due to limitations of API tagging preventing it from being both alpha and alpha+legacy.
 * TODO:
 * Expose Ajv validator for better error message quality somehow.
 * Maybe as part of a test utils or dev-tool package?
 * @internal
 */
function configuredSharedTree(options) {
    const sharedObjectOptions = {
        type: sharedTreeAttributes_js_1.SharedTreeFactoryType,
        attributes: sharedTreeAttributes_js_1.SharedTreeAttributes,
        telemetryContextPrefix: "fluid_sharedTree_",
        factory: treeKernelFactory(options),
    };
    return (0, internal_1.makeSharedObjectKind)(sharedObjectOptions);
}
exports.configuredSharedTree = configuredSharedTree;
//# sourceMappingURL=treeFactory.js.map