var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var use_hover_interaction_exports = {};
__export(use_hover_interaction_exports, {
  useHoverInteraction: () => useHoverInteraction
});
module.exports = __toCommonJS(use_hover_interaction_exports);
var import_lodash_es = require("lodash");
var import_react = require("react");
var import_maplibre = require("react-map-gl/maplibre");
var import_constants = require("../constants.js");
var import_associated_features = require("../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 ((0, import_lodash_es.isString)(id) && (0, import_associated_features.hasAssociatedFeatures)(id)) {
    for (const associatedFeature of (0, import_associated_features.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 ((0, import_lodash_es.isString)(id) && (0, import_associated_features.hasAssociatedFeatures)(id)) {
    for (const associatedFeature of (0, import_associated_features.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 = (0, import_maplibre.useMap)().current;
  let featureId;
  let sourceId;
  const handleMouseOut = (0, import_react.useCallback)(
    ({ point }) => {
      if (!(0, import_lodash_es.isNil)(map) && !(0, import_lodash_es.isUndefined)(featureId) && !(0, import_lodash_es.isUndefined)(sourceId)) {
        blurFeature(map, sourceId, featureId);
      }
    },
    [featureId, sourceId, map]
  );
  const handleMouseMove = (0, import_react.useCallback)(
    ({ point }) => {
      if (!(0, import_lodash_es.isNil)(map)) {
        const features = map.queryRenderedFeatures(point).filter((feature) => !(0, import_associated_features.isAssociatedFeature)(feature.properties.id));
        map.getCanvas().style.cursor = "grab";
        const layerId = features?.[0]?.layer?.id;
        const hasHoveredFeatures = !(0, import_lodash_es.isNil)(features) && features.length > 0 && !(0, import_lodash_es.isUndefined)(layerId);
        const isBaseLayer = import_constants.BASE_LAYER_IDS.includes(layerId);
        if (hasHoveredFeatures && !isBaseLayer) {
          map.getCanvas().style.cursor = "pointer";
          if (!(0, import_lodash_es.isUndefined)(featureId) && !(0, import_lodash_es.isUndefined)(sourceId)) {
            blurFeature(map, sourceId, featureId);
          }
          featureId = features[0].id;
          sourceId = features[0].layer.source;
          if (!(0, import_lodash_es.isUndefined)(featureId)) {
            hoverFeature(map, sourceId, featureId);
          }
        } else {
          map.getCanvas().style.cursor = "grab";
          if (!(0, import_lodash_es.isUndefined)(featureId) && !(0, import_lodash_es.isUndefined)(sourceId)) {
            blurFeature(map, sourceId, featureId);
          }
        }
      }
    },
    [featureId, sourceId, map]
  );
  (0, import_react.useEffect)(() => {
    map?.on("mousemove", handleMouseMove);
    map?.on("mouseout", handleMouseOut);
    return () => {
      map?.off("mousemove", handleMouseMove);
      map?.off("mouseout", handleMouseOut);
    };
  }, []);
};
