"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RopidGtfsFactory = void 0;
const DatasetEnum_1 = require("../../../../helpers/DatasetEnum");
const Di_1 = require("../../../../ioc/Di");
const RopidGtfsContainerToken_1 = require("../../../../ioc/RopidGtfsContainerToken");
const RopidGTFSCisStopsTransformation_1 = require("../../../../transformations/RopidGTFSCisStopsTransformation");
const RopidGTFSOisTransformation_1 = require("../../../../transformations/RopidGTFSOisTransformation");
const RopidGTFSTransformation_1 = require("../../../../transformations/RopidGTFSTransformation");
const RopidGtfsRouteSubAgencyTransformation_1 = require("../../../../transformations/RopidGtfsRouteSubAgencyTransformation");
const ropid_gtfs_1 = require("../../../../../../schema-definitions/ropid-gtfs");
const integration_engine_1 = require("@golemio/core/dist/integration-engine");
const RetryDataSource_1 = require("@golemio/core/dist/integration-engine/datasources/RetryDataSource");
const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const HelperTypes_1 = require("./HelperTypes");
let RopidGtfsFactory = class RopidGtfsFactory {
    constructor() {
        // Ropid GTFS
        this.dataSourceGtfs = new RetryDataSource_1.RetryDataSource(ropid_gtfs_1.RopidGTFS.name + "DataSource", new integration_engine_1.FTPProtocolStrategy({
            filename: integration_engine_1.config.datasources.RopidGTFSFilename,
            targetType: integration_engine_1.FTPTargetType.COMPRESSED,
            returnType: integration_engine_1.FTPReturnType.Stream,
            path: integration_engine_1.config.datasources.RopidGTFSPath,
            url: integration_engine_1.config.datasources.RopidFTP,
            whitelistedFiles: [
                "agency.txt",
                "calendar.txt",
                "calendar_dates.txt",
                "shapes.txt",
                "stop_times.txt",
                "stops.txt",
                "routes.txt",
                "route_sub_agencies.txt",
                "trips.txt",
            ],
            encoding: "utf8",
        }), new integration_engine_1.JSONDataTypeStrategy({ resultsPath: "" }), null);
        this.transformationGtfs = new RopidGTFSTransformation_1.RopidGTFSTransformation();
        // Ropid GTFS Run Numbers Datasource
        this.dataSourceRunNumbers = new RetryDataSource_1.RetryDataSource(ropid_gtfs_1.RopidGTFS.run_numbers.name + "DataSource", new integration_engine_1.FTPProtocolStrategy({
            filename: integration_engine_1.config.datasources.RopidGTFSRunNumbersFilename,
            returnType: integration_engine_1.FTPReturnType.Stream,
            path: integration_engine_1.config.datasources.RopidGTFSRunNumbersPath,
            url: integration_engine_1.config.datasources.RopidFTP,
            encoding: "utf8",
        }), new integration_engine_1.JSONDataTypeStrategy({ resultsPath: "" }), null);
        // Ropid GTFS CIS STOPS Datasource
        const cisStopsTypeStrategy = new integration_engine_1.JSONDataTypeStrategy({ resultsPath: "stopGroups" });
        cisStopsTypeStrategy.setFilter((item) => item.cis !== 0);
        this.dataSourceCisStops = new RetryDataSource_1.RetryDataSource(ropid_gtfs_1.RopidGTFS.name + "CisStops", new integration_engine_1.FTPProtocolStrategy({
            filename: integration_engine_1.config.datasources.RopidGTFSCisStopsFilename,
            path: integration_engine_1.config.datasources.RopidGTFSCisStopsPath,
            url: integration_engine_1.config.datasources.RopidFTP,
            encoding: "utf8",
        }), cisStopsTypeStrategy, null);
        this.transformationCisStops = new RopidGTFSCisStopsTransformation_1.RopidGTFSCisStopsTransformation();
        // Ropid GTFS Ois Datasource
        this.dataSourceOisMapping = new RetryDataSource_1.RetryDataSource(ropid_gtfs_1.RopidGTFS.name + "Ois", new integration_engine_1.FTPProtocolStrategy({
            filename: integration_engine_1.config.datasources.RopidGTFSOisFilename,
            path: integration_engine_1.config.datasources.RopidGTFSOisPath,
            url: integration_engine_1.config.datasources.RopidFTP,
            encoding: "utf8",
        }), new integration_engine_1.JSONDataTypeStrategy({ resultsPath: "" }), new golemio_validator_1.JSONSchemaValidator(ropid_gtfs_1.RopidGTFS.ois.name + "DataSource", ropid_gtfs_1.RopidGTFS.ois.datasourceJsonSchema));
        this.transformationOisMapping = new RopidGTFSOisTransformation_1.RopidGTFSOisMappingTransformation();
        this.datasetMap = {
            [DatasetEnum_1.DatasetEnum.PID_GTFS]: {
                datasource: this.dataSourceGtfs,
                transformation: this.transformationGtfs,
                cacheContentType: HelperTypes_1.CacheContentType.CSV,
            },
            [DatasetEnum_1.DatasetEnum.RUN_NUMBERS]: {
                datasource: this.dataSourceRunNumbers,
                transformation: this.transformationGtfs,
                cacheContentType: HelperTypes_1.CacheContentType.CSV,
            },
            [DatasetEnum_1.DatasetEnum.CIS_STOPS]: {
                datasource: this.dataSourceCisStops,
                transformation: this.transformationCisStops,
                cacheContentType: HelperTypes_1.CacheContentType.JSON,
            },
            [DatasetEnum_1.DatasetEnum.OIS_MAPPING]: {
                datasource: this.dataSourceOisMapping,
                transformation: this.transformationOisMapping,
                cacheContentType: HelperTypes_1.CacheContentType.JSON,
            },
        };
        this.staticFileRedisRepository = Di_1.RopidGtfsContainer.resolve(RopidGtfsContainerToken_1.RopidGtfsContainerToken.StaticFileRedisRepository);
        this.ropidGtfsRouteSubAgencyTransformation = new RopidGtfsRouteSubAgencyTransformation_1.RopidGtfsRouteSubAgencyTransformation();
    }
    getDatasetMap() {
        return this.datasetMap;
    }
    getTransformation(dataset) {
        return this.datasetMap[dataset].transformation;
    }
    getSaveMethod(dataset, modelName) {
        if (modelName === "route_sub_agencies") {
            return HelperTypes_1.SaveMethod.SEQUELIZE;
        }
        switch (dataset) {
            case DatasetEnum_1.DatasetEnum.PID_GTFS:
            case DatasetEnum_1.DatasetEnum.RUN_NUMBERS:
                return HelperTypes_1.SaveMethod.STREAM;
            default:
                return HelperTypes_1.SaveMethod.SEQUELIZE;
        }
    }
    async loadAndTransformDataset(inputData, modelName) {
        if (inputData.name === "route_sub_agencies") {
            const data = await this.staticFileRedisRepository.getCsvAsJson(inputData.filepath);
            return this.ropidGtfsRouteSubAgencyTransformation.transformArray(data);
        }
        else if (inputData.dataset === DatasetEnum_1.DatasetEnum.PID_GTFS || inputData.dataset === DatasetEnum_1.DatasetEnum.RUN_NUMBERS) {
            const sourceStream = this.staticFileRedisRepository.getReadableStream(inputData.filepath);
            return await this.transformationGtfs.transform({
                sourceStream,
                name: modelName,
            });
        }
        else {
            const data = await this.staticFileRedisRepository.get(inputData.filepath);
            return await this.datasetMap[inputData.dataset].transformation.transform({
                data: this.datasetMap[inputData.dataset].cacheContentType === HelperTypes_1.CacheContentType.JSON ? JSON.parse(data) : data,
                name: inputData.name,
            });
        }
    }
};
exports.RopidGtfsFactory = RopidGtfsFactory;
exports.RopidGtfsFactory = RopidGtfsFactory = __decorate([
    (0, tsyringe_1.injectable)(),
    __metadata("design:paramtypes", [])
], RopidGtfsFactory);
//# sourceMappingURL=RopidGtfsFactory.js.map