"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.V3TransferBoardsController = void 0;
const OgPidToken_1 = require("../../ioc/OgPidToken");
const InfotextFacade_1 = require("../../service/facade/InfotextFacade");
const StopFacade_1 = require("../../service/facade/StopFacade");
const TransferFacade_1 = require("../../service/facade/TransferFacade");
const RopidRouterUtils_1 = require("../../../shared/RopidRouterUtils");
const trace_provider_1 = require("@golemio/core/dist/monitoring/opentelemetry/trace-provider");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const moment_timezone_1 = __importDefault(require("@golemio/core/dist/shared/moment-timezone"));
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const express_validator_1 = require("@golemio/core/dist/shared/express-validator");
let V3TransferBoardsController = class V3TransferBoardsController {
    constructor(stopFacade, infotextFacade, transferFacade) {
        this.stopFacade = stopFacade;
        this.infotextFacade = infotextFacade;
        this.transferFacade = transferFacade;
        this.getTransferDepartures = async (req, res, next) => {
            const queryData = (0, express_validator_1.matchedData)(req);
            const span = (0, trace_provider_1.createChildSpan)("V3TransferBoardsController.getTransferDepartures");
            const cisId = queryData.cisId;
            const tripNumber = queryData.tripNumber ?? null;
            span?.setAttributes({ cisId, tripNumber });
            // Additional type check for query string parameters
            if (!this.isValidQueryStringParam(cisId)) {
                return next(this.recordBadRequestException("cisId is not valid", span));
            }
            else if (tripNumber && !this.isValidQueryStringParam(tripNumber)) {
                return next(this.recordBadRequestException("tripNumber is not valid", span));
            }
            const currentMoment = (0, moment_timezone_1.default)();
            let timeFrom = null;
            let minutesOffset = 0;
            if (queryData.timeFrom) {
                timeFrom = moment_timezone_1.default.tz(queryData.timeFrom, RopidRouterUtils_1.RopidRouterUtils.TIMEZONE);
                minutesOffset = timeFrom.diff(currentMoment, "minutes");
            }
            try {
                //#region GTFS stops
                let stopIds = [];
                try {
                    stopIds = await this.stopFacade.getStopIdsForTransferBoards(cisId);
                }
                catch (error) {
                    span?.recordException(error);
                    throw error;
                }
                //#endregion
                if (stopIds.length === 0) {
                    return res.status(404).json({
                        departures: [],
                        infotexts: [],
                    });
                }
                //#region Transfer departures and infotexts
                let departuresToInclude = [];
                let infotextsToInclude = [];
                try {
                    const [departures, infotexts] = await Promise.all([
                        this.transferFacade.getTransferDepartures(stopIds, tripNumber, currentMoment, minutesOffset),
                        this.infotextFacade.getInfotextsForTransferBoards(stopIds, currentMoment, timeFrom),
                    ]);
                    departuresToInclude = departures;
                    infotextsToInclude = infotexts;
                }
                catch (error) {
                    span?.recordException(error);
                    throw error;
                }
                //#endregion
                return res.status(200).json({
                    departures: departuresToInclude,
                    infotexts: infotextsToInclude,
                });
            }
            catch (err) {
                next(err);
            }
            finally {
                span?.end();
            }
        };
        this.recordBadRequestException = (errorMessage, span) => {
            const exception = new golemio_errors_1.GeneralError(errorMessage, this.constructor.name, undefined, 400);
            span?.recordException(exception);
            return exception;
        };
        this.isValidQueryStringParam = (param) => {
            return typeof param === "string";
        };
    }
};
exports.V3TransferBoardsController = V3TransferBoardsController;
exports.V3TransferBoardsController = V3TransferBoardsController = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.StopFacade)),
    __param(1, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.InfotextFacade)),
    __param(2, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.TransferFacade)),
    __metadata("design:paramtypes", [StopFacade_1.StopFacade,
        InfotextFacade_1.InfotextFacade,
        TransferFacade_1.TransferFacade])
], V3TransferBoardsController);
//# sourceMappingURL=V3TransferBoardsController.js.map