import { useEffect } from 'react';
import { popoverMap } from '../utils';
import useUID from './useUID';
/**
 * Adds target to popover map and returns the unique ID for the popover.
 *
 * @param target The target element for the popover.
 * @param active Whether the popover is active.
 * @returns The unique ID for the popover.
 */
const usePopoverMap = (target, active = true) => {
    const popvoerId = useUID();
    useEffect(() => {
        if (!target || !active)
            return;
        if (!popoverMap.has(target)) {
            popoverMap.set(target, new Set());
        }
        target.toggleAttribute('data-popover-target', true);
        popoverMap.get(target).add(popvoerId);
        return () => {
            if (!popoverMap.has(target))
                return;
            popoverMap.get(target).delete(popvoerId);
            if (popoverMap.get(target).size === 0) {
                popoverMap.delete(target);
                target.removeAttribute('data-popover-target');
            }
        };
    }, [active, target, popvoerId]);
    return popvoerId;
};
export default usePopoverMap;
//# sourceMappingURL=usePopoverMap.js.map