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_active_interaction_exports = {};
__export(use_active_interaction_exports, {
  useActiveInteraction: () => useActiveInteraction
});
module.exports = __toCommonJS(use_active_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");
const useActiveInteraction = () => {
  const map = (0, import_maplibre.useMap)().current;
  let featureId;
  let sourceId;
  const handleClick = (0, import_react.useCallback)(
    ({ point }) => {
      const features = map.queryRenderedFeatures(point);
      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) {
        if (!(0, import_lodash_es.isUndefined)(featureId) && !(0, import_lodash_es.isUndefined)(sourceId)) {
          map.setFeatureState(
            { source: sourceId, id: featureId },
            { active: false }
          );
        }
        featureId = features[0].id;
        sourceId = features[0].layer.source;
        const activeState = features[0].state.active;
        map.setFeatureState(
          { source: sourceId, id: featureId },
          { active: !activeState }
        );
      } else {
        if (!(0, import_lodash_es.isUndefined)(featureId) && !(0, import_lodash_es.isUndefined)(sourceId)) {
          map.setFeatureState(
            { source: sourceId, id: featureId },
            { active: false }
          );
        }
      }
    },
    [featureId, sourceId, map]
  );
  (0, import_react.useEffect)(() => {
    map.on("click", handleClick);
    return () => {
      map.off("click", handleClick);
    };
  }, []);
};
