/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { LoggingError } from "@fluidframework/telemetry-utils/internal";
/**
 * Wait for a promise or PromiseTimer to elapse.
 */
export async function raceTimer(promise, timer, cancellationToken) {
    const promises = [
        promise.then((value) => ({ result: "done", value })),
        timer.then(({ timerResult: result }) => ({ result })),
    ];
    if (cancellationToken !== undefined) {
        promises.push(cancellationToken.waitCancelled.then(() => ({ result: "cancelled" })));
    }
    return Promise.race(promises);
}
/**
 * Should we try to run a last summary for the given stop reason?
 * Currently only allows "parentNotConnected"
 * @param stopReason - SummarizerStopReason
 * @returns `true` if the stop reason can run a last summary, otherwise `false`.
 */
export function stopReasonCanRunLastSummary(stopReason) {
    return stopReason === "parentNotConnected";
}
const summarizeErrors = {
    /**
     * Error encountered while generating the summary tree, uploading
     * it to storage, or submitting the op. It could be a result of
     * the client becoming disconnected while generating or an actual error.
     */
    submitSummaryFailure: "Error while generating, uploading, or submitting summary",
    /**
     * The summaryAckWaitTimeout time has elapsed before receiving the summarize op
     * sent by this summarize attempt. It is expected to be broadcast quickly.
     */
    summaryOpWaitTimeout: "Timeout while waiting for summarize op broadcast",
    /**
     * The summaryAckWaitTimeout time has elapsed before receiving either a
     * summaryAck or summaryNack op from the server in response to this
     * summarize attempt. It is expected that the server should respond.
     */
    summaryAckWaitTimeout: "Timeout while waiting for summaryAck/summaryNack op",
    /**
     * The server responded with a summaryNack op, thus rejecting this
     * summarize attempt.
     */
    summaryNack: "Server rejected summary via summaryNack op",
    disconnect: "Summary cancelled due to summarizer or main client disconnect",
};
// Helper functions to report failures and return.
export const getFailMessage = (errorCode) => `${errorCode}: ${summarizeErrors[errorCode]}`;
/**
 * Errors type for errors hit during summary that may be retriable.
 */
export class RetriableSummaryError extends LoggingError {
    constructor(message, retryAfterSeconds, props) {
        super(message, props);
        this.retryAfterSeconds = retryAfterSeconds;
    }
}
//# sourceMappingURL=summarizerUtils.js.map