/**
 * Helpers for emitting partial PII when a fully-redacted value would lose
 * essential debugging signal (e.g. last 4 digits of a phone number).
 *
 * Prefer the Pino redaction config in `./index` for everything else.
 */

export function redactPhone(value: string | null | undefined): string {
  if (!value) return '';
  const digits = String(value).replace(/\D/g, '');
  if (digits.length === 0) return '';
  if (digits.length <= 4) return `***${digits}`;
  return `***${digits.slice(-4)}`;
}

export function redactEmail(value: string | null | undefined): string {
  if (!value) return '';
  const s = String(value);
  const at = s.indexOf('@');
  if (at <= 0) return '***';
  const local = s.slice(0, at);
  const domain = s.slice(at + 1);
  const head = local.length <= 2 ? local[0] || '' : local.slice(0, 2);
  return `${head}***@${domain}`;
}

export function redactToken(value: string | null | undefined, keep = 4): string {
  if (!value) return '';
  const s = String(value);
  if (s.length <= keep) return '***';
  return `***${s.slice(-keep)}`;
}
