import { MailProviderError } from "@platform-modules/mail";
//#region src/email.ts
function defaultMapMessage(rendered, recipient) {
	return {
		from: recipient.from,
		to: recipient.to,
		replyTo: recipient.replyTo,
		subject: rendered.subject ?? recipient.subjectFallback ?? "notification",
		text: rendered.body,
		html: rendered.html,
		idempotencyKey: recipient.idempotencyKey
	};
}
/** Thin bridge — delegates send to `@platform-modules/mail`, does not re-implement. */
function createEmailChannel(mail, opts) {
	const mapMessage = opts?.mapMessage ?? ((r, recip) => defaultMapMessage(r, recip));
	return {
		channel: "email",
		async send(rendered, recipient) {
			const msg = mapMessage(rendered, recipient);
			try {
				return {
					ok: true,
					id: (await mail.send(msg)).id
				};
			} catch (err) {
				if (err instanceof MailProviderError) return {
					ok: false,
					error: {
						message: err.message,
						retryable: err.retryable,
						code: err.name
					}
				};
				return {
					ok: false,
					error: {
						message: err instanceof Error ? err.message : String(err),
						retryable: true
					}
				};
			}
		}
	};
}
//#endregion
export { createEmailChannel };
