{"version":3,"file":"GraphLayout.js","names":[],"sources":["jsx/graphLayout/GraphLayout.js"],"sourcesContent":["import { defaultKnobs, usePresetContext } from \"@multiplatform.one/theme\";\nimport { Fragment, useMemo, useState } from \"react\";\nimport { getTokenValue, useTheme, View } from \"tamagui\";\nimport { edgeStrokeWidth, themeTokenName } from \"../edges/edgeStyle\";\nimport { GraphPath, GraphSvg } from \"./GraphSvg\";\nimport { GraphViewport } from \"./GraphViewport\";\nimport { planGraphLayout } from \"./plan\";\nimport { jsx, jsxs } from \"react/jsx-runtime\";\nfunction resolveGraphDirection(direction, viewport) {\n  if (direction !== \"auto\") return direction;\n  return viewport.height > viewport.width ? \"LR\" : \"TB\";\n}\nfunction space(token) {\n  const value = getTokenValue(token, \"space\");\n  return typeof value === \"number\" ? value : 0;\n}\nfunction useStrokeWidth() {\n  const preset = usePresetContext();\n  const knobs = preset ? { ...preset.preset.knobs, ...preset.overrides } : defaultKnobs;\n  return edgeStrokeWidth(knobs.borderWidth);\n}\nfunction useColour() {\n  const theme = useTheme();\n  return (token) => {\n    const value = theme[themeTokenName(token)]?.get?.();\n    return typeof value === \"string\" ? value : \"currentColor\";\n  };\n}\nfunction GraphLayout({\n  nodes,\n  edges,\n  renderNode,\n  direction = \"auto\",\n  positions,\n  spacing,\n  dots = true,\n  frozen,\n  testID = \"graph-layout\"\n}) {\n  const [viewport, setViewport] = useState({ width: 0, height: 0 });\n  const strokeWidth = useStrokeWidth();\n  const colour = useColour();\n  const resolved = resolveGraphDirection(direction, viewport);\n  const nodeSpacing = space(spacing?.node ?? \"$4\");\n  const rankSpacing = space(spacing?.rank ?? \"$8\");\n  const margin = space(\"$4\");\n  const pitch = space(\"$6\");\n  const plan = useMemo(\n    () => planGraphLayout({\n      nodes,\n      edges,\n      direction: resolved,\n      nodeSpacing,\n      rankSpacing,\n      margin,\n      strokeWidth,\n      wrapWidth: viewport.width > 0 ? viewport.width - margin * 2 : void 0,\n      positions\n    }),\n    [\n      nodes,\n      edges,\n      resolved,\n      nodeSpacing,\n      rankSpacing,\n      margin,\n      strokeWidth,\n      viewport.width,\n      positions\n    ]\n  );\n  const onLayout = (event) => {\n    const { width, height } = event.nativeEvent.layout;\n    if (width !== viewport.width || height !== viewport.height) setViewport({ width, height });\n  };\n  const content = {\n    width: Math.max(plan.width, viewport.width),\n    height: Math.max(plan.height, viewport.height)\n  };\n  return /* @__PURE__ */ jsx(View, { flex: 1, onLayout, testID, \"data-graph-direction\": resolved, children: viewport.width > 0 && viewport.height > 0 ? /* @__PURE__ */ jsx(\n    GraphViewport,\n    {\n      content,\n      viewport,\n      dots: dots ? { pitch, dot: strokeWidth, color: colour(\"$color-6\") } : void 0,\n      frozen,\n      children: /* @__PURE__ */ jsxs(View, { width: content.width, height: content.height, children: [\n        /* @__PURE__ */ jsx(GraphSvg, { width: plan.width, height: plan.height, children: plan.edges.map((edge) => {\n          const paint = colour(edge.stroke);\n          return /* @__PURE__ */ jsxs(Fragment, { children: [\n            /* @__PURE__ */ jsx(\n              GraphPath,\n              {\n                d: edge.d,\n                stroke: paint,\n                strokeWidth: edge.emphasis ? strokeWidth * 2 : strokeWidth,\n                dash: edge.dash\n              }\n            ),\n            /* @__PURE__ */ jsx(GraphPath, { d: edge.head, fill: paint })\n          ] }, edge.id);\n        }) }),\n        plan.nodes.map((node) => /* @__PURE__ */ jsx(\n          View,\n          {\n            position: \"absolute\",\n            left: node.x,\n            top: node.y,\n            width: node.width,\n            height: node.height,\n            testID: `graph-node-${node.id}`,\n            children: renderNode(node.id)\n          },\n          node.id\n        ))\n      ] })\n    }\n  ) : null });\n}\nexport {\n  GraphLayout,\n  resolveGraphDirection\n};\n//# sourceMappingURL=GraphLayout.js.map\n"],"mappings":";;;;;;;;;;AAQA,SAAS,sBAAsB,WAAW,UAAU;CAClD,IAAI,cAAc,QAAQ,OAAO;CACjC,OAAO,SAAS,SAAS,SAAS,QAAQ,OAAO;AACnD;AACA,SAAS,MAAM,OAAO;CACpB,MAAM,QAAQ,cAAc,OAA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