import { useState, useEffect } from 'react';
import { PartialStateShare, raf } from './utils';

export function getLayerHoverStatus(hoverId) {
  const [isHover, setHover] = useState(((PartialStateShare as any).get('mouse_over_layer') || {}).hover === hoverId);
  useEffect(() => {
    const cb = raf((layer) => {
      const _hoverId = layer ? layer.hover : null;
      setHover(_hoverId === hoverId);
    });
    PartialStateShare.on('mouse_over_layer', cb);
    return () => {
      cb && cb.destory();
      PartialStateShare.off('mouse_over_layer', cb);
    };
  }, []);
  return isHover;
}
