"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const const_1 = require("../../../../../../../const");
const AbstractDelayAtStop_1 = __importDefault(require("./AbstractDelayAtStop"));
class CommonDelayAtStop extends AbstractDelayAtStop_1.default {
    updateContext(context, positionToUpdate, position) {
        if (!positionToUpdate || !this.isValidPosition(position, 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 (this.didVehicleArrive(position) || this.isValidUntrackedPosition(positionToUpdate)) {
            context.lastPositionLastStop.arrival_delay = positionToUpdate?.delay_stop_arrival ?? context.lastPositionDelay;
        }
        else if (this.didVehicleDepart(position, context)) {
            context.lastPositionLastStop.departure_delay = positionToUpdate?.delay_stop_departure ?? context.lastPositionDelay;
        }
        return context;
    }
    updatePosition(context, positionToUpdate, position) {
        if (!position || !this.isValidPosition(position, positionToUpdate)) {
            return positionToUpdate;
        }
        context = this.resetContext(context, positionToUpdate, position);
        if (this.didVehicleArrive(position) || this.didVehicleArriveAtTheLastStop(positionToUpdate, context)) {
            positionToUpdate.delay_stop_arrival = this.calcDelayAtStop(position.origin_timestamp.getTime(), positionToUpdate.last_stop_arrival_time);
        }
        else if (this.didVehicleDepart(position, context)) {
            positionToUpdate.delay_stop_departure = this.calcDelayAtStop(position.origin_timestamp.getTime(), positionToUpdate.last_stop_departure_time);
        }
        return this.updatePositionStopDelays(positionToUpdate, context);
    }
    isValidPosition(position, positionToUpdate) {
        return this.hasTcpEvent(position) || this.isValidUntrackedPosition(positionToUpdate);
    }
    hasTcpEvent(position) {
        return typeof position?.tcp_event === "string";
    }
    didVehicleArrive(position) {
        return position.is_tracked && position.tcp_event === const_1.TCPEventEnum.ARRIVAL_ANNOUNCED;
    }
    didVehicleDepart(position, context) {
        return (position.is_tracked &&
            position.tcp_event === const_1.TCPEventEnum.DEPARTURED &&
            context.lastPositionLastStop.departure_delay === null);
    }
    updatePositionStopDelays(positionToUpdate, context) {
        const { delay_stop_arrival, delay_stop_departure } = positionToUpdate;
        if ((delay_stop_arrival === null || delay_stop_arrival === undefined) &&
            context.lastPositionLastStop.arrival_delay !== null) {
            positionToUpdate.delay_stop_arrival = context.lastPositionLastStop.arrival_delay;
        }
        if ((delay_stop_departure === null || delay_stop_departure === undefined) &&
            context.lastPositionLastStop.departure_delay !== null) {
            positionToUpdate.delay_stop_departure = context.lastPositionLastStop.departure_delay;
        }
        return positionToUpdate;
    }
}
exports.default = CommonDelayAtStop;
//# sourceMappingURL=CommonDelayAtStop.js.map