/**
 * Transactional email send — one-shot script invoked by the UI server
 * for system emails (magic-link delivery, etc.) that must not carry the
 * `email-send` tool's AI-generated disclosure footer.
 *
 * Stdin JSON payload: { accountId, to, subject, body, html? }
 * Stdout on success: `ok messageId=<id>`
 * Stderr on failure: error message
 * Exit code: 0 on success, 1 on any failure.
 *
 * Spawned via `node dist/scripts/send-transactional.js` from a child
 * process; reuses the email plugin's existing nodemailer + credentials
 * loaders so no new dependency travels into the UI bundle.
 */
import { readFileSync } from "node:fs";
import { loadCredentials } from "../lib/credentials.js";
import { sendEmail } from "../lib/smtp.js";
function readStdin() {
    // Block until EOF on fd 0 — simpler than the async stream pattern for a
    // one-shot script. Throws if no payload arrived.
    try {
        return readFileSync(0, "utf-8");
    }
    catch (err) {
        throw new Error(`failed to read stdin: ${err instanceof Error ? err.message : String(err)}`);
    }
}
async function main() {
    const raw = readStdin();
    if (!raw.trim()) {
        throw new Error("empty stdin payload");
    }
    const payload = JSON.parse(raw);
    for (const field of ["accountId", "to", "subject", "body"]) {
        if (typeof payload[field] !== "string" || !payload[field].length) {
            throw new Error(`missing required field: ${field}`);
        }
    }
    const result = await loadCredentials(payload.accountId);
    if ("error" in result)
        throw new Error(result.error);
    const { config, password } = result.credentials;
    const { messageId } = await sendEmail(config, password, {
        to: payload.to,
        subject: payload.subject,
        body: payload.body,
        html: payload.html,
        headers: payload.headers,
    });
    process.stdout.write(`ok messageId=${messageId}\n`);
}
main().catch((err) => {
    process.stderr.write(`${err instanceof Error ? err.message : String(err)}\n`);
    process.exit(1);
});
//# sourceMappingURL=send-transactional.js.map