{
  "version": 3,
  "sources": ["../../../../../../src/lib/shapes/arrow/elbow/routes/routeArrowWithAutoEdgePicking.tsx"],
  "sourcesContent": ["import { exhaustiveSwitchError } from '@tldraw/editor'\nimport {\n\tElbowArrowRoute,\n\tElbowArrowSide,\n\tElbowArrowSideOpposites,\n\tElbowArrowSideReason,\n\tElbowArrowSides,\n} from '../definitions'\nimport { tryRouteArrow } from './elbowArrowRoutes'\nimport { ElbowArrowWorkingInfo } from './ElbowArrowWorkingInfo'\n\nexport function routeArrowWithAutoEdgePicking(\n\tinfo: ElbowArrowWorkingInfo,\n\treason: ElbowArrowSideReason\n): ElbowArrowRoute | null {\n\tlet idealRoute = null\n\tif (\n\t\t// +1 to bias us towards the x-axis. without this, we get flicker as we move an arrow locket\n\t\t// to 45 deg (as gapx/gapy are almost equal and the result depends on floating point\n\t\t// precision)\n\t\tMath.abs(info.gapX) + 1 > Math.abs(info.gapY) &&\n\t\tinfo.midX !== null\n\t) {\n\t\tif (info.gapX > 0) {\n\t\t\tidealRoute = tryRouteArrow(info, 'right', 'left')\n\t\t} else {\n\t\t\tidealRoute = tryRouteArrow(info, 'left', 'right')\n\t\t}\n\t} else {\n\t\tconst aRight = info.A.edges.right\n\t\tconst aLeft = info.A.edges.left\n\t\tconst bTop = info.B.edges.top\n\t\tconst bBottom = info.B.edges.bottom\n\n\t\tif (info.A.isPoint && info.B.isPoint) {\n\t\t\tif (info.gapY > 0) {\n\t\t\t\tidealRoute = tryRouteArrow(info, 'bottom', 'top')\n\t\t\t} else {\n\t\t\t\tidealRoute = tryRouteArrow(info, 'top', 'bottom')\n\t\t\t}\n\t\t} else if (\n\t\t\taRight &&\n\t\t\tbTop &&\n\t\t\t(aRight.expanded ?? aRight.value) <= bTop.crossTarget &&\n\t\t\taRight.crossTarget <= (bTop.expanded ?? bTop.value)\n\t\t) {\n\t\t\tidealRoute = tryRouteArrow(info, 'right', 'top')\n\t\t} else if (\n\t\t\taRight &&\n\t\t\tbBottom &&\n\t\t\t(aRight.expanded ?? aRight.value) <= bBottom.crossTarget &&\n\t\t\taRight.crossTarget >= (bBottom.expanded ?? bBottom.value)\n\t\t) {\n\t\t\tidealRoute = tryRouteArrow(info, 'right', 'bottom')\n\t\t} else if (\n\t\t\taLeft &&\n\t\t\tbTop &&\n\t\t\t(aLeft.expanded ?? aLeft.value) >= bTop.crossTarget &&\n\t\t\taLeft.crossTarget <= (bTop.expanded ?? bTop.value)\n\t\t) {\n\t\t\tidealRoute = tryRouteArrow(info, 'left', 'top')\n\t\t} else if (\n\t\t\taLeft &&\n\t\t\tbBottom &&\n\t\t\t(aLeft.expanded ?? aLeft.value) >= bBottom.crossTarget &&\n\t\t\taLeft.crossTarget >= (bBottom.expanded ?? bBottom.value)\n\t\t) {\n\t\t\tidealRoute = tryRouteArrow(info, 'left', 'bottom')\n\t\t} else if (info.gapY > 0 && info.midY !== null) {\n\t\t\tidealRoute = tryRouteArrow(info, 'bottom', 'top')\n\t\t} else if (info.gapY < 0 && info.midY !== null) {\n\t\t\tidealRoute = tryRouteArrow(info, 'top', 'bottom')\n\t\t}\n\t}\n\n\tif (idealRoute) {\n\t\tidealRoute.aEdgePicking = reason\n\t\tidealRoute.bEdgePicking = reason\n\t\treturn idealRoute\n\t}\n\n\tconst aAvailableSide = ElbowArrowSides.filter((side) => info.A.edges[side])\n\tconst bAvailableSides = ElbowArrowSides.filter((side) => info.B.edges[side])\n\n\tconst nonPartialRouteCandidates = aAvailableSide.flatMap((aSide) =>\n\t\tbAvailableSides.map((bSide) => [aSide, bSide, reason, reason] as const)\n\t)\n\n\treturn pickBest(info, nonPartialRouteCandidates)\n}\n\nexport function routeArrowWithPartialEdgePicking(\n\tinfo: ElbowArrowWorkingInfo,\n\taSide: ElbowArrowSide\n) {\n\tlet idealRoute = null\n\n\tconst aRight = info.A.edges.right\n\tconst aLeft = info.A.edges.left\n\tconst bTop = info.B.edges.top\n\tconst bBottom = info.B.edges.bottom\n\n\tswitch (aSide) {\n\t\tcase 'right':\n\t\t\tif (info.gapX > 0 && info.gapX > Math.abs(info.gapY) && info.midX !== null) {\n\t\t\t\tidealRoute = tryRouteArrow(info, 'right', 'left')\n\t\t\t} else if (\n\t\t\t\taRight &&\n\t\t\t\tbTop &&\n\t\t\t\t(aRight.expanded ?? aRight.value) <= bTop.crossTarget &&\n\t\t\t\taRight.crossTarget <= (bTop.expanded ?? bTop.value)\n\t\t\t) {\n\t\t\t\tidealRoute = tryRouteArrow(info, 'right', 'top')\n\t\t\t} else if (\n\t\t\t\taRight &&\n\t\t\t\tbBottom &&\n\t\t\t\t(aRight.expanded ?? aRight.value) <= bBottom.crossTarget &&\n\t\t\t\taRight.crossTarget >= (bBottom.expanded ?? bBottom.value)\n\t\t\t) {\n\t\t\t\tidealRoute = tryRouteArrow(info, 'right', 'bottom')\n\t\t\t}\n\t\t\tbreak\n\t\tcase 'left':\n\t\t\tif (info.gapX < 0 && Math.abs(info.gapX) > Math.abs(info.gapY) && info.midX !== null) {\n\t\t\t\tidealRoute = tryRouteArrow(info, 'left', 'right')\n\t\t\t} else if (\n\t\t\t\taLeft &&\n\t\t\t\tbTop &&\n\t\t\t\t(aLeft.expanded ?? aLeft.value) >= bTop.crossTarget &&\n\t\t\t\taLeft.crossTarget <= (bTop.expanded ?? bTop.value)\n\t\t\t) {\n\t\t\t\tidealRoute = tryRouteArrow(info, 'left', 'top')\n\t\t\t} else if (\n\t\t\t\taLeft &&\n\t\t\t\tbBottom &&\n\t\t\t\t(aLeft.expanded ?? aLeft.value) >= bBottom.crossTarget &&\n\t\t\t\taLeft.crossTarget >= (bBottom.expanded ?? bBottom.value)\n\t\t\t) {\n\t\t\t\tidealRoute = tryRouteArrow(info, 'left', 'bottom')\n\t\t\t}\n\t\t\tbreak\n\t\tcase 'top':\n\t\tcase 'bottom':\n\t\t\t// top and bottom are handled by the pickShortest approach below - it automatically\n\t\t\t// picks the path we would pick with heuristics anyway.\n\t\t\tbreak\n\t\tdefault:\n\t\t\texhaustiveSwitchError(aSide)\n\t}\n\n\tif (idealRoute) {\n\t\tidealRoute.aEdgePicking = 'manual'\n\t\tidealRoute.bEdgePicking = 'auto'\n\t\treturn idealRoute\n\t}\n\n\tswitch (aSide) {\n\t\tcase 'top':\n\t\t\treturn pickBest(info, [\n\t\t\t\t['top', 'bottom', 'manual', 'auto'],\n\t\t\t\t['top', 'right', 'manual', 'auto'],\n\t\t\t\t['top', 'left', 'manual', 'auto'],\n\t\t\t\t['top', 'top', 'manual', 'auto'],\n\t\t\t])\n\t\tcase 'bottom':\n\t\t\treturn pickBest(info, [\n\t\t\t\t['bottom', 'top', 'manual', 'auto'],\n\t\t\t\t['bottom', 'right', 'manual', 'auto'],\n\t\t\t\t['bottom', 'left', 'manual', 'auto'],\n\t\t\t\t['bottom', 'bottom', 'manual', 'auto'],\n\t\t\t])\n\t\tcase 'left':\n\t\t\treturn pickBest(info, [\n\t\t\t\t['left', 'right', 'manual', 'auto'],\n\t\t\t\t['left', 'bottom', 'manual', 'auto'],\n\t\t\t\t['left', 'left', 'manual', 'auto'],\n\t\t\t\t['left', 'top', 'manual', 'auto'],\n\t\t\t])\n\t\tcase 'right':\n\t\t\treturn pickBest(info, [\n\t\t\t\t['right', 'left', 'manual', 'auto'],\n\t\t\t\t['right', 'bottom', 'manual', 'auto'],\n\t\t\t\t['right', 'right', 'manual', 'auto'],\n\t\t\t\t['right', 'top', 'manual', 'auto'],\n\t\t\t])\n\t}\n}\n\nexport function routeArrowWithManualEdgePicking(\n\tinfo: ElbowArrowWorkingInfo,\n\taSide: ElbowArrowSide,\n\tbSide: ElbowArrowSide\n) {\n\tconst route = tryRouteArrow(info, aSide, bSide)\n\tif (route) return route\n\n\tif (info.A.isPoint && info.B.isPoint) {\n\t\treturn pickBest(info, [\n\t\t\t[ElbowArrowSideOpposites[aSide], ElbowArrowSideOpposites[bSide], 'manual', 'manual'],\n\t\t\t[aSide, ElbowArrowSideOpposites[bSide], 'manual', 'auto'],\n\t\t\t[ElbowArrowSideOpposites[aSide], bSide, 'auto', 'manual'],\n\t\t])\n\t} else if (info.A.isPoint) {\n\t\treturn tryRouteArrow(info, ElbowArrowSideOpposites[aSide], bSide)\n\t} else if (info.B.isPoint) {\n\t\treturn tryRouteArrow(info, aSide, ElbowArrowSideOpposites[bSide])\n\t}\n\n\treturn null\n}\nfunction pickBest(\n\tinfo: ElbowArrowWorkingInfo,\n\tedges: ReadonlyArray<\n\t\treadonly [ElbowArrowSide, ElbowArrowSide, ElbowArrowSideReason, ElbowArrowSideReason]\n\t>\n) {\n\tlet bestRoute: ElbowArrowRoute | null = null\n\tlet bestCornerCount = Infinity\n\tlet bestDistance = Infinity\n\tlet distanceBias = 0\n\tfor (const [aSide, bSide, aEdgePicking, bEdgePicking] of edges) {\n\t\tdistanceBias += 1\n\t\tconst route = tryRouteArrow(info, aSide, bSide)\n\t\tif (route) {\n\t\t\troute.aEdgePicking = aEdgePicking\n\t\t\troute.bEdgePicking = bEdgePicking\n\t\t\tif (route.points.length < bestCornerCount) {\n\t\t\t\tbestCornerCount = route.points.length\n\t\t\t\tbestDistance = route.distance\n\t\t\t\tbestRoute = route\n\t\t\t} else if (\n\t\t\t\troute.points.length === bestCornerCount &&\n\t\t\t\troute.distance + distanceBias < bestDistance\n\t\t\t) {\n\t\t\t\tbestDistance = route.distance\n\t\t\t\tbestRoute = route\n\t\t\t}\n\t\t}\n\t}\n\treturn bestRoute\n}\n"],
  "mappings": "AAAA,SAAS,6BAA6B;AACtC;AAAA,EAGC;AAAA,EAEA;AAAA,OACM;AACP,SAAS,qBAAqB;AAGvB,SAAS,8BACf,MACA,QACyB;AACzB,MAAI,aAAa;AACjB;AAAA;AAAA;AAAA;AAAA,IAIC,KAAK,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,KAAK,IAAI,KAC5C,KAAK,SAAS;AAAA,IACb;AACD,QAAI,KAAK,OAAO,GAAG;AAClB,mBAAa,cAAc,MAAM,SAAS,MAAM;AAAA,IACjD,OAAO;AACN,mBAAa,cAAc,MAAM,Q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