const sensitiveKeys = new Set([
  'fullname',
  'name',
  'workemail',
  'email',
  'company',
  'market',
  'message',
  'phone',
  'fieldvalue',
  'serverresponse',
]);

export function redactSensitive(value: unknown): unknown {
  if (Array.isArray(value)) return value.map(redactSensitive);
  if (!value || typeof value !== 'object') return value;

  return Object.fromEntries(
    Object.entries(value).map(([key, child]) => [
      key,
      sensitiveKeys.has(key.toLowerCase()) ? '[REDACTED]' : redactSensitive(child),
    ]),
  );
}

export function containsSensitiveKey(value: unknown): boolean {
  if (Array.isArray(value)) return value.some(containsSensitiveKey);
  if (!value || typeof value !== 'object') return false;
  return Object.entries(value).some(
    ([key, child]) => sensitiveKeys.has(key.toLowerCase()) || containsSensitiveKey(child),
  );
}
