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: Record<number, number> = {};
    for (const [position, points] of Object.entries(serialized.pointsByPosition)) {
      record[Number(position)] = points;
    }
    return new PointsTable(record);
  }
}