"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransferBoardIconsResolver = void 0;
const RouteTypeEnums_1 = require("../../../helpers/RouteTypeEnums");
const SPACE_ICON = "space";
const ICONS_SPLIT_PATTERN = /.{2}/g;
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"]);
class TransferBoardIconsResolver {
    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.flatMap(TransferBoardIconsResolver.iconIdToTexts));
        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(icons, routeType, name) {
        const source = icons?.match(ICONS_SPLIT_PATTERN) || [];
        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_ICON);
        }
        return metro.concat(rest);
    }
}
exports.TransferBoardIconsResolver = TransferBoardIconsResolver;
//# sourceMappingURL=TransferBoardIconsResolver.js.map