import { ReturnProcessQuestionKey } from '../../models';

/**
 * Serializes a return details object into a string representation.
 *
 * @param returnDetails - An object mapping ReturnProcessQuestionKey to values. May be undefined.
 * @returns A semicolon-separated string of key:value pairs, or undefined if input is empty or undefined.
 */
export const serializeReturnDetails = (
  returnDetails: Partial<Record<ReturnProcessQuestionKey, unknown>> | undefined,
): string | undefined => {
  if (!returnDetails || Object.keys(returnDetails).length === 0) {
    return undefined;
  }

  return Object.entries(returnDetails)
    .map(([key, value]) => `${key}:${String(value)}`)
    .join(';');
};

/**
 * Maps a return details object to an array of formatted strings.
 *
 * @param returnDetails - An object mapping ReturnProcessQuestionKey to values.
 * @returns An array of strings in the format "key: value", with arrays joined by comma and space.
 */
export const returnDetailsMapping = (
  returnDetails: Partial<Record<ReturnProcessQuestionKey, unknown>>,
): string[] => {
  const entries = Object.entries(returnDetails) as [
    ReturnProcessQuestionKey,
    unknown,
  ][];
  const mapped: string[] = [];

  for (const [key, value] of entries) {
    const valString = Array.isArray(value)
      ? value.map((v) => String(v)).join(', ')
      : String(value);

    mapped.push(`${key}: ${valString}`);
  }

  return mapped;
};
