"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeMitigatedRebaser = exports.makeMitigatedChangeFamily = void 0;
/**
 * 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.
 */
function makeMitigatedChangeFamily(unmitigatedChangeFamily, fallbackChange, onError) {
    return {
        buildEditor: (mintRevisionTag, changeReceiver) => {
            return unmitigatedChangeFamily.buildEditor(mintRevisionTag, changeReceiver);
        },
        rebaser: makeMitigatedRebaser(unmitigatedChangeFamily.rebaser, fallbackChange, onError),
        codecs: unmitigatedChangeFamily.codecs,
    };
}
exports.makeMitigatedChangeFamily = makeMitigatedChangeFamily;
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, revision) => {
            return withFallback(() => unmitigatedRebaser.invert(changes, isRollback, revision));
        },
        rebase: (change, over, revisionMetadata, ignoreNoChangeViolation) => {
            return withFallback(() => unmitigatedRebaser.rebase(change, over, revisionMetadata, ignoreNoChangeViolation));
        },
        getRevisions: (change) => {
            try {
                return unmitigatedRebaser.getRevisions(change);
            }
            catch (error) {
                onError(error);
                return new Set();
            }
        },
        changeRevision: (change, replacer) => withFallback(() => unmitigatedRebaser.changeRevision(change, replacer)),
    };
}
exports.makeMitigatedRebaser = makeMitigatedRebaser;
//# sourceMappingURL=mitigatedChangeFamily.js.map