/**
 * Handles the dynamic positioning of the candle countdown timer.
 * Ensures the timer slides smoothly from the current candle to the next position.
 */
window.CountdownPositioning = {
    /**
     * Calculates the X position of the timer box.
     * @param {number} candleRightEdge - The pixel X coordinate of the current candle's right edge.
     * @param {number} currentElapsed - Seconds elapsed in the current candle.
     * @param {number} totalDuration - Total duration of the candle in seconds (e.g., 60).
     * @returns {number} The calculated X position for the timer.
     */
    calculatePosition: function (candleRightEdge, currentElapsed, totalDuration) {
        // Validation to prevent errors
        if (typeof candleRightEdge !== 'number' || typeof currentElapsed !== 'number' || !totalDuration) {
            console.warn('CountdownPositioning: Invalid inputs', { candleRightEdge, currentElapsed, totalDuration });
            return candleRightEdge + 50; // Fallback safe position
        }

        const startBuffer = 25; // Start 25px away from the candle
        const travelDistance = 50; // The width of one candle interval (100px / 2)

        // Calculate progress (0.0 to 1.0)
        let progress = currentElapsed / totalDuration;

        // Clamp progress to 0-1 range to prevent overshooting
        progress = Math.max(0, Math.min(1, progress));

        // Calculate dynamic offset
        // At 0% progress: offset is 5px
        // At 100% progress: offset is 55px (5 + 50)
        const currentOffset = startBuffer + (travelDistance * progress);

        return candleRightEdge + currentOffset;
    }
};
