const require_logger = require('./logger.cjs');
const require_extractErrorMessage = require('./utils/extractErrorMessage.cjs');

//#region ../config/dist/esm/retryManager.mjs
const DEFAULT_MAX_RETRY = 3;
const DEFAULT_DELAY = 0;
/**
* Wrap an async function `fn` so it’s retried on failure.
*
* @param fn       The async function to wrap
* @param options  { maxRetry, delay }
* @returns        A new function with the same signature as `fn`
*/
const retryManager = (fn, { maxRetry = DEFAULT_MAX_RETRY, delay = DEFAULT_DELAY, onError, onMaxTryReached } = {}) => async (...args) => {
	let lastError;
	for (let attempt = 0; attempt <= maxRetry; attempt++) try {
		return await fn(...args);
	} catch (err) {
		lastError = err;
		const error = require_extractErrorMessage.extractErrorMessage(err);
		if (attempt >= maxRetry) {
			if (onMaxTryReached) {
				onMaxTryReached?.({
					error,
					attempt,
					maxRetry
				});
				return null;
			}
			throw err;
		}
		if (onError) onError?.({
			error,
			attempt,
			maxRetry
		});
		else require_logger.logger(error, { level: "error" });
		if (delay > 0) await new Promise((res) => setTimeout(res, delay));
	}
	throw lastError ?? /* @__PURE__ */ new Error("Unexpected: retry loop completed without result");
};

//#endregion
exports.retryManager = retryManager;
//# sourceMappingURL=retryManager.cjs.map