/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
/**
 * Makes a given `ChangeFamily` safer to use by wrapping some of its functions in try-catch blocks.
 *
 * Mitigated functions:
 * - {@link ChangeFamily.intoDelta} (an empty Delta is returned instead)
 * - {@link ChangeRebaser.rebase} (the given `fallbackChange` is returned instead)
 * - {@link ChangeRebaser.invert} (the given `fallbackChange` is returned instead)
 * - {@link ChangeRebaser.compose} (the given `fallbackChange` is returned instead)
 *
 * @param unmitigatedChangeFamily - The change family to mitigate.
 * @param fallbackChange - A changeset to use as a fallback when one of the mitigated functions throws.
 * @param onError - A callback invoked for each error thrown.
 * @returns a mitigated change family.
 */
export function makeMitigatedChangeFamily(unmitigatedChangeFamily, fallbackChange, onError) {
    return {
        buildEditor: (changeReceiver) => {
            return unmitigatedChangeFamily.buildEditor(changeReceiver);
        },
        rebaser: makeMitigatedRebaser(unmitigatedChangeFamily.rebaser, fallbackChange, onError),
        codecs: unmitigatedChangeFamily.codecs,
    };
}
export function makeMitigatedRebaser(unmitigatedRebaser, fallbackChange, onError) {
    const withFallback = (fn) => {
        try {
            return fn();
        }
        catch (error) {
            onError(error);
            return fallbackChange;
        }
    };
    return {
        compose: (changes) => {
            return withFallback(() => unmitigatedRebaser.compose(changes));
        },
        invert: (changes, isRollback) => {
            return withFallback(() => unmitigatedRebaser.invert(changes, isRollback));
        },
        rebase: (change, over, revisionMetadata) => {
            return withFallback(() => unmitigatedRebaser.rebase(change, over, revisionMetadata));
        },
        changeRevision: (change, newRevision, rollbackOf) => withFallback(() => unmitigatedRebaser.changeRevision(change, newRevision, rollbackOf)),
    };
}
//# sourceMappingURL=mitigatedChangeFamily.js.map