import { coerceString } from "../string.js";
import { logger } from "../../logger/index.js";
import { HttpStats } from "../stats.js";
import "./legacy.js";
import { coerceNumber } from "../number.js";
import { hooks } from "./hooks.js";
import _got, { RequestError, RequestError as RequestError$1 } from "got";

//#region lib/util/http/got.ts
const got = _got;
async function fetch(url, options, queueStats) {
	logger.trace({
		url,
		options
	}, "got request");
	let duration = 0;
	let statusCode = 0;
	try {
		const resp = await got(url, {
			...options,
			hooks
		});
		statusCode = resp.statusCode;
		duration = coerceNumber(resp.timings.phases.total, 0);
		return resp;
	} catch (error) {
		// v8 ignore else -- TODO: add test #40625
		if (error instanceof RequestError) {
			statusCode = coerceNumber(error.response?.statusCode, -1);
			duration = coerceNumber(error.timings?.phases.total, -1);
			const method = options.method.toUpperCase();
			const code = coerceString(error.code, "UNKNOWN");
			const retryCount = coerceNumber(error.request?.retryCount, -1);
			logger.debug(`${method} ${url} = (code=${code}, statusCode=${statusCode} retryCount=${retryCount}, duration=${duration})`);
		}
		throw error;
	} finally {
		HttpStats.write({
			method: options.method,
			url,
			reqMs: duration,
			queueMs: queueStats.queueMs,
			status: statusCode
		});
	}
}
function stream(url, options) {
	return got.stream(url, options);
}

//#endregion
export { RequestError$1 as RequestError, fetch, stream };
//# sourceMappingURL=got.js.map