/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { UsageError } from "@fluidframework/telemetry-utils/internal";
import { assertValidConstraint } from "./treeCheckout.js";
/**
 * A {@link TreeContextAlpha | tree context} that can be used for e.g. unhydrated nodes.
 */
export class UnhydratedTreeContext {
    constructor() {
        this.transactionCount = 0;
    }
    isBranch() {
        return false;
    }
    runTransaction(t, params) {
        for (const constraint of params?.preconditions ?? []) {
            assertValidConstraint(constraint, false);
        }
        this.transactionCount += 1;
        try {
            return UnhydratedTreeContext.wrapTransactionResult(t());
        }
        finally {
            this.transactionCount -= 1;
        }
    }
    async runTransactionAsync(t, params) {
        if (this.transactionCount > 0) {
            throw new UsageError("An asynchronous transaction cannot be started while another transaction is already in progress.");
        }
        for (const constraint of params?.preconditions ?? []) {
            assertValidConstraint(constraint, false);
        }
        this.transactionCount += 1;
        try {
            return UnhydratedTreeContext.wrapTransactionResult(await t());
        }
        finally {
            this.transactionCount -= 1;
        }
    }
    static wrapTransactionResult(value) {
        if (value?.value !== undefined) {
            return { success: true, value: value.value };
        }
        return { success: true };
    }
}
UnhydratedTreeContext.instance = new UnhydratedTreeContext();
//# sourceMappingURL=unhydratedTreeContext.js.map