"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.webhookPayloadSchema = void 0;
const zod_1 = require("zod");
const types_1 = require("./types");
/**
 * Incoming string values are trimmed so stray whitespace never leaks into
 * data points, identifiers, or the pre-signed URLs we pass downstream.
 */
const trimmedString = zod_1.z.string().trim();
const metaSchema = zod_1.z.object({
    messageId: trimmedString,
    when: trimmedString,
    requestId: trimmedString.optional(),
    data: zod_1.z.unknown().optional(),
});
const adtEventPayloadSchema = zod_1.z.object({
    url: trimmedString.url(),
    patientId: trimmedString,
    externalId: trimmedString.optional(),
    additionalIds: zod_1.z.record(zod_1.z.array(trimmedString)).optional(),
    admitTimestamp: trimmedString,
    dischargeTimestamp: trimmedString.optional(),
    whenSourceSent: trimmedString.optional(),
});
/**
 * A patient entry as sent by the `medical.*` webhook family. Only `patientId`
 * is required; everything else is optional and unknown fields are preserved
 * (`passthrough`) since the discharge summary event is undocumented.
 */
const medicalPatientEntrySchema = zod_1.z
    .object({
    patientId: trimmedString,
    externalId: trimmedString.optional(),
    additionalIds: zod_1.z.record(zod_1.z.array(trimmedString)).optional(),
    status: trimmedString.optional(),
    bundle: zod_1.z.unknown().optional(),
    url: trimmedString.url().optional(),
})
    .passthrough();
const patientAdmitWebhookSchema = zod_1.z.object({
    meta: metaSchema.extend({
        type: zod_1.z.literal(types_1.MetriportWebhookType.PatientAdmit),
    }),
    payload: adtEventPayloadSchema,
});
const patientDischargeWebhookSchema = zod_1.z.object({
    meta: metaSchema.extend({
        type: zod_1.z.literal(types_1.MetriportWebhookType.PatientDischarge),
    }),
    payload: adtEventPayloadSchema.extend({
        dischargeTimestamp: trimmedString,
    }),
});
const patientTransferWebhookSchema = zod_1.z.object({
    meta: metaSchema.extend({
        type: zod_1.z.literal(types_1.MetriportWebhookType.PatientTransfer),
    }),
    payload: adtEventPayloadSchema,
});
const dischargeSummaryWebhookSchema = zod_1.z.object({
    meta: metaSchema.extend({
        type: zod_1.z.literal(types_1.MetriportWebhookType.DischargeSummary),
    }),
    patients: zod_1.z.array(medicalPatientEntrySchema).optional(),
    payload: adtEventPayloadSchema.partial().optional(),
});
const pingWebhookSchema = zod_1.z.object({
    meta: metaSchema.extend({
        type: zod_1.z.literal(types_1.MetriportWebhookType.Ping),
    }),
    ping: trimmedString,
});
/**
 * Parses an incoming Metriport webhook. The discriminator (`meta.type`) is
 * nested, so we rely on a union rather than a discriminated union.
 */
exports.webhookPayloadSchema = zod_1.z.union([
    patientAdmitWebhookSchema,
    patientDischargeWebhookSchema,
    patientTransferWebhookSchema,
    dischargeSummaryWebhookSchema,
    pingWebhookSchema,
]);
//# sourceMappingURL=validation.zod.js.map