import { IntUtils } from '../IntUtils'; import { ProtocolResponse } from '../Base/ProtocolResponse'; import { TLevel } from '../WSAPI/WSAPITypes'; import { Level } from './Level'; export interface GetLevelMapResponse extends ProtocolResponse { levels: Level[]; } export const GetLevelMapResponseTransform = (levels: GetLevelMapResponse): TLevel[] => { if (!levels?.levels) return []; const mapped = levels.levels.map((l) => ({ id: l.level_id, name: l.level_public_meta.name, description: l.level_public_meta.description, image: l.level_public_meta.image_url, required_points: l.required_points, visibility_points: l.level_public_meta.visibility_points ? parseInt(l.level_public_meta.visibility_points as any) : null, required_level_counter_1: l.required_level_counter_1, required_level_counter_2: l.required_level_counter_2, custom_data: IntUtils.JsonOrText(l.level_public_meta?.custom_data), ordinal_position: 0, })); const ordinalById = new Map(); [...mapped] .sort((a, b) => a.required_points - b.required_points) .forEach((l, index) => ordinalById.set(l.id, index + 1)); return mapped.map((l) => ({ ...l, ordinal_position: ordinalById.get(l.id) })); };