"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.CheckForNewDataTask = void 0;
const RopidGtfsMetadataRepository_1 = require("../../../RopidGtfsMetadataRepository");
const RopidGtfsContainerToken_1 = require("../../../ioc/RopidGtfsContainerToken");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const integration_engine_1 = require("@golemio/core/dist/integration-engine");
const workers_1 = require("@golemio/core/dist/integration-engine/workers");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const constants_1 = require("../constants");
const RopidGtfsFactory_1 = require("./helpers/RopidGtfsFactory");
const CheckForNewDataMessageValidation_1 = require("./schema/CheckForNewDataMessageValidation");
let CheckForNewDataTask = class CheckForNewDataTask extends workers_1.AbstractTask {
    constructor(metadataRepository, ropidGtfsFactory, logger) {
        super(constants_1.TIMETABLE_WORKER_NAME);
        this.metadataRepository = metadataRepository;
        this.ropidGtfsFactory = ropidGtfsFactory;
        this.logger = logger;
        this.queueName = "checkForNewData";
        this.queueTtl = 19 * 60 * 1000; // 19 minutes
        this.schema = CheckForNewDataMessageValidation_1.CheckForNewDataMessageValidation;
    }
    async execute(data) {
        let forceRefresh = !!data.forceRefresh;
        let outdatedDatasetCount = 0;
        const datasetMap = this.ropidGtfsFactory.getDatasetMap();
        const datasetList = Object.keys(datasetMap);
        for (const dataset of datasetList) {
            const serverLastModified = await datasetMap[dataset].datasource.getLastModified();
            const dbLastModified = await this.metadataRepository.getLastModified(dataset);
            const isDeployed = await this.metadataRepository.isDeployed(dataset);
            if (serverLastModified !== dbLastModified.lastModified || !isDeployed) {
                outdatedDatasetCount++;
            }
        }
        if (outdatedDatasetCount !== datasetList.length) {
            this.logger.warn(`RopidGTFS datasets are out of sync`);
        }
        if (outdatedDatasetCount > 0) {
            // NOTE: We are sending all datasets to download even if only some of them are outdated
            // to avoid unwanted side effects (such as empty precomputed tables)
            await integration_engine_1.QueueManager.sendMessageToExchange(this.queuePrefix, "downloadDatasets", {
                datasets: datasetList,
            });
        }
        else {
            this.logger.info("RopidGTFS datasets are up to date");
            if (forceRefresh) {
                await integration_engine_1.QueueManager.sendMessageToExchange(this.queuePrefix, "refreshPrecomputedTables", {});
            }
        }
        await integration_engine_1.QueueManager.sendMessageToExchange(this.queuePrefix, "saveStaticData", {});
    }
};
exports.CheckForNewDataTask = CheckForNewDataTask;
exports.CheckForNewDataTask = CheckForNewDataTask = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.RopidGtfsMetadataRepository)),
    __param(1, (0, tsyringe_1.inject)(RopidGtfsContainerToken_1.RopidGtfsContainerToken.RopidGtfsFactory)),
    __param(2, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.Logger)),
    __metadata("design:paramtypes", [RopidGtfsMetadataRepository_1.RopidGtfsMetadataRepository,
        RopidGtfsFactory_1.RopidGtfsFactory, Object])
], CheckForNewDataTask);
//# sourceMappingURL=CheckForNewDataTask.js.map