"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlertsTransformation = void 0;
const HTMLUtils_1 = require("../../../../shared/HTMLUtils");
const transformations_1 = require("@golemio/core/dist/integration-engine/transformations");
const AlertCauseMapper_1 = require("../helpers/AlertCauseMapper");
const AlertEffectMapper_1 = require("../helpers/AlertEffectMapper");
class AlertsTransformation extends transformations_1.BaseTransformation {
    constructor(eventsRoutesModel) {
        super();
        this.transform = async (alertEntities) => {
            const transformedEntities = [];
            for (const alertEntity of alertEntities) {
                transformedEntities.push(await this.transformElement(alertEntity));
            }
            return transformedEntities;
        };
        this.transformElement = async (alertEntity) => {
            return {
                id: `alert-${alertEntity.vymi_id}-${alertEntity.vymi_id_dtb}`,
                alert: {
                    activePeriod: [this.getActivePeriod(alertEntity)],
                    informedEntity: await this.getAffectedRoutes(alertEntity),
                    cause: (0, AlertCauseMapper_1.getGtfsCause)(alertEntity.cause ?? ""),
                    causeDetail: {
                        translation: [],
                    },
                    effect: (0, AlertEffectMapper_1.getGtfsEffectByEventType)(Number(alertEntity.event_type ?? 0)),
                    url: {
                        translation: [
                            {
                                text: `https://pid.cz/mimoradnost/?id=${alertEntity.vymi_id}-${alertEntity.vymi_id_dtb}`,
                                language: "cs",
                            },
                        ],
                    },
                    headerText: {
                        translation: [
                            {
                                text: alertEntity.title,
                                language: "cs",
                            },
                        ],
                    },
                    descriptionText: {
                        translation: [
                            {
                                text: this.getDescription(alertEntity),
                                language: "cs",
                            },
                        ],
                    },
                },
            };
        };
        this.getDescription = (alertEntity) => {
            const description = [
                HTMLUtils_1.HTMLUtils.outputPlainText(alertEntity.description) ?? "",
                HTMLUtils_1.HTMLUtils.outputPlainText(alertEntity.dpp_action) ?? "",
                HTMLUtils_1.HTMLUtils.outputPlainText(alertEntity.ropid_action) ?? "",
            ];
            return description.join(" ").replaceAll("\n", " ").trim();
        };
        this.getActivePeriod = (alertEntity) => {
            if (!alertEntity.expiration_date) {
                return {
                    start: new Date(alertEntity.time_from).getTime() / 1000,
                };
            }
            return {
                start: new Date(alertEntity.time_from).getTime() / 1000,
                end: new Date(alertEntity.expiration_date).getTime() / 1000,
            };
        };
        this.name = "GtfsRtAlertTransformation";
        this.eventsRoutesModel = eventsRoutesModel;
    }
    async getAffectedRoutes(alertEntity) {
        const affectedRotues = await this.eventsRoutesModel.findByEventId(alertEntity.vymi_id);
        const routeNames = [];
        for (const route of affectedRotues) {
            routeNames.push({
                routeId: route.gtfs_route_id ?? route.name,
            });
        }
        return routeNames;
    }
}
exports.AlertsTransformation = AlertsTransformation;
//# sourceMappingURL=AlertsTransformation.js.map