import { isPlainObject } from "es-toolkit";

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

import { compactExpect, normalizeContent, 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, {
      answerCount: normalizeValue(raw["answerCount"]),
      authenticatedData: raw["authenticatedData"],
      authoritative: raw["authoritative"],
      durationMs: normalizeValue(raw["durationMs"]),
      rcode: raw["rcode"],
      recursionAvailable: raw["recursionAvailable"],
      responded: raw["responded"],
      result: normalizeContent(raw["result"]),
      truncated: raw["truncated"],
      ttlMax: normalizeValue(raw["ttlMax"]),
      ttlMin: normalizeValue(raw["ttlMin"]),
      valueCount: normalizeValue(raw["valueCount"]),
      values: raw["values"],
    }),
  };
}
