import { TELEMETRY_SUMMARY_RULES } from './telemetry-event-summary.config';
import { fallbackCompactJson, unwrapJsonPayload } from './telemetry-event-summary.payload';

export function summarizeTelemetryData(data: unknown): string {
  const raw = unwrapJsonPayload(data);
  if (raw === null || raw === undefined) {
    return '—';
  }
  if (Array.isArray(raw)) {
    return raw.map((v) => String(v)).join(', ');
  }
  if (typeof raw !== 'object') {
    return String(raw);
  }

  const o = raw as Record<string, unknown>;
  for (const rule of TELEMETRY_SUMMARY_RULES) {
    if (rule.match(o)) {
      try {
        return rule.summarize(o);
      } catch {
        // ignore
      }
    }
  }
  return fallbackCompactJson(o);
}
