/**
 * Wraps a promise with a timeout. If the promise doesn't resolve within the
 * specified timeout, it rejects with a TimeoutError.
 *
 * This is particularly useful for browser RPC calls which can hang indefinitely
 * if the browser tab becomes unresponsive or disconnects.
 */
export class RpcTimeoutError extends Error {
    operationName;
    timeoutMs;
    constructor(operationName, timeoutMs) {
        super(`${operationName} timed out after ${timeoutMs}ms - browser may be unresponsive`);
        this.operationName = operationName;
        this.timeoutMs = timeoutMs;
        this.name = "RpcTimeoutError";
    }
}
/**
 * Default timeout for browser RPC calls (60 seconds).
 * This is generous enough for slow operations but will catch hung calls.
 */
export const DEFAULT_RPC_TIMEOUT_MS = 60_000;
/**
 * Wraps a promise with a timeout.
 *
 * @param promise - The promise to wrap
 * @param timeoutMs - Timeout in milliseconds (default: 30000)
 * @param operationName - Name of the operation for error messages
 * @returns The result of the promise if it resolves within the timeout
 * @throws RpcTimeoutError if the promise doesn't resolve within the timeout
 *
 * @example
 * ```ts
 * const integrations = await withRpcTimeout(
 *   editorClient.call.aiGetIntegrations(),
 *   30000,
 *   'aiGetIntegrations'
 * );
 * ```
 */
export async function withRpcTimeout(promise, timeoutMs = DEFAULT_RPC_TIMEOUT_MS, operationName = "RPC call") {
    let timeoutId;
    const timeoutPromise = new Promise((_, reject) => {
        timeoutId = setTimeout(() => {
            reject(new RpcTimeoutError(operationName, timeoutMs));
        }, timeoutMs);
    });
    try {
        const result = await Promise.race([promise, timeoutPromise]);
        clearTimeout(timeoutId);
        return result;
    }
    catch (error) {
        clearTimeout(timeoutId);
        throw error;
    }
}
//# sourceMappingURL=rpc-timeout.js.map