import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useState, useMemo, useCallback, useEffect, useRef, } from 'react';
import { v4 as uuidv4 } from 'uuid';
export const useChartId = () => {
    const idRef = useRef(null);
    if (idRef.current === null) {
        idRef.current = uuidv4();
    }
    return idRef.current;
};
const ChartLegendContext = createContext(null);
export const ChartLegendWrapper = ({ children, colorSet, }) => {
    const [registeredColorSets, setRegisteredColorSets] = useState({});
    const [internalColorSet, setInternalColorSet] = useState(() => {
        return typeof colorSet === 'function' ? {} : colorSet;
    });
    useEffect(() => {
        if (typeof colorSet === 'function') {
            const allUniqueSeriesNames = Array.from(new Set(Object.values(registeredColorSets).flat()));
            if (allUniqueSeriesNames.length > 0) {
                const newColorSet = colorSet(allUniqueSeriesNames);
                setInternalColorSet(newColorSet);
            }
        }
        else {
            setInternalColorSet(colorSet);
        }
    }, [registeredColorSets, colorSet]);
    const allResources = useMemo(() => Object.keys(internalColorSet), [internalColorSet]);
    const [selectedResources, setSelectedResources] = useState(allResources);
    useEffect(() => {
        setSelectedResources(allResources);
    }, [allResources]);
    const register = useCallback((chartId, seriesNames) => {
        setRegisteredColorSets((prev) => ({
            ...prev,
            [chartId]: seriesNames,
        }));
    }, []);
    const addSelectedResource = useCallback((resource) => {
        setSelectedResources((prev) => prev.includes(resource) ? prev : [...prev, resource]);
    }, []);
    const removeSelectedResource = useCallback((resource) => {
        setSelectedResources((prev) => prev.filter((r) => r !== resource));
    }, []);
    const selectAllResources = useCallback(() => {
        setSelectedResources(Object.keys(internalColorSet));
    }, [internalColorSet]);
    const selectOnlyResource = useCallback((resource) => {
        setSelectedResources([resource]);
    }, []);
    const isOnlyOneSelected = useCallback(() => {
        return selectedResources.length === 1;
    }, [selectedResources]);
    const isSelected = useCallback((resource) => {
        return selectedResources.includes(resource);
    }, [selectedResources]);
    const getColor = useCallback((resource) => {
        const color = internalColorSet[resource];
        if (!color) {
            console.warn(`ChartLegendWrapper: No color defined for resource "${resource}"`);
            return undefined;
        }
        return color;
    }, [internalColorSet]);
    const listResources = useCallback(() => {
        return Object.keys(internalColorSet).sort();
    }, [internalColorSet]);
    const chartLegendState = useMemo(() => ({
        selectedResources,
        addSelectedResource,
        removeSelectedResource,
        selectAllResources,
        selectOnlyResource,
        isSelected,
        getColor,
        listResources,
        isOnlyOneSelected,
        register,
    }), [
        selectedResources,
        addSelectedResource,
        removeSelectedResource,
        selectAllResources,
        selectOnlyResource,
        isSelected,
        getColor,
        listResources,
        isOnlyOneSelected,
        register,
    ]);
    return (_jsx(ChartLegendContext.Provider, { value: chartLegendState, children: children }));
};
export const useChartLegend = () => {
    const context = useContext(ChartLegendContext);
    if (!context) {
        throw new Error('useChartLegend must be used within a ChartLegendWrapper');
    }
    return context;
};
