"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.JISInfotextStopSuppressionFilter = void 0;
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const MAX_SEVERITY_LEVEL = 2;
let JISInfotextStopSuppressionFilter = class JISInfotextStopSuppressionFilter {
    filterBySeverityLevel(list) {
        const stopsBySeverity = new Map([
            [MAX_SEVERITY_LEVEL, new Set()], // SEVERE
            [1, new Set()], // WARNING
            [0, new Set()], // INFO
        ]);
        let filteredList = [];
        for (const infotext of list) {
            if (!infotext.stops) {
                continue;
            }
            const severity = this.mapSeverityLevelToNumber(infotext.severity_level);
            for (let i = infotext.stops.length - 1; i >= 0; i--) {
                const stop = infotext.stops[i];
                let stopId;
                if ("getDataValue" in stop && typeof stop.getDataValue === "function") {
                    stopId = stop.getDataValue("stop_id");
                }
                else {
                    stopId = stop.stop_id;
                }
                if (severity === MAX_SEVERITY_LEVEL) {
                    stopsBySeverity.get(severity).add(stopId);
                    continue;
                }
                let isStopPresent = this.isStopPresentInHigherSeverityLevels(severity, stopId, stopsBySeverity);
                if (isStopPresent) {
                    infotext.stops.splice(i, 1);
                }
                else {
                    stopsBySeverity.get(severity).add(stopId);
                }
            }
            if (infotext.stops.length > 0) {
                filteredList.push(infotext);
            }
        }
        return filteredList;
    }
    isStopPresentInHigherSeverityLevels(currentSeverity, stopId, stopsBySeverity) {
        for (let i = currentSeverity + 1; i <= MAX_SEVERITY_LEVEL; i++) {
            if (stopsBySeverity.get(i).has(stopId)) {
                return true;
            }
        }
        return false;
    }
    mapSeverityLevelToNumber(severityLevel) {
        switch (severityLevel) {
            case "SEVERE":
                return MAX_SEVERITY_LEVEL;
            case "WARNING":
                return 1;
            case "INFO":
            default:
                return 0;
        }
    }
};
exports.JISInfotextStopSuppressionFilter = JISInfotextStopSuppressionFilter;
exports.JISInfotextStopSuppressionFilter = JISInfotextStopSuppressionFilter = __decorate([
    (0, tsyringe_1.injectable)()
], JISInfotextStopSuppressionFilter);
//# sourceMappingURL=JISInfotextStopSuppressionFilter.js.map