import { isNil, isString, isUndefined } from "lodash-es";
import { useCallback, useEffect } from "react";
import { useMap } from "react-map-gl/maplibre";
import { BASE_LAYER_IDS } from "../constants.js";
import {
  getAssociatedFeatures,
  hasAssociatedFeatures,
  isAssociatedFeature
} from "../functions/associated-features.js";
const featureExists = (map, source, id) => {
  const isSourcePresent = map.getSource(source) !== void 0;
  return isSourcePresent && map.getFeatureState({
    source,
    id
  }) !== void 0;
};
const blurFeature = (map, source, id) => {
  map.setFeatureState({ source, id }, { hover: false });
  if (isString(id) && hasAssociatedFeatures(id)) {
    for (const associatedFeature of getAssociatedFeatures("connection")) {
      const associatedSource = `${source}-direction`;
      const associatedId = `${id}-${associatedFeature}`;
      if (featureExists(map, associatedSource, associatedId)) {
        map.setFeatureState(
          { source: associatedSource, id: associatedId },
          {
            hover: false
          }
        );
      }
    }
  }
};
const hoverFeature = (map, source, id) => {
  map.setFeatureState({ source, id }, { hover: true });
  if (isString(id) && hasAssociatedFeatures(id)) {
    for (const associatedFeature of getAssociatedFeatures("connection")) {
      const associatedSource = `${source}-direction`;
      const associatedId = `${id}-${associatedFeature}`;
      if (featureExists(map, associatedSource, associatedId)) {
        map.setFeatureState(
          { source: `${source}-direction`, id: `${id}-${associatedFeature}` },
          {
            hover: true
          }
        );
      }
    }
  }
};
const useHoverInteraction = () => {
  const map = useMap().current;
  let featureId;
  let sourceId;
  const handleMouseOut = useCallback(
    ({ point }) => {
      if (!isNil(map) && !isUndefined(featureId) && !isUndefined(sourceId)) {
        blurFeature(map, sourceId, featureId);
      }
    },
    [featureId, sourceId, map]
  );
  const handleMouseMove = useCallback(
    ({ point }) => {
      if (!isNil(map)) {
        const features = map.queryRenderedFeatures(point).filter((feature) => !isAssociatedFeature(feature.properties.id));
        map.getCanvas().style.cursor = "grab";
        const layerId = features?.[0]?.layer?.id;
        const hasHoveredFeatures = !isNil(features) && features.length > 0 && !isUndefined(layerId);
        const isBaseLayer = BASE_LAYER_IDS.includes(layerId);
        if (hasHoveredFeatures && !isBaseLayer) {
          map.getCanvas().style.cursor = "pointer";
          if (!isUndefined(featureId) && !isUndefined(sourceId)) {
            blurFeature(map, sourceId, featureId);
          }
          featureId = features[0].id;
          sourceId = features[0].layer.source;
          if (!isUndefined(featureId)) {
            hoverFeature(map, sourceId, featureId);
          }
        } else {
          map.getCanvas().style.cursor = "grab";
          if (!isUndefined(featureId) && !isUndefined(sourceId)) {
            blurFeature(map, sourceId, featureId);
          }
        }
      }
    },
    [featureId, sourceId, map]
  );
  useEffect(() => {
    map?.on("mousemove", handleMouseMove);
    map?.on("mouseout", handleMouseOut);
    return () => {
      map?.off("mousemove", handleMouseMove);
      map?.off("mouseout", handleMouseOut);
    };
  }, []);
};
export {
  useHoverInteraction
};
//# sourceMappingURL=use-hover-interaction.js.map
