"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransferBoardIconsResolver = void 0;
const RouteTypeEnums_1 = require("../../../helpers/RouteTypeEnums");
const iconTranslateMap = new Map([
    ["Ma", ["metro", "a"]],
    ["Mb", ["metro", "b"]],
    ["Mc", ["metro", "c"]],
    ["Md", ["metro", "d"]],
    ["Sb", ["train", "s_bahn"]],
    ["Ra", ["train"]],
    ["Ap", ["airport"]],
    // more sparse transport types
    ["Fu", ["funicular"]],
    ["Fe", ["ferry"]],
    ["Tb", ["trolleybus"]],
]);
const deduplicateMetroLineNames = new Set(["a", "b", "c", "d"]);
const metroSpecificIconId = new Set(["Ma", "Mb", "Mc", "Md"]);
const iconSourceSplitter = /.{1,2}/g;
const stopNameToIcon = new Map([
    ["Kačerov", "McSbRa"],
    ["Smíchovské nádraží", "MbSbRa"],
    ["Skalka", "Ma"],
    ["Želivského", "Ma"],
    ["Florenc", "MbMc"],
    ["Kobylisy", "Mc"],
    ["Letňany", "Mc"],
    ["Háje", "Mc"],
    ["Radlická", "Mb"],
    ["Nádraží Braník", "SbRa"],
    ["Praha hl.n.", "Mc"],
    ["Terminál 1", "Ap"],
    ["Terminál 2", "Ap"],
    ["Terminál 3", "Ap"],
    ["Letiště", "Ap"],
    ["Letiště / Airport", "Ap"],
    ["Praha Masarykovo nádraží", "MbSbRa"],
]);
const stopNameToIconsList = new Map();
stopNameToIcon.forEach((icon, key, map) => {
    stopNameToIconsList.set(key, icon.match(iconSourceSplitter));
});
class TransferBoardIconsResolver {
    static enhanceInplaceWithStaticIcons(rows) {
        for (const [index, row] of rows.entries()) {
            row.icons = TransferBoardIconsResolver.headsignToIconsList(row.trip.headsign, row.route.type, row.route.short_name);
            rows[index] = row;
        }
    }
    static iconIdToTexts(id) {
        return iconTranslateMap.get(id) ?? [];
    }
    static onlyMetro(ids) {
        return ids.filter((s) => metroSpecificIconId.has(s));
    }
    static withoutMetro(ids) {
        return ids.filter((s) => !metroSpecificIconId.has(s));
    }
    static translateToIcons(ids, routeShortName) {
        // using Set to eliminate duplicity of "metro" word in ["metro", "a", "metro", "b", ...] => metro, a, b
        // or similar patterns as "train", "s_bahn" duplicating "train", "train", "s_bahn" => train, s_bahn
        const result = new Set(ids.map(TransferBoardIconsResolver.iconIdToTexts).flat());
        if (routeShortName && deduplicateMetroLineNames.has(routeShortName.toLowerCase())) {
            result.delete(routeShortName.toLowerCase());
            if (result.size === 1) {
                // it was the only "metro" + "letter", remove its metro keyword too
                result.delete("metro");
            }
        }
        return [...result];
    }
    static headsignToIconsList(headsign, routeType, name) {
        const source = stopNameToIconsList.get(headsign) || [];
        if (!source.length) {
            return [];
        }
        const metro = [];
        const rest = [];
        const filterLineName = routeType === RouteTypeEnums_1.GTFSRouteTypeEnum.METRO ? name : null;
        metro.push(...TransferBoardIconsResolver.translateToIcons(TransferBoardIconsResolver.onlyMetro(source), filterLineName));
        rest.push(...TransferBoardIconsResolver.translateToIcons(TransferBoardIconsResolver.withoutMetro(source), null));
        if (metro.length && rest.length) {
            metro.push("space");
        }
        return metro.concat(rest);
    }
}
exports.TransferBoardIconsResolver = TransferBoardIconsResolver;
//# sourceMappingURL=TransferBoardIconsResolver.js.map