/**
 * Utility functions for formatting data
 */

/**
 * Format a status string to be more user-friendly
 * @param {string} status - The status string
 * @returns {string} The formatted status string
 */
function formatStatus(status) {
    return ResearchStates.formatStatus(status);
}

/**
 * Format a research mode string to be more user-friendly
 * @param {string} mode - The mode string
 * @returns {string} The formatted mode string
 */
function formatMode(mode) {
    switch(mode) {
        case 'quick': return 'Quick Summary';
        case 'detailed': return 'Detailed Report';
        default: return mode.charAt(0).toUpperCase() + mode.slice(1);
    }
}

/**
 * Format a date string with optional duration
 * @param {string} date - The date string in ISO format
 * @param {number|null} duration - Optional duration in seconds
 * @returns {string} The formatted date string
 */
function formatDate(date, duration = null) {
    if (!date) return 'Unknown';

    try {
        const dateObj = new Date(date);
        const options = {
            year: 'numeric',
            month: 'short',
            day: 'numeric',
            hour: '2-digit',
            minute: '2-digit'
        };

        let formattedDate = dateObj.toLocaleDateString('en-US', options);

        if (duration) {
            // Format the duration
            const minutes = Math.floor(duration / 60);
            const seconds = duration % 60;

            if (minutes > 0) {
                formattedDate += ` (${minutes}m ${seconds}s)`;
            } else {
                formattedDate += ` (${seconds}s)`;
            }
        }

        return formattedDate;
    } catch (e) {
        SafeLogger.error('Error formatting date:', e);
        return date; // Return the original date if there's an error
    }
}

/**
 * Format a duration in seconds to a readable string
 * @param {number} seconds - Duration in seconds
 * @returns {string} Formatted duration string
 */
function formatDuration(seconds) {
    if (!seconds || isNaN(seconds)) return 'Unknown';

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

    if (minutes === 0) {
        return `${remainingSeconds}s`;
    }
    return `${minutes}m ${remainingSeconds}s`;
}

/**
 * Capitalize the first letter of a string
 * @param {string} string - The string to capitalize
 * @returns {string} The capitalized string
 */
function capitalizeFirstLetter(string) {
    if (!string) return '';
    return string.charAt(0).toUpperCase() + string.slice(1);
}

/**
 * Format a number with thousands separators
 * @param {number} num - The number to format
 * @returns {string} Formatted number
 */
function formatNumber(num) {
    if (num === null || num === undefined) return '0';
    return num.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}

/**
 * Format a dollar amount, using more decimal places for small numbers.
 * @param {number} amount - Amount in dollars
 * @returns {string} Formatted currency string (e.g. "$0.000123", "$12.34")
 */
function formatCurrency(amount) {
    if (amount < 0.01) {
        return `$${amount.toFixed(6)}`;
    } else if (amount < 1) {
        return `$${amount.toFixed(4)}`;
    }
    return `$${amount.toFixed(2)}`;
}

/**
 * Generate background/border colors for a chart with `count` categories,
 * cycling through a fixed base palette.
 * @param {number} count - Number of colors needed
 * @returns {{background: string[], border: string[]}}
 */
function generateChartColors(count) {
    const baseColors = [
        'rgba(107, 70, 193, 0.8)',   // Purple
        'rgba(245, 158, 11, 0.8)',   // Orange
        'rgba(59, 130, 246, 0.8)',   // Blue
        'rgba(16, 185, 129, 0.8)',   // Green
        'rgba(239, 68, 68, 0.8)',    // Red
        'rgba(139, 69, 19, 0.8)',    // Brown
        'rgba(255, 192, 203, 0.8)',  // Pink
        'rgba(128, 128, 128, 0.8)',  // Gray
        'rgba(255, 165, 0, 0.8)',    // Orange
        'rgba(75, 0, 130, 0.8)'      // Indigo
    ];

    const background = [];
    const border = [];

    for (let i = 0; i < count; i++) {
        const colorIndex = i % baseColors.length;
        background.push(baseColors[colorIndex]);
        border.push(baseColors[colorIndex].replace('0.8', '1'));
    }

    return { background, border };
}

// Export the functions to make them available to other modules
window.formatting = {
    formatStatus,
    formatMode,
    formatDate,
    formatDuration,
    formatNumber,
    formatCurrency,
    generateChartColors,
    capitalizeFirstLetter
};
