"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlertsGenerator = void 0;
const JISEventsGtfsRtTransformation_1 = require("../../../../jis/transformations/JISEventsGtfsRtTransformation");
const ovapi_gtfs_realtime_bindings_1 = require("@golemio/ovapi-gtfs-realtime-bindings");
const AlertsTransformation_1 = require("../transformations/AlertsTransformation");
class AlertsGenerator {
    constructor(config, gtfsRtRedisRepository, jisEventsRepository, vymiEventsRepository, vymiRoutesRepository) {
        this.config = config;
        this.gtfsRtRedisRepository = gtfsRtRedisRepository;
        this.jisEventsRepository = jisEventsRepository;
        this.vymiEventsRepository = vymiEventsRepository;
        this.alertsTransformation = new AlertsTransformation_1.AlertsTransformation(vymiRoutesRepository);
        this.jisEventsTransformation = new JISEventsGtfsRtTransformation_1.JISEventsGtfsRtTransformation();
        this.jisAllowedOrgs = this.config.getValue("module.pid.jis.gtfsRt.events.allowedOrgs.*");
    }
    async generateAlerts(feedHeader) {
        const alertsMessage = ovapi_gtfs_realtime_bindings_1.transit_realtime.FeedMessage.create({ header: feedHeader });
        const [alertEntities, jisIncidents] = await Promise.all([
            this.vymiEventsRepository.getCurrentAlerts(feedHeader.timestamp ? new Date(feedHeader.timestamp * 1000) : new Date()),
            this.jisEventsRepository.getCurrentIncidents(this.jisAllowedOrgs),
        ]);
        const transformedAlerts = await this.alertsTransformation.transform(alertEntities);
        const transformedJISIncidents = this.jisEventsTransformation.transformArray(jisIncidents);
        const allIncidents = [...transformedJISIncidents, ...transformedAlerts];
        for (const alert of allIncidents) {
            const feedEntity = ovapi_gtfs_realtime_bindings_1.transit_realtime.FeedEntity.fromObject(alert);
            alertsMessage.entity.push(feedEntity);
        }
        if (ovapi_gtfs_realtime_bindings_1.transit_realtime.FeedMessage.verify(alertsMessage) === null) {
            const buffer = ovapi_gtfs_realtime_bindings_1.transit_realtime.FeedMessage.encode(alertsMessage).finish();
            await this.gtfsRtRedisRepository.set("alerts" + "_timestamp", alertsMessage.header.timestamp);
            await this.gtfsRtRedisRepository.hset("alerts.pb", buffer.toString("binary"));
        }
    }
}
exports.AlertsGenerator = AlertsGenerator;
//# sourceMappingURL=AlertsGenerator.js.map