/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { assert } from "@fluidframework/core-utils";
import { AnchorSet, InMemoryStoredSchemaRepository, assertIsRevisionTag, UndoRedoManager, schemaDataIsEmpty, applyDelta, } from "../core";
import { createEmitter } from "../events";
import { NodeKeyIndex, buildForest, DefaultChangeFamily, getEditableTreeContext, ForestRepairDataStoreProvider, createNodeKeyManager, ForestRepairDataStore, nodeKeyFieldKey, getTreeContext, } from "../feature-libraries";
import { SharedTreeBranch } from "../shared-tree-core";
import { TransactionResult, brand } from "../util";
import { noopValidator } from "../codec";
import { initializeContent, schematize, } from "./schematizedTree";
/**
 * Creates a {@link SharedTreeView}.
 * @param args - an object containing optional components that will be used to build the view.
 * Any components not provided will be created by default.
 * @remarks This does not create a {@link SharedTree}, but rather a view with the minimal state
 * and functionality required to implement {@link ISharedTreeView}.
 */
export function createSharedTreeView(args) {
    const schema = args?.schema ?? new InMemoryStoredSchemaRepository();
    const forest = args?.forest ?? buildForest();
    const changeFamily = args?.changeFamily ?? new DefaultChangeFamily({ jsonValidator: noopValidator });
    const repairDataStoreProvider = args?.repairProvider ??
        new ForestRepairDataStoreProvider(forest, schema, (change) => changeFamily.intoDelta(change));
    const undoRedoManager = UndoRedoManager.create(changeFamily);
    const branch = args?.branch ??
        new SharedTreeBranch({
            change: changeFamily.rebaser.compose([]),
            revision: assertIsRevisionTag("00000000-0000-4000-8000-000000000000"),
        }, changeFamily, repairDataStoreProvider, undoRedoManager);
    const nodeKeyManager = args?.nodeKeyManager ?? createNodeKeyManager();
    const context = getEditableTreeContext(forest, schema, branch.editor, nodeKeyManager, brand(nodeKeyFieldKey));
    const nodeKeyIndex = args?.nodeKeyIndex ?? new NodeKeyIndex(brand(nodeKeyFieldKey));
    const events = args?.events ?? createEmitter();
    const transaction = new Transaction(branch, changeFamily, forest);
    return new SharedTreeView(transaction, branch, changeFamily, schema, forest, context, nodeKeyManager, nodeKeyIndex, events);
}
class Transaction {
    constructor(branch, changeFamily, forest) {
        this.branch = branch;
        this.changeFamily = changeFamily;
        this.forest = forest;
    }
    start() {
        this.branch.startTransaction(new ForestRepairDataStore(this.forest, (change) => this.changeFamily.intoDelta(change)));
        this.branch.editor.enterTransaction();
    }
    commit() {
        this.branch.commitTransaction();
        this.branch.editor.exitTransaction();
        return TransactionResult.Commit;
    }
    abort() {
        this.branch.abortTransaction();
        this.branch.editor.exitTransaction();
        return TransactionResult.Abort;
    }
    inProgress() {
        return this.branch.isTransacting();
    }
}
/**
 * An implementation of {@link ISharedTreeBranchView}.
 */
export class SharedTreeView {
    constructor(transaction, branch, changeFamily, storedSchema, forest, context, nodeKeyManager, nodeKeyIndex, events) {
        this.transaction = transaction;
        this.branch = branch;
        this.changeFamily = changeFamily;
        this.storedSchema = storedSchema;
        this.forest = forest;
        this.context = context;
        this.nodeKeyManager = nodeKeyManager;
        this.nodeKeyIndex = nodeKeyIndex;
        this.events = events;
        this.nodeKey = {
            generate: () => this.nodeKeyManager.generateLocalNodeKey(),
            stabilize: (key) => this.nodeKeyManager.stabilizeNodeKey(key),
            localize: (key) => this.nodeKeyManager.localizeNodeKey(key),
            map: this.nodeKeyIndex,
        };
        branch.on("change", ({ change }) => {
            if (change !== undefined) {
                const delta = this.changeFamily.intoDelta(change);
                applyDelta(delta, this.forest.anchors);
                applyDelta(delta, this.forest);
                this.nodeKeyIndex.scanKeys(this.context);
                this.events.emit("afterBatch");
            }
        });
        branch.on("revertible", (type) => {
            this.events.emit("revertible", type);
        });
    }
    get rootEvents() {
        return this.forest.anchors;
    }
    get editor() {
        return this.branch.editor;
    }
    undo() {
        this.branch.undo();
    }
    redo() {
        this.branch.redo();
    }
    schematize(config) {
        schematizeView(this, config, this.storedSchema);
        return this;
    }
    editableTree2(viewSchema) {
        const context = getTreeContext(viewSchema, this.forest, this.branch.editor, this.nodeKeyManager, this.nodeKeyIndex.fieldKey);
        return context.root;
    }
    locate(anchor) {
        return this.forest.anchors.locate(anchor);
    }
    fork() {
        const anchors = new AnchorSet();
        // TODO: ensure editing this clone of the schema does the right thing.
        const storedSchema = new InMemoryStoredSchemaRepository(this.storedSchema);
        const forest = this.forest.clone(storedSchema, anchors);
        const repairDataStoreProvider = new ForestRepairDataStoreProvider(forest, storedSchema, (change) => this.changeFamily.intoDelta(change));
        const branch = this.branch.fork(repairDataStoreProvider);
        const context = getEditableTreeContext(forest, storedSchema, branch.editor, this.nodeKeyManager, this.nodeKeyIndex.fieldKey);
        const transaction = new Transaction(branch, this.changeFamily, forest);
        return new SharedTreeView(transaction, branch, this.changeFamily, storedSchema, forest, context, this.nodeKeyManager, this.nodeKeyIndex.clone(context), createEmitter());
    }
    rebase(view) {
        view.branch.rebaseOnto(this.branch);
    }
    rebaseOnto(view) {
        view.rebase(this);
    }
    merge(view, disposeView = true) {
        assert(!this.transaction.inProgress() || disposeView, 0x710 /* A view that is merged into an in-progress transaction must be disposed */);
        while (view.transaction.inProgress()) {
            view.transaction.commit();
        }
        this.branch.merge(view.branch);
        if (disposeView) {
            view.dispose();
        }
    }
    get root() {
        return this.context.unwrappedRoot;
    }
    setContent(data) {
        this.context.setContent(data);
    }
    /**
     * Dispose this view, freezing its state and allowing the SharedTree to release resources required by it.
     * Attempts to further mutate or dispose this view will error.
     */
    dispose() {
        this.branch.dispose();
    }
}
/**
 * @param view - view to edit.
 * @param config - config to apply.
 * @param storedSchema - provided separate from view since editing schema of view doesn't send ops properly.
 */
// TODO: once schematize is removed from ISharedTreeView, this should be moved/integrated into SharedTree.
export function schematizeView(view, config, storedSchema) {
    // TODO:
    // When this becomes a more proper out of schema adapter, editing should be made lazy.
    // This will improve support for readonly documents, cross version collaboration and attribution.
    // Check for empty.
    // TODO: Better detection of empty case
    if (view.forest.isEmpty && schemaDataIsEmpty(storedSchema)) {
        view.transaction.start();
        initializeContent(storedSchema, config.schema, () => view.setContent(config.initialTree));
        view.transaction.commit();
    }
    schematize(view.events, storedSchema, config);
}
/**
 * Run a synchronous transaction on the given shared tree view.
 * This is a convenience helper around the {@link SharedTreeFork#transaction} APIs.
 * @param view - the view on which to run the transaction
 * @param transaction - the transaction function. This will be executed immediately. It is passed `view` as an argument for convenience.
 * If this function returns an `Abort` result then the transaction will be aborted. Otherwise, it will be committed.
 * @returns whether or not the transaction was committed or aborted
 * @alpha
 */
export function runSynchronous(view, transaction) {
    view.transaction.start();
    const result = transaction(view);
    return result === TransactionResult.Abort
        ? view.transaction.abort()
        : view.transaction.commit();
}
//# sourceMappingURL=sharedTreeView.js.map