/**
 * Salesforce DateTime normalization
 *
 * Salesforce APIs often return DateTime strings with timezone offsets like `+0000`
 * (instead of the `Z`-suffixed ISO form expected by our public domain schemas).
 *
 * This utility normalizes supported timezone-bearing inputs to a strict UTC ISO string.
 */
export function normalizeSalesforceDateTimeToIsoUtc(value: unknown): string | null {
  if (value == null) return null;

  if (value instanceof Date) {
    const t = value.getTime();
    return Number.isNaN(t) ? null : value.toISOString();
  }

  if (typeof value !== "string") return null;

  const raw = value.trim();
  if (raw.length === 0) return null;

  // Only normalize strings that explicitly include a timezone (Z or numeric offset).
  // This avoids accidentally interpreting timezone-less strings as local time.
  const hasTimezone = raw.endsWith("Z") || /[+-]\d{2}:\d{2}$/.test(raw) || /[+-]\d{4}$/.test(raw);
  if (!hasTimezone) return null;

  const date = new Date(raw);
  const t = date.getTime();
  return Number.isNaN(t) ? null : date.toISOString();
}
