"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SharedTreeFactory = exports.SharedTree = exports.identifierKeySymbol = exports.identifierKey = void 0;
const core_1 = require("../core");
const shared_tree_core_1 = require("../shared-tree-core");
const feature_libraries_1 = require("../feature-libraries");
const events_1 = require("../events");
const util_1 = require("../util");
const sharedTreeView_1 = require("./sharedTreeView");
/**
 * The key for the special identifier field, which allows nodes to be given identifiers that can be used
 * to find the nodes via the identifier index
 * @alpha
 */
exports.identifierKey = (0, util_1.brand)("identifier");
/**
 * The global field key symbol that corresponds to {@link identifierKey}
 * @alpha
 */
exports.identifierKeySymbol = (0, core_1.symbolFromKey)(exports.identifierKey);
/**
 * Shared tree, configured with a good set of indexes and field kinds which will maintain compatibility over time.
 * TODO: node identifier index.
 *
 * TODO: detail compatibility requirements.
 */
class SharedTree extends shared_tree_core_1.SharedTreeCore {
    constructor(id, runtime, attributes, telemetryContextPrefix) {
        const schema = new core_1.InMemoryStoredSchemaRepository(feature_libraries_1.defaultSchemaPolicy);
        const forest = (0, feature_libraries_1.buildForest)(schema, new core_1.AnchorSet());
        const schemaSummarizer = new feature_libraries_1.SchemaSummarizer(runtime, schema);
        const forestSummarizer = new feature_libraries_1.ForestSummarizer(runtime, forest);
        const repairProvider = new feature_libraries_1.ForestRepairDataStoreProvider(forest, schema);
        super([schemaSummarizer, forestSummarizer], feature_libraries_1.defaultChangeFamily, forest.anchors, repairProvider, id, runtime, attributes, telemetryContextPrefix);
        this.schema = new feature_libraries_1.SchemaEditor(schema, (op) => this.submitLocalMessage(op));
        this.identifierIndex = new feature_libraries_1.IdentifierIndex(exports.identifierKey);
        this.view = (0, sharedTreeView_1.createSharedTreeView)({
            branch: this.getLocalBranch(),
            schema,
            forest,
            repairProvider,
            identifierIndex: this.identifierIndex,
        });
        this.events = (0, events_1.createEmitter)();
        this.getLocalBranch().on("change", () => this.finishBatch());
    }
    get rootEvents() {
        return this.view.rootEvents;
    }
    get storedSchema() {
        return this.schema;
    }
    get forest() {
        return this.view.forest;
    }
    get identifiedNodes() {
        return this.view.identifiedNodes;
    }
    get root() {
        return this.view.root;
    }
    set root(data) {
        this.view.root = data;
    }
    get context() {
        return this.view.context;
    }
    locate(anchor) {
        return this.view.locate(anchor);
    }
    schematize(config) {
        return this.view.schematize(config);
    }
    get transaction() {
        return this.view.transaction;
    }
    fork() {
        return this.view.fork();
    }
    merge(fork) {
        this.view.merge(fork);
    }
    rebase(fork) {
        fork.rebaseOnto(this.view);
    }
    getLocalBranch() {
        return super.getLocalBranch();
    }
    /**
     * TODO: Shared tree needs a pattern for handling non-changeset operations.
     * Whatever pattern is adopted should probably also handle multiple versions of changeset operations.
     * A single top level enum listing all ops (including their different versions),
     * with at least fine grained enough detail to direct them to the correct subsystem would be a good approach.
     * The current use-case (with an op applying to a specific index) is a temporary hack,
     * and its not clear how it would fit into such a system if implemented in shared-tree-core:
     * maybe op dispatch is part of the shared-tree level?
     */
    processCore(message, local, localOpMetadata) {
        if (!this.schema.tryHandleOp(message)) {
            super.processCore(message, local, localOpMetadata);
        }
    }
    reSubmitCore(content, localOpMetadata) {
        if (!this.schema.tryResubmitOp(content)) {
            super.reSubmitCore(content, localOpMetadata);
        }
    }
    async loadCore(services) {
        await super.loadCore(services);
        this.finishBatch();
    }
    /** Finish a batch (see {@link ViewEvents}) */
    finishBatch() {
        this.identifierIndex.scanIdentifiers(this.context);
        this.events.emit("afterBatch");
    }
}
exports.SharedTree = SharedTree;
/**
 * A channel factory that creates {@link ISharedTree}s.
 * @alpha
 */
class SharedTreeFactory {
    constructor() {
        this.type = "SharedTree";
        this.attributes = {
            type: this.type,
            snapshotFormatVersion: "0.0.0",
            packageVersion: "0.0.0",
        };
    }
    async load(runtime, id, services, channelAttributes) {
        const tree = new SharedTree(id, runtime, channelAttributes, "SharedTree");
        await tree.load(services);
        return tree;
    }
    create(runtime, id) {
        const tree = new SharedTree(id, runtime, this.attributes, "SharedTree");
        tree.initializeLocal();
        return tree;
    }
}
exports.SharedTreeFactory = SharedTreeFactory;
//# sourceMappingURL=sharedTree.js.map