"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RopidGTFSCisStopsTransformation = void 0;
const transformations_1 = require("@golemio/core/dist/integration-engine/transformations");
const ropid_gtfs_1 = require("../../../schema-definitions/ropid-gtfs");
const helpers_1 = require("@golemio/core/dist/integration-engine/helpers");
class RopidGTFSCisStopsTransformation extends transformations_1.BaseTransformation {
    constructor() {
        super();
        /**
         * Overrides BaseTransformation::transform
         */
        this.transform = async ({ data }) => {
            const res = {
                cis_stop_groups: [],
                cis_stops: [],
            };
            const promises = data.map(async (stopGroup) => {
                const promisesStops = stopGroup.stops.map((stop) => {
                    return {
                        alt_idos_name: stop.altIdosName,
                        cis: stopGroup.cis,
                        id: stop.id,
                        jtsk_x: stop.jtskX,
                        jtsk_y: stop.jtskY,
                        lat: stop.lat,
                        lon: stop.lon,
                        platform: stop.platform,
                        wheelchair_access: stop.wheelchairAccess,
                        zone: stop.zone,
                    };
                });
                res.cis_stops.push(...(await Promise.all(promisesStops)));
                return {
                    avg_jtsk_x: stopGroup.avgJtskX,
                    avg_jtsk_y: stopGroup.avgJtskY,
                    avg_lat: stopGroup.avgLat,
                    avg_lon: stopGroup.avgLon,
                    cis: stopGroup.cis,
                    district_code: stopGroup.districtCode,
                    full_name: stopGroup.fullName,
                    idos_category: stopGroup.idosCategory,
                    idos_name: stopGroup.idosName,
                    municipality: stopGroup.municipality,
                    name: stopGroup.name,
                    node: stopGroup.node,
                    unique_name: stopGroup.uniqueName,
                };
            });
            res.cis_stop_groups = this.getUniqueStopGroups(await Promise.all(promises));
            return res;
        };
        this.transformElement = async () => { };
        this.getUniqueStopGroups = (data) => {
            // duplicity checking
            // TODO is it ok if all duplicate items are removed?
            const uniqueMap = new Map();
            const duplicateCisStopGroups = [];
            data.forEach((item) => {
                if (uniqueMap.has(item.cis)) {
                    duplicateCisStopGroups.push(item.cis);
                    uniqueMap.delete(item.cis);
                }
                else {
                    uniqueMap.set(item.cis, item);
                }
            });
            if (duplicateCisStopGroups.length > 0) {
                helpers_1.log.warn("CIS_STOP_GROUPS contains duplicate cis. Items with this cis was removed: " +
                    JSON.stringify(duplicateCisStopGroups));
            }
            return [...uniqueMap.values()];
        };
        this.name = ropid_gtfs_1.RopidGTFS.name + "CisStops";
    }
}
exports.RopidGTFSCisStopsTransformation = RopidGTFSCisStopsTransformation;
//# sourceMappingURL=RopidGTFSCisStopsTransformation.js.map