/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { EditTxn } from "../EditTxn";
export function withEditTxn(iModel, saveArgsOrFn, maybeFn) {
    const saveArgs = "function" === typeof saveArgsOrFn ? undefined : saveArgsOrFn;
    const fn = "function" === typeof saveArgsOrFn ? saveArgsOrFn : maybeFn;
    if (undefined === fn)
        throw new Error("withEditTxn requires a callback");
    const txn = new EditTxn(iModel, "test");
    txn.start();
    try {
        const result = fn(txn);
        if (result instanceof Promise) {
            return result.then((value) => {
                txn.end("save", saveArgs);
                return value;
            }, (err) => {
                if (txn.isActive)
                    txn.end("abandon");
                throw err;
            });
        }
        txn.end("save", saveArgs);
        return result;
    }
    catch (err) {
        if (txn.isActive)
            txn.end("abandon");
        throw err;
    }
}
//# sourceMappingURL=TestEditTxn.js.map