import { isPlainObject } from "es-toolkit";

import type { RawTargetConfig } from "../../types";

import { compactExpect, normalizeValue } from "../../expect/normalize";

export function normalizeTargetExpect(target: RawTargetConfig): RawTargetConfig {
  if (target.expect === undefined || !isPlainObject(target.expect)) return target;
  const raw = target.expect as Record<string, unknown>;
  return {
    ...target,
    expect: compactExpect(raw, {
      alive: raw["alive"],
      avgLatencyMs: normalizeValue(raw["avgLatencyMs"]),
      durationMs: normalizeValue(raw["durationMs"]),
      maxLatencyMs: normalizeValue(raw["maxLatencyMs"]),
      packetLossPercent: normalizeValue(raw["packetLossPercent"]),
    }),
  };
}
