"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("@golemio/core/dist/integration-engine/config");
const const_1 = require("../../../../../../../const");
const AbstractDelayAtStop_1 = __importDefault(require("./AbstractDelayAtStop"));
class MetroDelayAtStop extends AbstractDelayAtStop_1.default {
    constructor() {
        super();
        this.metroArrivalDelayAddition = config_1.config.vehiclePositions.metroArrivalDelayAddition;
        this.metroDepartureDelayAddition = config_1.config.vehiclePositions.metroDepartureDelayAddition;
    }
    updateContext(context, positionToUpdate, position) {
        if (!positionToUpdate || (!position.is_tracked && !this.isValidUntrackedPosition(positionToUpdate))) {
            return context;
        }
        context = this.resetContext(context, positionToUpdate, position);
        if (!context.lastPositionLastStop.sequence) {
            context.lastPositionLastStop.sequence = positionToUpdate.last_stop_sequence ?? position.last_stop_sequence ?? null;
        }
        if (positionToUpdate.state_position === const_1.StatePositionEnum.AT_STOP ||
            positionToUpdate.state_position === const_1.StatePositionEnum.ON_TRACK ||
            this.isValidUntrackedPosition(positionToUpdate)) {
            context.lastPositionLastStop.arrival_delay = positionToUpdate.delay_stop_arrival ?? null;
            context.lastPositionLastStop.departure_delay = positionToUpdate.delay_stop_departure ?? null;
        }
        return context;
    }
    updatePosition(context, positionToUpdate, position) {
        if (!position || (!position.is_tracked && !this.isValidUntrackedPosition(positionToUpdate))) {
            return positionToUpdate;
        }
        context = this.resetContext(context, positionToUpdate, position);
        positionToUpdate.delay_stop_arrival = context.lastPositionLastStop.arrival_delay;
        positionToUpdate.delay_stop_departure = context.lastPositionLastStop.departure_delay;
        if ((positionToUpdate.state_position === const_1.StatePositionEnum.AT_STOP && context.atStopStreak.stop_sequence === null) ||
            this.didVehicleArriveAtTheLastStop(positionToUpdate, context)) {
            if (positionToUpdate.last_stop_sequence === 1) {
                // first stop
                positionToUpdate.delay_stop_arrival = null;
            }
            else {
                const delayAtStopInSeconds = this.calcDelayAtStop(position.origin_timestamp.getTime(), positionToUpdate.last_stop_arrival_time);
                positionToUpdate.delay_stop_arrival = delayAtStopInSeconds
                    ? this.roundToNearestMultipleOfFive(delayAtStopInSeconds + this.metroArrivalDelayAddition)
                    : null;
            }
        }
        else if (positionToUpdate.state_position === const_1.StatePositionEnum.ON_TRACK && context.atStopStreak.stop_sequence) {
            const delayAtStopInSeconds = this.calcDelayAtStop(position.origin_timestamp.getTime(), positionToUpdate.last_stop_departure_time);
            positionToUpdate.delay_stop_departure = delayAtStopInSeconds
                ? this.roundToNearestMultipleOfFive(delayAtStopInSeconds + this.metroDepartureDelayAddition)
                : null;
        }
        return positionToUpdate;
    }
    // [-2,2] -> 0
    // [3,7] -> 5
    // [8,12] -> 10
    // [13,17] -> 15
    // etc.
    roundToNearestMultipleOfFive(value) {
        return Math.round(value / 5) * 5;
    }
}
exports.default = MetroDelayAtStop;
//# sourceMappingURL=MetroDelayAtStop.js.map