export const RUNTIME_ARTIFACT_MARKER = "TESTKIT_ARTIFACT:";

export function emitArtifact(name, data, options = {}) {
  const normalizedName = normalizeArtifactName(name);
  const payload = encodeURIComponent(
    JSON.stringify({
      name: normalizedName,
      kind: normalizeOptionalString(options.kind),
      summary: normalizeOptionalString(options.summary),
      contentType: normalizeContentType(options.contentType),
      data,
      emittedAt: new Date().toISOString(),
    })
  );
  console.log(
    `${RUNTIME_ARTIFACT_MARKER}${payload}`
  );
}

function normalizeArtifactName(name) {
  if (typeof name !== "string" || name.trim().length === 0) {
    throw new Error("emitArtifact(name, data) requires a non-empty artifact name");
  }
  return name.trim();
}

function normalizeOptionalString(value) {
  if (value === undefined || value === null) return null;
  const normalized = String(value).trim();
  return normalized.length > 0 ? normalized : null;
}

function normalizeContentType(value) {
  const normalized = normalizeOptionalString(value);
  return normalized || "application/json";
}
