import bikePointsData from '../../shared/data/bike-point-data.json'
import { IBikePoint } from '@entities/BikePoint'

const bikePoints = bikePointsData as IBikePoint[]

export interface IBikePointDao {
    getById: (bikePointId: string) => Promise<IBikePoint | null>
    getAll: (bikePointId: string) => Promise<IBikePoint[]>
}

class BikePointDao implements IBikePointDao {

    /**
     * @param bikePointId (without Prefix 'BikePoints_', only the numbers behind)
     */
    public getById(bikePointId: string): Promise<IBikePoint | null> {
        return Promise.resolve(getBikePointById(bikePoints, bikePointId))
    }

    public getAll(): Promise<IBikePoint[]> {
        return Promise.resolve(bikePoints.map(bikePoint => removeBikePointIdPrefix(bikePoint)))
    }
}

export const getBikePointById = (bikePoints: IBikePoint[], bikePointId: string) => {
    for (const bikePoint of bikePoints) {
        if (bikePoint.id.replace('BikePoints_', '') === bikePointId) {
            return removeBikePointIdPrefix(bikePoint)
        }
    }
    return null
}

const removeBikePointIdPrefix = (bikePoint: IBikePoint) => ({...bikePoint, id: bikePoint.id.replace('BikePoints_', '')})

export default BikePointDao
