/**
 * Process raw heatmap data and calculate necessary values
 */
function processHeatmapData(data, valueRange) {
    // Extract unique categories preserving the order they appear in the data
    const xCategories = [];
    const yCategories = [];
    // Preserve order by iterating through data and collecting unique values
    data.forEach(point => {
        if (!xCategories.includes(point.x)) {
            xCategories.push(point.x);
        }
        if (!yCategories.includes(point.y)) {
            yCategories.push(point.y);
        }
    });
    // Calculate value range if not provided
    const values = data.map(d => d.value).filter(v => v !== null);
    const calculatedMin = Math.min(...values);
    const calculatedMax = Math.max(...values);
    const finalValueRange = valueRange || [calculatedMin, calculatedMax];
    return {
        data,
        xCategories,
        yCategories,
        valueRange: finalValueRange,
        minValue: finalValueRange[0],
        maxValue: finalValueRange[1],
    };
}
/**
 * Calculate intensity (0-1) for a given value within the range
 */
function calculateIntensity(value, minValue, maxValue) {
    if (value === null)
        return 0;
    if (minValue === maxValue)
        return 1;
    return Math.max(0, Math.min(1, (value - minValue) / (maxValue - minValue)));
}
/**
 * Create a complete grid matrix with all x,y combinations
 * Fills missing data points with null values
 */
function createGridMatrix(data, xCategories, yCategories) {
    // Create a map for quick lookup
    const dataMap = new Map();
    data.forEach(point => {
        const key = `${point.x}-${point.y}`;
        dataMap.set(key, point);
    });
    // Create complete grid
    return yCategories.map(y => xCategories.map(x => {
        const key = `${x}-${y}`;
        return dataMap.get(key) || { x, y, value: null };
    }));
}
/**
 * Generate default tooltip content for a data point
 */
function getDefaultTooltipContent(data) {
    const value = data.value !== null ? data.value.toString() : 'No data';
    return `${data.x}, ${data.y}: ${value}`;
}
/**
 * Format axis labels with common patterns
 */
function formatAxisLabel(value, type = 'default') {
    switch (type) {
        case 'day':
            if (typeof value === 'string') {
                // Handle day abbreviations
                const dayMap = {
                    'Mon': 'Mo', 'Monday': 'Mo',
                    'Tue': 'Tu', 'Tuesday': 'Tu',
                    'Wed': 'We', 'Wednesday': 'We',
                    'Thu': 'Th', 'Thursday': 'Th',
                    'Fri': 'Fr', 'Friday': 'Fr',
                    'Sat': 'Sa', 'Saturday': 'Sa',
                    'Sun': 'Su', 'Sunday': 'Su',
                };
                return dayMap[value] || value.substring(0, 2);
            }
            break;
        case 'month':
            if (typeof value === 'string') {
                // Handle month abbreviations
                const monthMap = {
                    'January': 'Jan', 'February': 'Feb', 'March': 'Mar',
                    'April': 'Apr', 'May': 'May', 'June': 'Jun',
                    'July': 'Jul', 'August': 'Aug', 'September': 'Sep',
                    'October': 'Oct', 'November': 'Nov', 'December': 'Dec',
                };
                return monthMap[value] || value.substring(0, 3);
            }
            break;
        case 'hour':
            if (typeof value === 'number') {
                return value === 0 ? '12am' : value === 12 ? '12pm' :
                    value < 12 ? `${value}am` : `${value - 12}pm`;
            }
            break;
        case 'date':
            if (typeof value === 'string') {
                // Try to parse as date string
                const date = new Date(value);
                if (!isNaN(date.getTime())) {
                    return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
                }
            }
            break;
        case 'number':
            if (typeof value === 'number') {
                return value.toLocaleString();
            }
            break;
    }
    return String(value);
}
/**
 * Generate sample heatmap data for testing/examples
 */
function generateSampleData(type = 'weekly') {
    const data = [];
    switch (type) {
        case 'weekly':
            const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
            const weeks = Array.from({ length: 12 }, (_, i) => `Week ${i + 1}`);
            weeks.forEach(week => {
                days.forEach(day => {
                    data.push({
                        x: day,
                        y: week,
                        value: Math.floor(Math.random() * 20),
                    });
                });
            });
            break;
        case 'monthly':
            const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
            const years = ['2022', '2023', '2024'];
            years.forEach(year => {
                months.forEach(month => {
                    data.push({
                        x: month,
                        y: year,
                        value: Math.floor(Math.random() * 100),
                    });
                });
            });
            break;
        case 'hourly':
            const hours = Array.from({ length: 24 }, (_, i) => i);
            const daysOfWeek = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
            daysOfWeek.forEach(day => {
                hours.forEach(hour => {
                    data.push({
                        x: hour,
                        y: day,
                        value: Math.floor(Math.random() * 50),
                    });
                });
            });
            break;
    }
    return data;
}

export { calculateIntensity, createGridMatrix, formatAxisLabel, generateSampleData, getDefaultTooltipContent, processHeatmapData };
