/**
 * Shared preamble for uploading an artifact payload from an `ArtifactWriter`.
 *
 * All three `ArtifactWriter` implementations (direct GCS, API Gateway, batching
 * API Gateway) must apply the same `redact → serialize → bytecount` pipeline so
 * secrets are stripped before leaving the process. Routing each writer through
 * this helper prevents drift — any future writer that skips the helper would
 * fail the contract test in
 * `src/__tests__/artifact-upload-redaction.test.ts`.
 *
 * NDJSON streaming is **not** handled here — each row is redacted independently
 * by the NDJSON writer path before being concatenated into a part body.
 */
import { redactArtifactData } from "./redact-artifact.js";
/**
 * Redact, serialize, and size `payload` for upload.
 *
 * Serialization branches on `mime`:
 * - `application/json` (and anything else JSON-shaped, including the
 *   single-shot side of `application/x-ndjson`) → `JSON.stringify`.
 * - `text/markdown` / `application/yaml` → coerce to string via `String()`.
 */
export function prepareUploadBody(payload, mime) {
    const redacted = redactArtifactData(payload);
    const body = serializeForMime(redacted, mime);
    const bytes = Buffer.byteLength(body, "utf-8");
    return { body, bytes };
}
function serializeForMime(payload, mime) {
    if (mime === "text/markdown" || mime === "application/yaml") {
        if (typeof payload === "string")
            return payload;
        return String(payload ?? "");
    }
    return JSON.stringify(payload);
}
