import { isPlainObject } from "es-toolkit";

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

import { compactExpect, normalizeContent, normalizeKeyed, 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, {
      durationMs: normalizeValue(raw["durationMs"]),
      result: normalizeContent(raw["result"]),
      rowCount: normalizeValue(raw["rowCount"]),
      rows: Array.isArray(raw["rows"]) ? raw["rows"].map((row) => normalizeKeyed(row)) : raw["rows"],
    }),
  };
}
