import { t as E_INVALID_CONFIG } from "./errors-CzM80k9O.js";
//#region src/utils.ts
/**
* Headers that the JSON based transports (Resend, Brevo) reconstruct from
* the structured payload fields or that are owned by the provider/MIME
* layer. Forwarding them inside the "headers" object would either
* duplicate or conflict with the values the API already computes.
*/
const STRUCTURAL_HEADERS = /* @__PURE__ */ new Set([
	"from",
	"sender",
	"reply-to",
	"to",
	"cc",
	"bcc",
	"subject",
	"message-id",
	"date",
	"content-type",
	"content-transfer-encoding",
	"mime-version"
]);
/**
* Extracts the user defined headers (custom headers, "List-" headers,
* "In-Reply-To", "References", etc.) from a Nodemailer message so they
* can be forwarded to the API based transports that accept an arbitrary
* "headers" object (Resend, Brevo).
*
* Nodemailer has already serialized every header onto the underlying
* MimeNode (including the tricky "List-*" formatting), so we read them
* back instead of re-implementing the serialization. Structural headers
* that the API derives from the structured payload are skipped, and
* duplicate keys (e.g. a multi value "List-Unsubscribe") are collapsed
* into a single comma separated value.
*/
function extractMessageHeaders(mail) {
	const rawHeaders = mail.message._headers;
	if (!Array.isArray(rawHeaders)) return;
	const headers = {};
	for (const { key, value } of rawHeaders) {
		if (STRUCTURAL_HEADERS.has(key.toLowerCase())) continue;
		/**
		* Prepared and "List-" headers are stored as
		* `{ prepared, foldLines, value }` objects.
		*/
		const normalized = value && typeof value === "object" && "value" in value ? value.value : value;
		const stringValue = Array.isArray(normalized) ? normalized.join(", ") : String(normalized);
		headers[key] = headers[key] ? `${headers[key]}, ${stringValue}` : stringValue;
	}
	return Object.keys(headers).length ? headers : void 0;
}
/**
* Resolves the content of a message attachment to a Buffer. Nodemailer's
* "resolveContent" reads the attachment regardless of how it was defined
* (local path, "file://"/"data:"/remote URL, stream or raw buffer), the
* same way the MIME based transports resolve attachments. This is needed
* by the JSON based transports (Resend, Brevo) that must send the actual
* file content as a base64 string instead of forwarding the raw input.
*/
function resolveAttachmentContent(mail, index) {
	return new Promise((resolve, reject) => {
		mail.resolveContent(mail.data.attachments, index, (error, content) => {
			if (error) {
				reject(error);
				return;
			}
			resolve(Buffer.isBuffer(content) ? content : Buffer.from(content));
		});
	});
}
/**
* Convert a stream to a blob
*/
function streamToBlob(stream, mimeType) {
	return new Promise((resolve, reject) => {
		const chunks = [];
		stream.on("data", (chunk) => chunks.push(chunk)).once("end", () => resolve(new Blob(chunks, { type: mimeType }))).once("error", reject);
	});
}
/**
* Normalize the base URL by removing the trailing slash
*/
function normalizeBaseUrl(baseUrl) {
	return baseUrl.replace(/\/$/, "");
}
/**
* Validate transport config to ensure the "key" and
* "baseUrl" are present.
*/
function validateConfig(transportName, config) {
	if (!config.key) throw new E_INVALID_CONFIG(`Invalid config for "${transportName}" transport. The "key" property is missing`);
	if (!config.baseUrl) throw new E_INVALID_CONFIG(`Invalid config for "${transportName}" transport. The "baseUrl" property is missing`);
}
//#endregion
//#region src/transports/base_api_transport.ts
/**
* Base class for HTTP based transports to validate the config
* and normalize the base URL
*/
var BaseApiTransport = class {
	name;
	config;
	constructor(name, config) {
		this.name = name;
		this.config = config;
		validateConfig(name, config);
	}
};
//#endregion
export { streamToBlob as a, resolveAttachmentContent as i, extractMessageHeaders as n, normalizeBaseUrl as r, BaseApiTransport as t };
