import { loadCredentials } from "../lib/credentials.js";
import { sendEmail } from "../lib/smtp.js";
import { resolveOutboundAttachments, formatAttachNote } from "../lib/attachment-resolve.js";
export async function emailSend(params) {
    const result = await loadCredentials(params.accountId);
    if ("error" in result)
        throw new Error(result.error);
    const { config, password } = result.credentials;
    // Validate attachment paths before the SMTP try so a bad path surfaces as a
    // clean validation error (no email sent) rather than picking up the alias
    // diagnostic below.
    const attachments = resolveOutboundAttachments(process.env.ACCOUNT_DIR, params.attachments);
    try {
        // Append AI provenance disclosure (EU AI Act Code of Practice)
        const bodyWithDisclosure = params.body + "\n\n---\nThis message was generated by AI.";
        const htmlWithDisclosure = params.html
            ? params.html + '<br/><hr style="margin-top:20px;border:none;border-top:1px solid #ccc;"/><p style="color:#888;font-size:12px;">This message was generated by AI.</p>'
            : undefined;
        const { messageId } = await sendEmail(config, password, {
            to: params.to,
            subject: params.subject,
            body: bodyWithDisclosure,
            html: htmlWithDisclosure,
            headers: { "X-AI-Generated": "true" },
            attachments,
        });
        return `Email sent to ${params.to}${formatAttachNote(attachments)} (Message-ID: ${messageId})`;
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        if (config.agentAddress !== config.email) {
            throw new Error(`${msg} — Note: sending as ${config.agentAddress} (authenticated as ${config.email}). If your provider doesn't allow sending as this alias, check your catchall or alias settings.`);
        }
        throw err;
    }
}
//# sourceMappingURL=email-send.js.map