"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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RopidGTFSTransformation = void 0;
const ropid_gtfs_1 = require("../../../schema-definitions/ropid-gtfs");
const integration_engine_1 = require("@golemio/core/dist/integration-engine");
const transformations_1 = require("@golemio/core/dist/integration-engine/transformations");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const csv_parser_1 = __importDefault(require("csv-parser"));
const csv_stringify_1 = __importDefault(require("csv-stringify"));
const stream_1 = require("stream");
let RopidGTFSTransformation = class RopidGTFSTransformation extends transformations_1.BaseTransformation {
    constructor() {
        super();
        this.transform = async (input) => {
            // create object by table structure and fill its values with null
            const tableToObjectFilledWithNulls = Object.keys(ropid_gtfs_1.RopidGTFS[input.name].outputSequelizeAttributes)
                .filter((column) => !column.startsWith("computed_"))
                .reduce((acc, curr) => ((acc[curr] = null), acc), {});
            const addColumns = new stream_1.Transform({
                objectMode: true,
                transform(chunk, _encoding, callback) {
                    this.push({
                        // prepare object to be same as db table (number and order of columns)
                        ...tableToObjectFilledWithNulls,
                        // filter redundant properties and fill data to prepared structure
                        ...Object.keys(chunk)
                            .filter((key) => Object.keys(tableToObjectFilledWithNulls).includes(key))
                            .reduce((obj, key) => {
                            obj[key] = chunk[key];
                            return obj;
                        }, {}),
                    });
                    callback();
                },
            });
            return (0, stream_1.pipeline)(input.sourceStream, (0, csv_parser_1.default)(), addColumns, (0, csv_stringify_1.default)({ header: true }), (err) => {
                if (err) {
                    integration_engine_1.log.error(`Datasets: transformation error (${input.name}): ${err.toString()}`);
                    throw err;
                }
                else {
                    integration_engine_1.log.info(`Datasets: transformation success (${input.name})`);
                }
            });
        };
        this.transformElement = () => { };
        this.name = ropid_gtfs_1.RopidGTFS.name;
    }
};
exports.RopidGTFSTransformation = RopidGTFSTransformation;
exports.RopidGTFSTransformation = RopidGTFSTransformation = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], RopidGTFSTransformation);
//# sourceMappingURL=RopidGTFSTransformation.js.map