export function normalizeTelemetryConfig(telemetry) {
  if (!telemetry) return null;
  if (telemetry.endpoint) {
    let parsed;
    try {
      parsed = new URL(telemetry.endpoint);
    } catch {
      throw new Error("testkit.setup telemetry.endpoint must be a valid URL");
    }
    if (!["http:", "https:"].includes(parsed.protocol)) {
      throw new Error("testkit.setup telemetry.endpoint must use http or https");
    }
  }
  if (telemetry.enabled === true) {
    if (!telemetry.endpoint) {
      throw new Error("testkit.setup telemetry.endpoint is required when telemetry.enabled is true");
    }
    if (!telemetry.tokenEnv) {
      throw new Error("testkit.setup telemetry.tokenEnv is required when telemetry.enabled is true");
    }
  }
  return {
    enabled: telemetry.enabled === true,
    endpoint: telemetry.endpoint,
    tokenEnv: telemetry.tokenEnv,
    timeoutMs: telemetry.timeoutMs || 3000,
  };
}
