"use strict";
/**
 * Router /WEB LAYER/: maps routes to specific controller functions, passes request parameters and handles responses.
 * Handles web logic (http request, response). Sets response headers, handles error responses.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.v2PidRouter = exports.V2PIDRouter = void 0;
const V2DepartureBoardsController_1 = require("../../controllers/v2/V2DepartureBoardsController");
const ParamValidatorManager_1 = require("../../helpers/ParamValidatorManager");
const constants_1 = require("../../../shared/constants");
const AbstractRouter_1 = require("@golemio/core/dist/helpers/routing/AbstractRouter");
const Validation_1 = require("@golemio/core/dist/output-gateway/Validation");
const ioc_1 = require("@golemio/core/dist/output-gateway/ioc");
const express_1 = require("@golemio/core/dist/shared/express");
const express_validator_1 = require("@golemio/core/dist/shared/express-validator");
const const_1 = require("../../../../const");
class V2PIDRouter extends AbstractRouter_1.AbstractRouter {
    constructor() {
        super(constants_1.RouteVersion.v2, "pid");
        this.router = (0, express_1.Router)();
        this.initRoutes = () => {
            this.registerDepartureBoardsRoutes();
        };
        this.registerDepartureBoardsRoutes = () => {
            const { preferredTimezone, departureBoards: { departureMode, order, filter, skip }, } = constants_1.ValidationArrays;
            const validation = [
                (0, express_validator_1.query)("aswIds").optional(),
                (0, express_validator_1.query)("cisIds").optional(),
                (0, express_validator_1.query)("ids").optional(),
                (0, express_validator_1.query)("names").optional(),
                // at least one of aswIds, cisIds, ids or names is required
                (0, express_validator_1.oneOf)([(0, express_validator_1.query)("aswIds").exists(), (0, express_validator_1.query)("cisIds").exists(), (0, express_validator_1.query)("ids").exists(), (0, express_validator_1.query)("names").exists()]),
                // the validation of aswIds, cisIds, ids and names present in oneOf above
                (0, express_validator_1.query)("aswIds").optional().custom(ParamValidatorManager_1.ParamValidatorManager.getAswIdsValidator()),
                (0, express_validator_1.query)("cisIds").optional().isInt({ min: const_1.PG_INT_MIN, max: const_1.PG_INT_MAX }),
                (0, express_validator_1.query)("ids").optional().not().isEmpty({ ignore_whitespace: true }),
                (0, express_validator_1.query)("names").optional().not().isEmpty({ ignore_whitespace: true }),
                (0, express_validator_1.query)("minutesBefore")
                    .optional()
                    .isInt(process.env.NODE_ENV === "test"
                    ? {}
                    : { lt: const_1.DATA_RETENTION_IN_MINUTES + 1, gt: -1 * (const_1.GTFS_CALENDAR_LIMIT_IN_MINUTES + 1) })
                    .not()
                    .isArray(),
                (0, express_validator_1.query)("minutesAfter")
                    .optional()
                    .isInt({
                    lt: const_1.GTFS_CALENDAR_LIMIT_IN_MINUTES + 1,
                    gt: -1 * (const_1.GTFS_CALENDAR_LIMIT_IN_MINUTES + const_1.DATA_RETENTION_IN_MINUTES + 1),
                })
                    .not()
                    .isArray(),
                (0, express_validator_1.query)("preferredTimezone").optional().isIn(preferredTimezone).not().isArray(),
                (0, express_validator_1.query)("mode").optional().isIn(departureMode),
                (0, express_validator_1.query)("order").optional().isIn(order).not().isArray(),
                (0, express_validator_1.query)("filter").optional().isIn(filter).not().isArray(),
                (0, express_validator_1.query)("skip").optional().isIn(skip),
                (0, express_validator_1.query)("timeFrom").optional().isISO8601().custom(ParamValidatorManager_1.ParamValidatorManager.getTimeFromValidator()).not().isArray(),
                (0, express_validator_1.query)("includeMetroTrains").optional().isBoolean().not().isArray(),
                (0, express_validator_1.query)("airCondition").optional().isBoolean().not().isArray(),
                (0, express_validator_1.query)("total").optional().isInt().not().isArray(),
            ];
            this.router.get("/departureboards", validation, ...this.commonMiddleware("PIDDepartureBoards"), Validation_1.checkErrors, 
            // max-age 5 seconds, stale-while-revalidate 5 seconds
            this.cacheHeaderMiddleware.getMiddleware(5, 5), this.departureBoardsController.getDepartureBoard);
        };
        this.commonMiddleware = (name) => [(0, Validation_1.paginationLimitMiddleware)(name)];
        this.cacheHeaderMiddleware = ioc_1.OutputGatewayContainer.resolve(ioc_1.ContainerToken.CacheHeaderMiddleware);
        this.departureBoardsController = new V2DepartureBoardsController_1.V2DepartureBoardsController();
        this.initRoutes();
    }
}
exports.V2PIDRouter = V2PIDRouter;
const v2PidRouter = new V2PIDRouter();
exports.v2PidRouter = v2PidRouter;
//# sourceMappingURL=V2PIDRouter.js.map