var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellFloorMapLayout/components/EdgeLayer.tsx
var EdgeLayer_exports = {};
__export(EdgeLayer_exports, {
  EdgeLayer: () => EdgeLayer
});
module.exports = __toCommonJS(EdgeLayer_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_edgeRouting = require("../utils/edgeRouting");
var import_EdgeLayer = require("./EdgeLayer.less");
var PREFIX = "pisell-floor-map-layout";
var SEGMENT_HANDLE_BASE_SIZE = 12;
var SEGMENT_HANDLE_MIN_LENGTH = 16;
function resolveEdgeStroke(edge, appearance, selected) {
  if (selected) {
    return {
      stroke: appearance.edgeSelectedStroke,
      strokeWidth: (edge.strokeWidth ?? 1.5) + 1
    };
  }
  if (edge.color) {
    return {
      stroke: edge.color,
      strokeWidth: edge.strokeWidth ?? 1.5,
      strokeDasharray: edge.dashed ? "6 4" : void 0
    };
  }
  const status = edge.status ?? "unknown";
  const style = appearance.edgeStatusStyles[status];
  return {
    stroke: style.stroke,
    strokeWidth: edge.strokeWidth ?? 1.5,
    strokeDasharray: edge.dashed || style.dashed ? "6 4" : void 0
  };
}
function PortLabel(props) {
  const { x, y, text, appearance } = props;
  const padX = 6;
  const padY = 3;
  const estW = Math.max(32, text.length * 6.5 + padX * 2);
  const h = 18;
  return /* @__PURE__ */ import_react.default.createElement("g", { className: `${PREFIX}-edge-port-label` }, /* @__PURE__ */ import_react.default.createElement(
    "rect",
    {
      x: x - estW / 2,
      y: y - h / 2,
      width: estW,
      height: h,
      rx: 4,
      fill: appearance.edgePortLabelBackground,
      stroke: appearance.edgePortLabelBorder,
      strokeWidth: 1
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    "text",
    {
      x,
      y: y + 4,
      textAnchor: "middle",
      fontSize: 11,
      fill: appearance.edgePortLabelText
    },
    text
  ));
}
function EdgeLayer(props) {
  const {
    width,
    height,
    contentOffsetX = 0,
    contentOffsetY = 0,
    edges,
    routes,
    rectsById,
    appearance,
    mode,
    selectedEdgeId,
    ghostPathD,
    onEdgeClick,
    viewportScale = 1,
    onSegmentDragStart,
    className
  } = props;
  const safeScale = Math.max(viewportScale, 0.01);
  const handleSize = SEGMENT_HANDLE_BASE_SIZE / safeScale;
  const handleHalf = handleSize / 2;
  const edgeById = (0, import_react.useMemo)(() => {
    const m = /* @__PURE__ */ new Map();
    for (const e of edges) m.set(e.id, e);
    return m;
  }, [edges]);
  const routeById = (0, import_react.useMemo)(() => {
    const m = /* @__PURE__ */ new Map();
    for (const r of routes) m.set(r.edgeId, r);
    return m;
  }, [routes]);
  const centerLabelByEdgeId = (0, import_react.useMemo)(
    () => (0, import_edgeRouting.resolveEdgeCenterLabelPositions)(
      routes.map((route) => {
        const edge = edgeById.get(route.edgeId);
        if (!edge) return null;
        return {
          edgeId: route.edgeId,
          sourceId: edge.sourceId,
          targetId: edge.targetId,
          points: route.points,
          label: edge.label
        };
      }).filter((row) => row != null),
      { placement: "target-tail", rectsById }
    ),
    [routes, edgeById, rectsById]
  );
  return /* @__PURE__ */ import_react.default.createElement(
    "svg",
    {
      className: (0, import_classnames.default)(`${PREFIX}-edge-layer`, className),
      width,
      height,
      style: {
        position: "absolute",
        left: contentOffsetX,
        top: contentOffsetY,
        pointerEvents: "none",
        overflow: "visible"
      },
      "aria-hidden": mode === "read"
    },
    /* @__PURE__ */ import_react.default.createElement("g", { className: `${PREFIX}-edge-layer-inner` }, routes.map((route) => {
      const edge = edgeById.get(route.edgeId);
      if (!edge) return null;
      const selected = selectedEdgeId === edge.id;
      const stroke = resolveEdgeStroke(edge, appearance, selected);
      return /* @__PURE__ */ import_react.default.createElement("g", { key: edge.id, className: `${PREFIX}-edge-group` }, /* @__PURE__ */ import_react.default.createElement(
        "path",
        {
          d: route.pathD,
          fill: "none",
          stroke: stroke.stroke,
          strokeWidth: stroke.strokeWidth,
          strokeDasharray: stroke.strokeDasharray,
          strokeLinecap: "round",
          strokeLinejoin: "round",
          className: (0, import_classnames.default)(`${PREFIX}-edge-path`, {
            [`${PREFIX}-edge-path--selected`]: selected,
            [`${PREFIX}-edge-path--interactive`]: mode === "edit"
          }),
          style: {
            pointerEvents: mode === "edit" ? "stroke" : "none",
            cursor: mode === "edit" ? "pointer" : void 0
          },
          onClick: mode === "edit" && onEdgeClick ? (ev) => {
            ev.stopPropagation();
            onEdgeClick(edge.id, ev);
          } : void 0
        }
      ), edge.label && route.points.length >= 2 ? (() => {
        const labelPos = centerLabelByEdgeId.get(edge.id);
        if (!labelPos) return null;
        return /* @__PURE__ */ import_react.default.createElement(
          PortLabel,
          {
            x: labelPos.x,
            y: labelPos.y,
            text: edge.label,
            appearance
          }
        );
      })() : null);
    }), edges.map((edge) => {
      const route = routeById.get(edge.id);
      const srcRect = rectsById.get(edge.sourceId);
      const tgtRect = rectsById.get(edge.targetId);
      if (!route || !srcRect || !tgtRect) return null;
      const sourceAnchor = edge.sourceAnchor ?? "right";
      const targetAnchor = edge.targetAnchor ?? "left";
      const spLabel = (0, import_edgeRouting.getPortLabelPosition)(srcRect, sourceAnchor);
      const tpLabel = (0, import_edgeRouting.getPortLabelPosition)(tgtRect, targetAnchor);
      return /* @__PURE__ */ import_react.default.createElement("g", { key: `${edge.id}-labels` }, edge.sourceLabel ? /* @__PURE__ */ import_react.default.createElement(
        PortLabel,
        {
          x: spLabel.x,
          y: spLabel.y,
          text: edge.sourceLabel,
          appearance
        }
      ) : null, edge.targetLabel ? /* @__PURE__ */ import_react.default.createElement(
        PortLabel,
        {
          x: tpLabel.x,
          y: tpLabel.y,
          text: edge.targetLabel,
          appearance
        }
      ) : null);
    }), mode === "edit" && selectedEdgeId && onSegmentDragStart ? (() => {
      const route = routeById.get(selectedEdgeId);
      if (!route) return null;
      const pts = route.points;
      if (pts.length < 2) return null;
      const lastSegIdx = pts.length - 2;
      const handles = [];
      for (let i = 0; i <= lastSegIdx; i++) {
        const role = i === 0 ? "first" : i === lastSegIdx ? "last" : "inner";
        const [x1, y1] = pts[i];
        const [x2, y2] = pts[i + 1];
        let axis;
        let position;
        let length;
        let midX;
        let midY;
        if (y1 === y2 && x1 !== x2) {
          axis = "h";
          position = y1;
          length = Math.abs(x2 - x1);
          midX = (x1 + x2) / 2;
          midY = y1;
        } else if (x1 === x2 && y1 !== y2) {
          axis = "v";
          position = x1;
          length = Math.abs(y2 - y1);
          midX = x1;
          midY = (y1 + y2) / 2;
        } else {
          continue;
        }
        if (length < SEGMENT_HANDLE_MIN_LENGTH) continue;
        handles.push({
          key: `${role}-${i}`,
          role,
          /** inner 时传 path segment index（拖动逻辑直接消费） */
          segmentIndex: i,
          axis,
          position,
          length,
          midX,
          midY
        });
      }
      if (handles.length === 0) return null;
      return /* @__PURE__ */ import_react.default.createElement(
        "g",
        {
          className: `${PREFIX}-edge-segment-handles`,
          pointerEvents: "auto"
        },
        handles.map((h) => /* @__PURE__ */ import_react.default.createElement(
          "rect",
          {
            key: h.key,
            x: h.midX - handleHalf,
            y: h.midY - handleHalf,
            width: handleSize,
            height: handleSize,
            rx: handleHalf,
            ry: handleHalf,
            className: (0, import_classnames.default)(
              `${PREFIX}-edge-segment-handle`,
              `${PREFIX}-edge-segment-handle--${h.axis}`,
              `${PREFIX}-edge-segment-handle--${h.role}`
            ),
            fill: appearance.edgeSelectedStroke,
            stroke: "#fff",
            strokeWidth: 1.5 / safeScale,
            style: {
              cursor: h.axis === "h" ? "ns-resize" : "ew-resize",
              touchAction: "none"
            },
            onPointerDown: (ev) => {
              ev.stopPropagation();
              ev.preventDefault();
              onSegmentDragStart({
                edgeId: selectedEdgeId,
                role: h.role,
                segmentIndex: h.segmentIndex,
                axis: h.axis,
                startPosition: h.position,
                clientX: ev.clientX,
                clientY: ev.clientY,
                segmentLength: h.length
              });
            }
          }
        ))
      );
    })() : null, ghostPathD ? /* @__PURE__ */ import_react.default.createElement(
      "path",
      {
        d: ghostPathD,
        fill: "none",
        stroke: appearance.edgeGhostStroke,
        strokeWidth: 1.5,
        strokeDasharray: "4 4",
        strokeLinecap: "round",
        pointerEvents: "none"
      }
    ) : null)
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  EdgeLayer
});
