import { c as normalizeOptionalString } from "./string-coerce-DW4mBlAt.js";
import { u as readResponseWithLimit } from "./http-body-BtJOYB8-.js";
import { r as assertOkOrThrowHttpError } from "./provider-http-errors-Bh4de1Nm.js";
import { r as extensionForMime } from "./mime-CDtiB791.js";
import { t as executeProviderOperationWithRetry } from "./operation-retry-D0OANMNK.js";
import { o as fetchWithTimeoutGuarded } from "./shared-mcx3jCpt.js";
import "./string-coerce-runtime-DsF17V0Z.js";
import "./response-limit-runtime-CJit9vXg.js";
import "./media-mime-D2wRVJcz.js";
import "./provider-http-D6eC_5jF.js";
//#region extensions/xai/video-generation-transport.ts
function resolveXaiVideoFetchTimeoutMs(timeoutMs, defaultTimeoutMs) {
	const resolved = typeof timeoutMs === "function" ? timeoutMs() : timeoutMs;
	return typeof resolved === "number" && Number.isFinite(resolved) && resolved > 0 ? resolved : defaultTimeoutMs;
}
async function fetchXaiVideoResponse(params) {
	return await executeProviderOperationWithRetry({
		provider: "xai",
		stage: params.stage,
		operation: async () => {
			const result = await fetchWithTimeoutGuarded(params.url, params.init, resolveXaiVideoFetchTimeoutMs(params.timeoutMs, params.defaultTimeoutMs), params.fetchFn, {
				...params.allowPrivateNetwork ? { ssrfPolicy: { allowPrivateNetwork: true } } : {},
				...params.dispatcherPolicy ? { dispatcherPolicy: params.dispatcherPolicy } : {},
				auditContext: params.auditContext
			});
			try {
				await assertOkOrThrowHttpError(result.response, params.requestFailedMessage);
				return result;
			} catch (error) {
				await result.release();
				throw error;
			}
		}
	});
}
async function downloadXaiVideo(params) {
	const { response, release } = await fetchXaiVideoResponse({
		url: params.url,
		stage: "download",
		requestFailedMessage: "xAI generated video download failed",
		auditContext: "xai-video-download",
		init: { method: "GET" },
		timeoutMs: params.timeoutMs,
		defaultTimeoutMs: params.defaultTimeoutMs,
		allowPrivateNetwork: params.allowPrivateNetwork,
		dispatcherPolicy: params.dispatcherPolicy,
		fetchFn: params.fetchFn
	});
	try {
		const mimeType = normalizeOptionalString(response.headers.get("content-type")) ?? "video/mp4";
		return {
			buffer: await readResponseWithLimit(response, params.maxBytes, { onOverflow: ({ maxBytes }) => /* @__PURE__ */ new Error(`xAI generated video download exceeds ${maxBytes} bytes`) }),
			mimeType,
			fileName: `video-1.${extensionForMime(mimeType)?.slice(1) ?? "mp4"}`
		};
	} finally {
		await release();
	}
}
//#endregion
export { fetchXaiVideoResponse as n, downloadXaiVideo as t };
