"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BusDelayAtStop = void 0;
const const_1 = require("../../../../../../../../const");
const AbstractDelayAtStop_1 = __importDefault(require("../../../compute-positions/strategy/AbstractDelayAtStop"));
class BusDelayAtStop extends AbstractDelayAtStop_1.default {
    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 (this.didVehicleArriveAtTheLastStop(positionToUpdate, context)) {
            positionToUpdate.delay_stop_arrival = this.calcDelayAtStop(position.origin_timestamp.getTime(), positionToUpdate.last_stop_arrival_time);
        }
        else if (positionToUpdate.state_position === const_1.StatePositionEnum.AT_STOP) {
            const delayInSeconds = positionToUpdate.delay ?? position.delay ?? null;
            if (context.atStopStreak.stop_sequence === null) {
                positionToUpdate.delay_stop_arrival = delayInSeconds;
            }
            else if (this.didVehicleDepart(position)) {
                positionToUpdate.delay_stop_departure = delayInSeconds;
            }
        }
        return positionToUpdate;
    }
    didVehicleDepart(position) {
        return position.is_tracked && position.tcp_event === const_1.RegionalBusEventEnum.DEPARTURED;
    }
}
exports.BusDelayAtStop = BusDelayAtStop;
//# sourceMappingURL=BusDelayAtStop.js.map