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);

// src/components/pisellFloorMapLayout/hooks/useFloorMapEdgeViewModel.ts
var useFloorMapEdgeViewModel_exports = {};
__export(useFloorMapEdgeViewModel_exports, {
  useFloorMapEdgeViewModel: () => useFloorMapEdgeViewModel
});
module.exports = __toCommonJS(useFloorMapEdgeViewModel_exports);
var import_react = require("react");
var import_edgeRouting = require("../utils/edgeRouting");
function extractRouteSegments(points) {
  const segs = [];
  for (let i = 0; i < points.length - 1; i++) {
    const [x1, y1] = points[i];
    const [x2, y2] = points[i + 1];
    if (y1 === y2 && x1 !== x2) {
      segs.push({
        orientation: "h",
        key: y1,
        a: Math.min(x1, x2),
        b: Math.max(x1, x2)
      });
    } else if (x1 === x2 && y1 !== y2) {
      segs.push({
        orientation: "v",
        key: x1,
        a: Math.min(y1, y2),
        b: Math.max(y1, y2)
      });
    }
  }
  return segs;
}
function segmentsOverlap(a, b) {
  if (a.orientation !== b.orientation) return false;
  if (a.key !== b.key) return false;
  return Math.max(a.a, b.a) < Math.min(a.b, b.b);
}
function routesShareChannel(a, b) {
  const segsA = extractRouteSegments(a);
  const segsB = extractRouteSegments(b);
  for (const sa of segsA) {
    for (const sb of segsB) {
      if (segmentsOverlap(sa, sb)) return true;
    }
  }
  return false;
}
function edgesShareTarget(a, b) {
  return a.targetId === b.targetId || a.sourceId === b.sourceId;
}
function useFloorMapEdgeViewModel(params) {
  const {
    edges = [],
    items,
    cellSize,
    itemUnit,
    itemGap,
    pendingRectsById,
    pendingRoutingByEdge
  } = params;
  return (0, import_react.useMemo)(() => {
    const rectsById = /* @__PURE__ */ new Map();
    for (const item of items) {
      rectsById.set(
        item.id,
        (0, import_edgeRouting.getItemPixelRect)(item, {
          cellSize,
          itemUnit,
          itemGap,
          override: pendingRectsById == null ? void 0 : pendingRectsById[item.id]
        })
      );
    }
    const naive = edges.map((edge) => ({
      edge,
      route: (0, import_edgeRouting.buildEdgeRoute)(edge, rectsById, {
        routingOverride: pendingRoutingByEdge == null ? void 0 : pendingRoutingByEdge[edge.id]
      })
    }));
    const routes = [];
    const accepted = [];
    const acceptedEdges = [];
    for (let i = 0; i < naive.length; i++) {
      const { edge, route } = naive[i];
      if (!route) continue;
      let chosen = route;
      let attempt = 0;
      const maxAttempts = 10;
      while (attempt < maxAttempts) {
        const conflicts = accepted.some((r, idx) => {
          const other = acceptedEdges[idx];
          return routesShareChannel(chosen.points, r.points) || other != null && edgesShareTarget(edge, other);
        });
        if (!conflicts) break;
        attempt += 1;
        const offset = (0, import_edgeRouting.getEdgeChannelOffset)(attempt);
        const re = (0, import_edgeRouting.buildEdgeRoute)(edge, rectsById, {
          channelOffset: offset,
          routingOverride: pendingRoutingByEdge == null ? void 0 : pendingRoutingByEdge[edge.id]
        });
        if (!re) break;
        chosen = re;
      }
      accepted.push(chosen);
      acceptedEdges.push(edge);
      routes.push(chosen);
    }
    return { routes, rectsById };
  }, [
    edges,
    items,
    cellSize,
    itemUnit,
    itemGap,
    pendingRectsById,
    pendingRoutingByEdge
  ]);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useFloorMapEdgeViewModel
});
