"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnhydratedTreeContext = void 0;
const internal_1 = require("@fluidframework/telemetry-utils/internal");
const treeCheckout_js_1 = require("./treeCheckout.js");
/**
 * A {@link TreeContextAlpha | tree context} that can be used for e.g. unhydrated nodes.
 */
class UnhydratedTreeContext {
    constructor() {
        this.transactionCount = 0;
    }
    isBranch() {
        return false;
    }
    runTransaction(t, params) {
        for (const constraint of params?.preconditions ?? []) {
            (0, treeCheckout_js_1.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 internal_1.UsageError("An asynchronous transaction cannot be started while another transaction is already in progress.");
        }
        for (const constraint of params?.preconditions ?? []) {
            (0, treeCheckout_js_1.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 };
    }
}
exports.UnhydratedTreeContext = UnhydratedTreeContext;
UnhydratedTreeContext.instance = new UnhydratedTreeContext();
//# sourceMappingURL=unhydratedTreeContext.js.map