function checkOverlap(rectA, rectB, threshold) {
  const overlapHeight = Math.min(rectA.bottom, rectB.bottom) - Math.max(rectA.top, rectB.top);
  const gap = Math.max(
    0,
    Math.max(rectA.left, rectB.left) - Math.min(rectA.right, rectB.right)
  );
  return {
    overlaps: gap <= threshold && overlapHeight > 0,
    gap,
    overlapHeight
  };
}
function buildClusters(targets, adjacency) {
  const visited = /* @__PURE__ */ new Set();
  const clusters = [];
  targets.forEach((target) => {
    if (visited.has(target)) return;
    const stack = [target];
    const cluster = [];
    visited.add(target);
    while (stack.length > 0) {
      const node = stack.pop();
      cluster.push(node);
      adjacency.get(node).forEach((neighbor) => {
        if (!visited.has(neighbor)) {
          visited.add(neighbor);
          stack.push(neighbor);
        }
      });
    }
    if (cluster.length >= 2) clusters.push(cluster);
  });
  return clusters;
}
function buildAdjacencyMaps(targets, rects, thresholds, currentGroupByTarget) {
  const adjacency = /* @__PURE__ */ new Map();
  const preAdjacency = /* @__PURE__ */ new Map();
  const behindAdjacency = /* @__PURE__ */ new Map();
  const preGrouped = /* @__PURE__ */ new Set();
  targets.forEach((target) => {
    adjacency.set(target, /* @__PURE__ */ new Set());
    preAdjacency.set(target, /* @__PURE__ */ new Set());
    behindAdjacency.set(target, /* @__PURE__ */ new Set());
  });
  for (let i = 0; i < targets.length; i += 1) {
    const a = targets[i];
    const ra = rects.get(a);
    for (let j = i + 1; j < targets.length; j += 1) {
      const b = targets[j];
      const rb = rects.get(b);
      const sameGroup = currentGroupByTarget.get(a) && currentGroupByTarget.get(a) === currentGroupByTarget.get(b);
      const threshold = sameGroup ? thresholds.exitThreshold : thresholds.enterThreshold;
      const overlap = checkOverlap(ra, rb, threshold);
      const preOverlap = checkOverlap(ra, rb, thresholds.preThreshold);
      const behindOverlap = checkOverlap(ra, rb, thresholds.behindThreshold);
      if (preOverlap.overlaps) {
        preGrouped.add(a);
        preGrouped.add(b);
        preAdjacency.get(a).add(b);
        preAdjacency.get(b).add(a);
      }
      if (behindOverlap.overlaps) {
        behindAdjacency.get(a).add(b);
        behindAdjacency.get(b).add(a);
      }
      if (overlap.overlaps) {
        adjacency.get(a).add(b);
        adjacency.get(b).add(a);
      }
    }
  }
  return { adjacency, preAdjacency, behindAdjacency, preGrouped };
}
export {
  buildAdjacencyMaps,
  buildClusters
};
//# sourceMappingURL=poi-layer-grouping-utils.js.map
