"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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaveStaticDataTask = void 0;
const workers_1 = require("@golemio/core/dist/integration-engine/workers");
const StaticDataSourceFactory_1 = require("../../../datasources/StaticDataSourceFactory");
const RopidGtfsContainerToken_1 = require("../../../ioc/RopidGtfsContainerToken");
const DeparturesDirectionRepository_1 = require("./data-access/DeparturesDirectionRepository");
const MetroRailtrackDataTransformation_1 = require("./transformations/MetroRailtrackDataTransformation");
const MetroRailtrackGPSRepository_1 = require("./data-access/MetroRailtrackGPSRepository");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const DeparturesDirectionTransformation_1 = require("./transformations/DeparturesDirectionTransformation");
const constants_1 = require("../constants");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
let SaveStaticDataTask = class SaveStaticDataTask extends workers_1.AbstractEmptyTask {
    constructor(staticDataSourceFactory, departuresDirectionRepository, railtrackDataTransformation, railtrackGPSRepository, departuresDirectionTransformation) {
        super(constants_1.TIMETABLE_WORKER_NAME);
        this.staticDataSourceFactory = staticDataSourceFactory;
        this.departuresDirectionRepository = departuresDirectionRepository;
        this.railtrackDataTransformation = railtrackDataTransformation;
        this.railtrackGPSRepository = railtrackGPSRepository;
        this.departuresDirectionTransformation = departuresDirectionTransformation;
        this.queueName = "saveStaticData";
        this.queueTtl = 5 * 60 * 1000; // 5 minutes
    }
    async execute() {
        try {
            const departuresDirections = await this.staticDataSourceFactory.getDataSource("departuresDirections").getAll();
            const departuresDirectionsDto = this.departuresDirectionTransformation.transformArray(departuresDirections);
            await this.departuresDirectionRepository.saveData(departuresDirectionsDto);
        }
        catch (err) {
            if (err instanceof golemio_errors_1.AbstractGolemioError) {
                throw err;
            }
            throw new golemio_errors_1.GeneralError("Error while refreshing departures directions", this.constructor.name, err);
        }
        try {
            const metroRailTrack = await this.staticDataSourceFactory.getDataSource("metroRailTrack").getAll();
            const railtrackGPSData = this.railtrackDataTransformation.transformArray(metroRailTrack);
            await this.railtrackGPSRepository.saveData(railtrackGPSData);
        }
        catch (err) {
            if (err instanceof golemio_errors_1.AbstractGolemioError) {
                throw err;
            }
            throw new golemio_errors_1.GeneralError("Error while refreshing metro rail tracks", this.constructor.name, err);
        }
    }
};
exports.SaveStaticDataTask = SaveStaticDataTask;
exports.SaveStaticDataTask = SaveStaticDataTask = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.StaticDataSourceFactory)),
    __param(1, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.DeparturesDirectionRepository)),
    __param(2, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.MetroRailtrackDataTransformation)),
    __param(3, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.MetroRailtrackGPSRepository)),
    __param(4, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.DeparturesDirectionTransformation)),
    __metadata("design:paramtypes", [StaticDataSourceFactory_1.StaticDataSourceFactory,
        DeparturesDirectionRepository_1.DeparturesDirectionRepository,
        MetroRailtrackDataTransformation_1.MetroRailtrackDataTransformation,
        MetroRailtrackGPSRepository_1.MetroRailtrackGPSRepository,
        DeparturesDirectionTransformation_1.DeparturesDirectionTransformation])
], SaveStaticDataTask);
//# sourceMappingURL=SaveStaticDataTask.js.map