"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.V2PublicVehiclePositionsController = void 0;
const RouteTypeEnums_1 = require("../../../../helpers/RouteTypeEnums");
const const_1 = require("../../domain/const");
const Di_1 = require("../../ioc/Di");
const OgModuleToken_1 = require("../../ioc/OgModuleToken");
const express_validator_1 = require("@golemio/core/dist/shared/express-validator");
class V2PublicVehiclePositionsController {
    constructor() {
        this.getAll = async (req, res, next) => {
            try {
                const queryData = (0, express_validator_1.matchedData)(req);
                const facade = Di_1.OgPublicContainer.resolve(OgModuleToken_1.OgModuleToken.PublicVehiclePositionsFacade);
                const params = this.parsePositionsParams(queryData);
                const result = await facade.getAll(params.boundingBox, params.routeShortName, params.type);
                res.json(result);
            }
            catch (err) {
                next(err);
            }
        };
        this.getOneByVehicleId = async (req, res, next) => {
            try {
                const facade = Di_1.OgPublicContainer.resolve(OgModuleToken_1.OgModuleToken.DetailedTripFacade);
                const params = this.parseDetailedParams(req);
                const info = await facade.getOneByVehicleId(params.vehicleId, params.scopes);
                res.json(info);
            }
            catch (err) {
                next(err);
            }
        };
        this.getOneByParamsCombination = async (req, res, next) => {
            try {
                const facade = Di_1.OgPublicContainer.resolve(OgModuleToken_1.OgModuleToken.DetailedTripFacade);
                const params = this.parseDetailedParams(req);
                const info = await facade.getOneByVehicleId(params.vehicleId, params.scopes, params.tripId);
                res.json(info);
            }
            catch (err) {
                next(err);
            }
        };
    }
    parsePositionsParams(query) {
        const boundingBox = query.boundingBox ? query.boundingBox.split(",").map((x) => parseFloat(x)) : undefined;
        // format "topLeft.lat,topLeft.lon,bottomRight.lat,bottomRight.lon"
        return {
            boundingBox: {
                topLeftLongitude: boundingBox ? boundingBox[1] : const_1.DEFAULT_BOUDINGBOX.topLeftLongitude,
                topLeftLatitude: boundingBox ? boundingBox[0] : const_1.DEFAULT_BOUDINGBOX.topLeftLatitude,
                bottomRightLongitude: boundingBox ? boundingBox[3] : const_1.DEFAULT_BOUDINGBOX.bottomRightLongitude,
                bottomRightLatitude: boundingBox ? boundingBox[2] : const_1.DEFAULT_BOUDINGBOX.bottomRightLatitude,
            },
            routeShortName: query.routeShortName
                ? query.routeShortName instanceof Array
                    ? query.routeShortName.map((routeShortName) => routeShortName)
                    : query.routeShortName
                : undefined,
            type: query.routeType
                ? query.routeType instanceof Array
                    ? query.routeType.map((type) => (0, RouteTypeEnums_1.getGtfsRouteType)(type))
                    : [(0, RouteTypeEnums_1.getGtfsRouteType)(query.routeType)]
                : undefined,
        };
    }
    parseDetailedParams(req) {
        const queryData = (0, express_validator_1.matchedData)(req);
        return {
            vehicleId: req.params.vehicleId,
            tripId: req.params.gtfsTripId,
            scopes: queryData.scopes instanceof Array
                ? Array.from(new Set(queryData.scopes))
                : [queryData.scopes],
        };
    }
}
exports.V2PublicVehiclePositionsController = V2PublicVehiclePositionsController;
//# sourceMappingURL=V2PublicVehiclePositionsController.js.map