"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.V2PublicDepartureBoardsController = void 0;
const Di_1 = require("../../ioc/Di");
const OgModuleToken_1 = require("../../ioc/OgModuleToken");
const trace_provider_1 = require("@golemio/core/dist/monitoring/opentelemetry/trace-provider");
const express_validator_1 = require("@golemio/core/dist/shared/express-validator");
class V2PublicDepartureBoardsController {
    constructor() {
        this.getAll = async (req, res, next) => {
            const span = (0, trace_provider_1.createChildSpan)("V2PublicDepartureBoardsController.getAll");
            const queryData = (0, express_validator_1.matchedData)(req);
            try {
                const facade = Di_1.OgPublicContainer.resolve(OgModuleToken_1.OgModuleToken.DepartureBoardFacade);
                const params = this.parseDepartureParams(queryData);
                const result = await facade.getAll(params.stopIds, params.limit, params.routeShortNames, params.minutesAfter, params.minutesBefore);
                if (result === null) {
                    res.status(404).json([]);
                    return;
                }
                res.json(result);
            }
            catch (err) {
                next(err);
            }
            finally {
                span?.end();
            }
        };
    }
    parseDepartureParams(query) {
        return {
            stopIds: this.parseStopIds(query.stopIds instanceof Array ? query.stopIds : [query.stopIds]),
            routeShortNames: query.routeShortNames
                ? query.routeShortNames instanceof Array
                    ? query.routeShortNames
                    : [query.routeShortNames]
                : null,
            limit: query.limit ? parseInt(query.limit) : 5,
            minutesAfter: query.minutesAfter ? parseInt(query.minutesAfter) : 60,
            minutesBefore: query.minutesBefore ? parseInt(query.minutesBefore) : 0,
        };
    }
    parseStopIds(stopIdsFromInput) {
        let output = new Set();
        for (const stopId of stopIdsFromInput) {
            const parsed = JSON.parse(stopId);
            const priority = Object.keys(parsed)[0];
            const stopIds = parsed[priority];
            output.add({
                priority: Number.parseInt(priority),
                stopIds: [...new Set(stopIds)],
            });
        }
        return output;
    }
}
exports.V2PublicDepartureBoardsController = V2PublicDepartureBoardsController;
//# sourceMappingURL=V2PublicDepartureBoardsController.js.map