"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.ValidToCalculator = void 0;
const ModuleContainerToken_1 = require("../../../../ioc/ModuleContainerToken");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe");
const const_1 = require("../../../../../const");
const RouteTypeEnums_1 = require("../../../../../helpers/RouteTypeEnums");
let ValidToCalculator = class ValidToCalculator {
    constructor(simpleConfig, geoHelper) {
        this.geoHelper = geoHelper;
        /**
         * Calculates valid_to attribute value for updateDelay worker
         */
        this.getValidToAttribute = (positionToUpdate, tripPositionAttributes, gtfsRouteType, gtfsData, startTimestamp) => {
            if (positionToUpdate.state_position === const_1.StatePositionEnum.CANCELED) {
                return this.getCanceledValidToAttribute(tripPositionAttributes.origin_timestamp);
            }
            else if (gtfsRouteType === RouteTypeEnums_1.GTFSRouteTypeEnum.TRAM) {
                // For TRAM only:
                // For all real stops:
                // return default valid to attribute + the time difference between departure and arrival
                if (positionToUpdate.state_position == const_1.StatePositionEnum.AT_STOP &&
                    positionToUpdate.last_stop_arrival_time != positionToUpdate.last_stop_departure_time &&
                    positionToUpdate.last_stop_arrival_time &&
                    positionToUpdate.last_stop_departure_time) {
                    return new Date(this.getDefaultValidToAttribute(tripPositionAttributes.origin_timestamp).getTime() +
                        (positionToUpdate.last_stop_departure_time.getTime() - positionToUpdate.last_stop_arrival_time.getTime()));
                }
                // For final positions:
                // return default valid to attribute + the time difference between (next trip departure) and arrival
                if (positionToUpdate.state_position == const_1.StatePositionEnum.BEFORE_TRACK) {
                    const distance = this.geoHelper.getDistanceInKilometers([tripPositionAttributes.lng, tripPositionAttributes.lat], [gtfsData.stop_times[0].stop.stop_lon, gtfsData.stop_times[0].stop.stop_lat]);
                    if (distance <= 1) {
                        return this.getDefaultValidToAttribute(new Date(Math.max(tripPositionAttributes.origin_timestamp.getTime(), startTimestamp)));
                    }
                }
            }
            // For all other positions:
            return this.getDefaultValidToAttribute(tripPositionAttributes.origin_timestamp);
        };
        /**
         * Calculate default valid_to attribute
         * return 5 minutes + `origin_timestamp`
         */
        this.getDefaultValidToAttribute = (origin_timestamp) => {
            return new Date(origin_timestamp.getTime() + this.defaultAdditionInMs);
        };
        /**
         * Calculate canceled valid_to attribute
         * return 6 hours + `origin_timestamp`
         */
        this.getCanceledValidToAttribute = (origin_timestamp) => {
            return new Date(origin_timestamp.getTime() + this.canceledAdditionInMs);
        };
        const defaultAdditionInMinutes = Number.parseInt(simpleConfig.getValue("module.pid.vehicle-positions.validTo.defaultAdditionInMinutes", "5")); // fallback to 5 minutes
        const canceledAdditionInMinutes = Number.parseInt(simpleConfig.getValue("module.pid.vehicle-positions.validTo.canceledAdditionInMinutes", (6 * 60).toString())); // fallback to 6 hours
        this.defaultAdditionInMs = defaultAdditionInMinutes * 60 * 1000;
        this.canceledAdditionInMs = canceledAdditionInMinutes * 60 * 1000;
    }
};
exports.ValidToCalculator = ValidToCalculator;
exports.ValidToCalculator = ValidToCalculator = __decorate([
    (0, tsyringe_1.injectable)(),
    __param(0, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.SimpleConfig)),
    __param(1, (0, tsyringe_1.inject)(ModuleContainerToken_1.ModuleContainerToken.GeoMeasurementHelper)),
    __metadata("design:paramtypes", [Object, Object])
], ValidToCalculator);
//# sourceMappingURL=ValidToCalculator.js.map