import { useMemo } from 'react';
import { chartTheme } from '../theme/chart.theme.js';

function useChartTheme({ config, variant = 'default', theme }) {
    const resolvedTheme = useMemo(() => {
        var _a, _b, _c;
        // Merge default theme with overrides
        const mergedTheme = {
            ...chartTheme,
            baseStyle: {
                ...chartTheme.baseStyle,
                ...((theme === null || theme === void 0 ? void 0 : theme.baseStyle) || {}),
                colors: {
                    ...chartTheme.baseStyle.colors,
                    ...(((_a = theme === null || theme === void 0 ? void 0 : theme.baseStyle) === null || _a === void 0 ? void 0 : _a.colors) || {}),
                },
                opacity: {
                    ...chartTheme.baseStyle.opacity,
                    ...(((_b = theme === null || theme === void 0 ? void 0 : theme.baseStyle) === null || _b === void 0 ? void 0 : _b.opacity) || {}),
                },
                transitions: {
                    ...chartTheme.baseStyle.transitions,
                    ...(((_c = theme === null || theme === void 0 ? void 0 : theme.baseStyle) === null || _c === void 0 ? void 0 : _c.transitions) || {}),
                },
            },
            variants: {
                ...chartTheme.variants,
                ...theme === null || theme === void 0 ? void 0 : theme.variants,
            },
        };
        return mergedTheme;
    }, [theme]);
    const getSeriesColor = useMemo(() => {
        return (dataKey, index) => {
            var _a, _b, _c;
            // Add null check for config
            if (!config || typeof config !== 'object') {
                // Use base series colors as fallback
                const seriesColors = resolvedTheme.baseStyle.colors.series;
                return seriesColors[index % seriesColors.length];
            }
            // Priority 1: Check if config has a specific color for this dataKey
            if ((_a = config[dataKey]) === null || _a === void 0 ? void 0 : _a.color) {
                return config[dataKey].color;
            }
            // Priority 2: Use variant colors if available
            const variantColors = (_c = (_b = resolvedTheme.variants) === null || _b === void 0 ? void 0 : _b[variant]) === null || _c === void 0 ? void 0 : _c.colors;
            if (variantColors && variantColors.length > 0) {
                return variantColors[index % variantColors.length];
            }
            // Priority 3: Use base series colors
            const seriesColors = resolvedTheme.baseStyle.colors.series;
            return seriesColors[index % seriesColors.length];
        };
    }, [config, variant, resolvedTheme]);
    const getColorClass = useMemo(() => {
        return (type) => {
            return resolvedTheme.baseStyle.colors[type];
        };
    }, [resolvedTheme]);
    const getOpacityClass = useMemo(() => {
        return (type) => {
            return resolvedTheme.baseStyle.opacity[type];
        };
    }, [resolvedTheme]);
    const getTransitionClass = useMemo(() => {
        return (type) => {
            return resolvedTheme.baseStyle.transitions[type];
        };
    }, [resolvedTheme]);
    // Generate CSS variables for dynamic colors (for cases where Tailwind classes aren't enough)
    const chartColors = useMemo(() => {
        const colors = {};
        // Add null check for config
        if (!config || typeof config !== 'object') {
            return colors;
        }
        Object.entries(config).forEach(([key, value]) => {
            if (value === null || value === void 0 ? void 0 : value.color) {
                colors[`--color-${key}`] = value.color;
            }
            else if ((value === null || value === void 0 ? void 0 : value.theme) && typeof value.theme === 'string') {
                // Handle theme-based colors
                colors[`--color-${key}`] = value.theme;
            }
        });
        return colors;
    }, [config]);
    const getThemeValue = (key) => {
        return `var(--color-${key})`;
    };
    return {
        // New theme utilities
        getSeriesColor,
        getColorClass,
        getOpacityClass,
        getTransitionClass,
        resolvedTheme,
        // Legacy utilities (for backward compatibility)
        chartColors,
        getThemeValue,
    };
}

export { useChartTheme };
