import { PointsTable } from '@core/racing/domain/value-objects/PointsTable';

export type SerializedPointsTable = {
  pointsByPosition: Record<string, number>;
};

export class PointsTableJsonMapper {
  toJson(pointsTable: PointsTable): SerializedPointsTable {
    const pointsByPosition: Record<string, number> = {};
    for (const [position, points] of pointsTable.props.pointsByPosition.entries()) {
      pointsByPosition[String(position)] = points;
    }
    return { pointsByPosition };
  }

  fromJson(serialized: SerializedPointsTable): PointsTable {
    const record: Map<number, number> = new Map();
    for (const [position, points] of Object.entries(serialized.pointsByPosition)) {
      record.set(Number(position), points);
    }
    return new PointsTable(record);
  }
}