import { logger } from "../util/logger.js";
import { redactUrl } from "../util/redact.js";
import { encryptAesEcb } from "./aes-ecb.js";
import { buildCdnUploadUrl } from "./cdn-url.js";
//#region extensions/weixin/src/cdn/cdn-upload.ts
/** Maximum retry attempts for CDN upload. */
const UPLOAD_MAX_RETRIES = 3;
/**
* Upload one buffer to the Weixin CDN with AES-128-ECB encryption.
* Returns the download encrypted_query_param from the CDN response.
* Retries up to UPLOAD_MAX_RETRIES times on server errors; client errors (4xx) abort immediately.
*/
async function uploadBufferToCdn(params) {
	const { buf, uploadFullUrl, uploadParam, filekey, cdnBaseUrl, label, aeskey } = params;
	const ciphertext = encryptAesEcb(buf, aeskey);
	const trimmedFull = uploadFullUrl?.trim();
	let cdnUrl;
	if (trimmedFull) cdnUrl = trimmedFull;
	else if (uploadParam) cdnUrl = buildCdnUploadUrl({
		cdnBaseUrl,
		uploadParam,
		filekey
	});
	else throw new Error(`${label}: CDN upload URL missing (need upload_full_url or upload_param)`);
	logger.debug(`${label}: CDN POST url=${redactUrl(cdnUrl)} ciphertextSize=${ciphertext.length}`);
	let downloadParam;
	let lastError;
	for (let attempt = 1; attempt <= UPLOAD_MAX_RETRIES; attempt++) try {
		const res = await fetch(cdnUrl, {
			method: "POST",
			headers: { "Content-Type": "application/octet-stream" },
			body: new Uint8Array(ciphertext)
		});
		if (res.status >= 400 && res.status < 500) {
			const errMsg = res.headers.get("x-error-message") ?? await res.text();
			logger.error(`${label}: CDN client error attempt=${attempt} status=${res.status} errMsg=${errMsg}`);
			throw new Error(`CDN upload client error ${res.status}: ${errMsg}`);
		}
		if (res.status !== 200) {
			const errMsg = res.headers.get("x-error-message") ?? `status ${res.status}`;
			logger.error(`${label}: CDN server error attempt=${attempt} status=${res.status} errMsg=${errMsg}`);
			throw new Error(`CDN upload server error: ${errMsg}`);
		}
		downloadParam = res.headers.get("x-encrypted-param") ?? void 0;
		if (!downloadParam) {
			logger.error(`${label}: CDN response missing x-encrypted-param header attempt=${attempt}`);
			throw new Error("CDN upload response missing x-encrypted-param header");
		}
		logger.debug(`${label}: CDN upload success attempt=${attempt}`);
		break;
	} catch (err) {
		lastError = err;
		if (err instanceof Error && err.message.includes("client error")) throw err;
		if (attempt < UPLOAD_MAX_RETRIES) logger.error(`${label}: attempt ${attempt} failed, retrying... err=${String(err)}`);
		else logger.error(`${label}: all ${UPLOAD_MAX_RETRIES} attempts failed err=${String(err)}`);
	}
	if (!downloadParam) throw lastError instanceof Error ? lastError : /* @__PURE__ */ new Error(`CDN upload failed after ${UPLOAD_MAX_RETRIES} attempts`);
	return { downloadParam };
}
//#endregion
export { uploadBufferToCdn };

//# sourceMappingURL=cdn-upload.js.map