"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.V2DepartureBoardsController = void 0;
const dto_1 = require("../../dto");
const PIDDepartureBoardsModel_1 = require("../../models/PIDDepartureBoardsModel");
const shared_1 = require("../../../shared");
const trace_provider_1 = require("@golemio/core/dist/monitoring/opentelemetry/trace-provider");
const express_validator_1 = require("@golemio/core/dist/shared/express-validator");
const moment_timezone_1 = __importDefault(require("@golemio/core/dist/shared/moment-timezone"));
class V2DepartureBoardsController {
    constructor() {
        this.getDepartureBoard = async (req, res, next) => {
            const queryData = (0, express_validator_1.matchedData)(req);
            const query = shared_1.RopidRouterUtils.mapObjectToDTOInstance(dto_1.DepartureBoardsQueryDTO, queryData);
            const preferredTimezone = shared_1.RopidRouterUtils.getPreferredTimezone(query.preferredTimezone);
            // datetime is valid ISO 8601 (by express validator)
            // it matches:
            // 2020-10-10T10:00:00Z
            // 2020-10-10 10:00:00.00Z
            // 2020-10-10T10:00:00+02
            // 2020-10-10T10:00:00+02:00
            const timezoneDefinedRegexp = /[T ][\d:\.]+([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)/;
            // use timezone from ?preferredTimezone if not set in ?timeFrom
            const timeFrom = query.timeFrom
                ? query.timeFrom.match(timezoneDefinedRegexp)
                    ? (0, moment_timezone_1.default)(query.timeFrom)
                    : moment_timezone_1.default.tz(query.timeFrom, preferredTimezone)
                : undefined;
            const span = (0, trace_provider_1.createChildSpan)("V2DepartureBoardsController.getDepartureBoard");
            try {
                const data = await this.departureBoardsModel.getAll({
                    aswIds: query.aswIds,
                    cisIds: query.cisIds,
                    gtfsIds: query.ids,
                    names: query.names,
                    minutesAfter: query.minutesAfter,
                    minutesBefore: query.minutesBefore,
                    timeFrom,
                    mode: query.mode,
                    order: query.order,
                    filter: query.filter,
                    skip: query.skip,
                    limit: query.limit,
                    offset: query.offset,
                    total: query.total ?? query.limit,
                    timezone: preferredTimezone,
                    includeMetroTrains: query.includeMetroTrains,
                    airCondition: query.airCondition,
                });
                res.status(200).send(shared_1.RopidRouterUtils.mapObjectToDTOInstance(dto_1.DepartureBoardsResponseDTO, data));
            }
            catch (err) {
                next(err);
            }
            finally {
                span?.end();
            }
        };
        this.departureBoardsModel = new PIDDepartureBoardsModel_1.PIDDepartureBoardsModel();
    }
}
exports.V2DepartureBoardsController = V2DepartureBoardsController;
//# sourceMappingURL=V2DepartureBoardsController.js.map