import { Duration } from "@zwave-js/core";
import { setTimer } from "@zwave-js/shared";
/** Computes the interpolated current value from a running transition. */
export function getTransitionCurrentValue(transition) {
    const elapsed = Date.now() - transition.startTime;
    const progress = Math.min(elapsed / transition.durationMs, 1);
    return Math.round(transition.startValue
        + (transition.targetValue - transition.startValue) * progress);
}
/** Returns the remaining duration of a running transition. */
export function getTransitionRemainingDuration(transition) {
    const remaining = Math.max(0, transition.durationMs - (Date.now() - transition.startTime));
    return new Duration(Math.ceil(remaining / 1000), "seconds");
}
/**
 * Stops a running transition and returns the interpolated value at
 * the time of stopping.
 */
export function stopTransition(transition) {
    transition.timer.clear();
    const value = getTransitionCurrentValue(transition);
    void transition.onAbort?.();
    return value;
}
/**
 * Starts a value transition with proportional duration based on distance.
 * Returns the new {@link MockTransition}, or `undefined` when the
 * transition completes instantly (travelTime is 0 or current equals target).
 */
export function startTransition(options) {
    const { currentValue, targetValue, duration, supervised, onComplete, onAbort, } = options;
    if (currentValue === targetValue || duration === 0) {
        return undefined;
    }
    const durationMs = Math.round(duration * (Math.abs(targetValue - currentValue) / 99));
    return {
        startValue: currentValue,
        targetValue,
        startTime: Date.now(),
        durationMs,
        supervised,
        onAbort,
        timer: setTimer(onComplete, durationMs).unref(),
    };
}
//# sourceMappingURL=MockTransition.js.map