// Time calculation and formatting utilities

// Global variable to track total seconds across the application
let totalSeconds = 0;
// Track which steps have already been counted to avoid double-counting
let countedSteps = new Set();

// Function to get time difference text between two dates
function getTimeDifferenceText(createdDateTime, updatedDateTime, stepIdentifier = null) {
    // Require both timestamps; otherwise, treat as zero-duration
    if (createdDateTime && updatedDateTime) {
        const created = new Date(createdDateTime);
        const updated = new Date(updatedDateTime);

        // Guard against invalid dates
        if (isNaN(created.getTime()) || isNaN(updated.getTime())) {
            return "0s";
        }

        // Compute forward difference; clamp to zero if negative
        let diffInSeconds = Math.floor((updated - created) / 1000);
        if (!isFinite(diffInSeconds) || diffInSeconds < 0) diffInSeconds = 0;

        // Only add to totalSeconds if this step hasn't been counted yet
        if (stepIdentifier && !countedSteps.has(stepIdentifier)) {
            totalSeconds = totalSeconds + diffInSeconds;
            countedSteps.add(stepIdentifier);
        } else if (!stepIdentifier) {
            // If no identifier provided, add anyway (for backward compatibility)
            totalSeconds = totalSeconds + diffInSeconds;
        }

        if (diffInSeconds < 60) {
            return diffInSeconds === 1 ? "1s" : `${diffInSeconds}s`;
        }

        const diffInMinutes = Math.floor(diffInSeconds / 60);
        if (diffInMinutes < 60) {
            return diffInMinutes === 1 ? "1min" : `${diffInMinutes}min`;
        }

        const diffInHours = Math.floor(diffInMinutes / 60);
        if (diffInHours < 24) {
            return diffInHours === 1 ? "1hr" : `${diffInHours}hrs`;
        }

        const diffInDays = Math.floor(diffInHours / 24);
        return diffInDays === 1 ? "1day" : `${diffInDays}days`;
    }
    else {
        // Without an end time, we cannot compute a duration
        return "0s";
    }
}

// Function to format total seconds to human readable time text
function formatSecondsToTimeText(totalSeconds) {
    totalSeconds = Math.floor(totalSeconds);

    const hours = Math.floor(totalSeconds / 3600);
    totalSeconds %= 3600;

    const minutes = Math.floor(totalSeconds / 60);
    const seconds = totalSeconds % 60;

    let parts = [];

    if (hours > 0) parts.push(`${hours} ${hours === 1 ? 'Hour' : 'Hours'}`);
    if (minutes > 0) parts.push(`${minutes} ${minutes === 1 ? 'Minute' : 'Minutes'}`);
    if (seconds > 0 || parts.length === 0) parts.push(`${seconds} ${seconds === 1 ? 'Second' : 'Seconds'}`);

    return parts.join(' ');
}

// Function to get the current total seconds
function getTotalSeconds() {
    return totalSeconds;
}

// Function to set the total seconds (useful for initialization)
function setTotalSeconds(seconds) {
    totalSeconds = seconds;
}

// Function to reset total seconds
function resetTotalSeconds() {
    totalSeconds = 0;
    countedSteps.clear();
}
