{"version":3,"file":"GraphCommandBridge.js","names":[],"sources":["jsx/react-flow/GraphCommandBridge.js"],"sourcesContent":["import { useCallback, useEffect, useMemo, useRef } from \"react\";\nimport {\n  createDomTransformProbe,\n  createFrameSampler,\n  gestureWindow,\n  reportRun\n} from \"../bench/frameSampler\";\nimport { useGraphActions } from \"./GraphActions\";\nimport {\n  reactFlowNodeSelector,\n  reactFlowPaneSelector,\n  reactFlowViewportSelector,\n  useReactFlow\n} from \"./xy\";\nfunction toWire(run, label, context) {\n  const result = reportRun(run, { ...context, label });\n  if (!result.ok) {\n    return {\n      ok: false,\n      reason: result.error.reason,\n      message: result.error.message,\n      label,\n      nodeCount: run.nodeCount || context.nodeCount,\n      culling: context.culling,\n      layoutRunner: context.layoutRunner,\n      didMove: run.didMove,\n      durationMs: run.stoppedAt - run.startedAt,\n      stats: run.stats,\n      // Carried on a REJECTION too: a run discarded for `didMove` still says\n      // how many gestures were bracketed, which is what tells an operator\n      // whether the marks were the thing that went wrong.\n      gesture: gestureWindow(run),\n      reportJson: \"\"\n    };\n  }\n  return {\n    ok: true,\n    label,\n    nodeCount: result.value.nodeCount,\n    culling: result.value.culling,\n    layoutRunner: result.value.layoutRunner ?? context.layoutRunner,\n    didMove: true,\n    durationMs: result.value.durationMs,\n    stats: result.value.stats,\n    gesture: result.value.gesture,\n    reportJson: JSON.stringify(result.value)\n  };\n}\nfunction GraphCommandBridge({\n  commands,\n  culling,\n  emit,\n  host,\n  onMounted,\n  rootRef\n}) {\n  const flow = useReactFlow();\n  const actions = useGraphActions();\n  const latest = useRef({ actions, culling, host });\n  latest.current = { actions, culling, host };\n  const probe = useMemo(\n    () => createDomTransformProbe({\n      viewport: reactFlowViewportSelector,\n      node: reactFlowNodeSelector,\n      root: rootRef.current ?? void 0\n    }),\n    [rootRef]\n  );\n  const countNodes = useCallback(() => {\n    const root = rootRef.current ?? globalThis.document;\n    return root?.querySelectorAll(reactFlowNodeSelector).length ?? 0;\n  }, [rootRef]);\n  const sampler = useMemo(\n    () => createFrameSampler({ probeTransform: probe, countNodes }),\n    [countNodes, probe]\n  );\n  useEffect(() => {\n    onMounted?.();\n    commands.current = {\n      fitView: () => {\n        void flow.fitView();\n      },\n      /**\n       * It was a NO-OP and the comment said so: milestone 4 would fill it in,\n       * and milestone 4 built the engine and never came back. `layoutRunner`\n       * therefore read `none` on every device — not because the probe failed,\n       * but because nothing on a phone could ask for a layout.\n       */\n      relayout: () => {\n        void latest.current.actions.relayout().then((result) => {\n          emit({\n            t: \"layoutResult\",\n            // `none` is honest for an empty graph: no engine ran, so no runner\n            // produced anything, and inventing one would be a reading.\n            runner: result?.runner ?? \"none\",\n            ms: result?.ms ?? 0,\n            nodeCount: latest.current.actions.nodeCount\n          });\n        }).catch((error) => {\n          emit({\n            t: \"layoutResult\",\n            runner: \"none\",\n            ms: 0,\n            nodeCount: latest.current.actions.nodeCount,\n            error: error instanceof Error ? error.message : String(error)\n          });\n        });\n      },\n      zoomTo: (zoom) => {\n        void flow.zoomTo(zoom);\n      },\n      centerOn: (nodeId) => {\n        void flow.fitView({ nodes: [{ id: nodeId }], duration: 200 });\n      },\n      setSelection: (ids) => {\n        const wanted = new Set(ids);\n        flow.setNodes((nodes) => nodes.map((node) => ({ ...node, selected: wanted.has(node.id) })));\n        emit({ t: \"selection\", ids: [...wanted] });\n      },\n      exportViewport: () => {\n        emit({ t: \"viewport\", viewport: flow.getViewport() });\n      },\n      undo: () => latest.current.actions.undo(),\n      redo: () => latest.current.actions.redo(),\n      perfStart: () => sampler.start(),\n      perfMark: (label) => {\n        sampler.mark(label);\n      },\n      setViewport: (x, y, zoom) => {\n        void flow.setViewport({ x, y, zoom });\n      },\n      /**\n       * Synthesised pinch, design §13.2. Dispatched on the PANE, because that\n       * is the element d3-zoom binds its touch listener to.\n       *\n       * It moved in here from the Perf200 story in milestone 6 for the reason\n       * everything else did: the pane exists only in the realm the canvas\n       * paints in, and on native that realm is the WebView. A story on the\n       * Hermes side has no pane to dispatch on.\n       */\n      synthPinch: (scale, steps) => {\n        const root = rootRef.current ?? globalThis.document;\n        const pane = root?.querySelector(reactFlowPaneSelector);\n        if (!pane) return;\n        if (typeof Touch !== \"function\" || typeof TouchEvent !== \"function\") return;\n        const box = pane.getBoundingClientRect();\n        const cx = box.left + box.width / 2;\n        const cy = box.top + box.height / 2;\n        const point = (id, x, y) => new Touch({ identifier: id, target: pane, clientX: x, clientY: y });\n        const send = (type, points) => {\n          pane.dispatchEvent(\n            new TouchEvent(type, {\n              bubbles: true,\n              cancelable: true,\n              touches: points,\n              targetTouches: points,\n              changedTouches: points\n            })\n          );\n        };\n        const spread = 60;\n        const total = Math.max(1, Math.round(steps));\n        send(\"touchstart\", [point(1, cx - spread, cy), point(2, cx + spread, cy)]);\n        let step = 0;\n        const advance = () => {\n          step += 1;\n          const factor = 1 + (scale - 1) * (step / total);\n          const half = spread * factor;\n          send(\"touchmove\", [point(1, cx - half, cy), point(2, cx + half, cy)]);\n          if (step < total) globalThis.requestAnimationFrame(advance);\n          else send(\"touchend\", []);\n        };\n        globalThis.requestAnimationFrame(advance);\n      },\n      perfStop: (label) => {\n        const run = sampler.stop();\n        emit({\n          t: \"perfReport\",\n          report: toWire(run, label, {\n            culling: latest.current.culling,\n            host: latest.current.host,\n            layoutRunner: latest.current.actions.layoutRunner,\n            nodeCount: latest.current.actions.nodeCount\n          })\n        });\n      }\n    };\n    return () => {\n      commands.current = null;\n    };\n  }, [commands, emit, flow, onMounted, rootRef, sampler]);\n  return null;\n}\nexport {\n  GraphCommandBridge\n};\n//# sourceMappingURL=GraphCommandBridge.js.map\n"],"mappings":";;;;;;AAcA,SAAS,OAAO,KAAK,OAAO,SAAS;CACnC,MAAM,SAAS,UAAU,KAAK;EAAE,GAAG;EAAS;CAAM,CAAC;CACnD,IAAI,CAAC,OAAO,IAAI;EACd,OAAO;GACL,IAAI;GACJ,QAAQ,OAAO,MAAM;GACrB,SAAS,OAAO,MAAM;GACtB;GACA,WAAW,IAAI,aAAa,QAAQ;GACpC,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