import { MailProviderError } from "./index.js";
import { ServerClient } from "postmark";
//#region src/postmark.ts
function toList(value) {
	return (Array.isArray(value) ? value : [value]).join(",");
}
function makePostmarkAdapter({ serverToken }) {
	const client = new ServerClient(serverToken);
	return { async send(msg) {
		try {
			return {
				id: (await client.sendEmail({
					From: msg.from,
					To: toList(msg.to),
					Cc: msg.cc ? toList(msg.cc) : void 0,
					Bcc: msg.bcc ? toList(msg.bcc) : void 0,
					Subject: msg.subject,
					HtmlBody: msg.html,
					TextBody: msg.text,
					ReplyTo: msg.replyTo,
					Headers: msg.headers ? Object.entries(msg.headers).map(([Name, Value]) => ({
						Name,
						Value
					})) : void 0
				})).MessageID,
				provider: "postmark"
			};
		} catch (err) {
			const statusCode = typeof err === "object" && err !== null && "statusCode" in err && typeof err.statusCode === "number" ? err.statusCode : void 0;
			throw new MailProviderError(err instanceof Error ? err.message : "postmark send failed", "postmark", statusCode !== void 0 && statusCode >= 500, err);
		}
	} };
}
//#endregion
export { makePostmarkAdapter };
