{"version":3,"names":["clamp","getPushedThumbValues","resolveThumbCollision","behavior","values","currentValues","initialValues","pressedIndex","nextValue","min","max","step","minStepsBetweenValues","activeValues","baselineValues","range","length","value","thumbIndex","didSwap","minValueDifference","pressedInitialValue","epsilon","candidateValues","slice","previousNeighbor","nextNeighbor","lowerBound","upperBound","constrainedValue","pressedValueAfterClamp","Number","toFixed","movingForward","movingBackward","shouldSwapForward","shouldSwapBackward","targetIndex","initialValuesForPush","map","_","index","baseline","nextValueForTarget","Math","adjustedValues","neighborIndex","previousValue","nextValueAfter","neighborLowerBound","neighborUpperBound","restoredValue","nextValues"],"sources":["resolve-thumb-collision.ts"],"sourcesContent":["import { clamp } from \"./clamp\";\nimport { getPushedThumbValues } from \"./get-pushed-thumb-values\";\n\n/**\n * Strategy for how a moving thumb interacts with its neighbours.\n *\n * - `\"none\"`: thumbs cannot cross; the pressed thumb is clamped at its\n *   neighbour's boundary.\n * - `\"push\"`: the pressed thumb pushes its neighbours along.\n * - `\"swap\"`: thumbs swap identities when they cross.\n */\nexport type ThumbCollisionBehavior = \"none\" | \"push\" | \"swap\";\n\nexport interface ResolveThumbCollisionParams {\n\tbehavior: ThumbCollisionBehavior;\n\tvalues: readonly number[];\n\tcurrentValues?: readonly number[] | null | undefined;\n\tinitialValues?: readonly number[] | null | undefined;\n\tpressedIndex: number;\n\tnextValue: number;\n\tmin: number;\n\tmax: number;\n\tstep: number;\n\tminStepsBetweenValues: number;\n}\n\nexport interface ResolveThumbCollisionResult {\n\tvalue: number | number[];\n\tthumbIndex: number;\n\tdidSwap: boolean;\n}\n\n/**\n * Applies the chosen collision `behavior` to compute the new thumb values\n * when the thumb at `pressedIndex` attempts to move to `nextValue`. Returns\n * both the updated values and the resolved thumb index (swap may change it).\n */\nexport function resolveThumbCollision({\n\tbehavior,\n\tvalues,\n\tcurrentValues,\n\tinitialValues,\n\tpressedIndex,\n\tnextValue,\n\tmin,\n\tmax,\n\tstep,\n\tminStepsBetweenValues,\n}: ResolveThumbCollisionParams): ResolveThumbCollisionResult {\n\tconst activeValues = currentValues ?? values;\n\tconst baselineValues = initialValues ?? values;\n\tconst range = activeValues.length > 1;\n\n\tif (!range) {\n\t\treturn {\n\t\t\tvalue: nextValue,\n\t\t\tthumbIndex: 0,\n\t\t\tdidSwap: false,\n\t\t};\n\t}\n\n\tconst minValueDifference = step * minStepsBetweenValues;\n\n\tswitch (behavior) {\n\t\tcase \"swap\": {\n\t\t\tconst pressedInitialValue = activeValues[pressedIndex];\n\t\t\tconst epsilon = 1e-7;\n\t\t\tconst candidateValues = activeValues.slice();\n\t\t\tconst previousNeighbor = candidateValues[pressedIndex - 1];\n\t\t\tconst nextNeighbor = candidateValues[pressedIndex + 1];\n\n\t\t\tconst lowerBound =\n\t\t\t\tpreviousNeighbor != null ? previousNeighbor + minValueDifference : min;\n\t\t\tconst upperBound =\n\t\t\t\tnextNeighbor != null ? nextNeighbor - minValueDifference : max;\n\n\t\t\tconst constrainedValue = clamp(nextValue, lowerBound, upperBound);\n\t\t\tconst pressedValueAfterClamp = Number(constrainedValue.toFixed(12));\n\t\t\tcandidateValues[pressedIndex] = pressedValueAfterClamp;\n\n\t\t\tconst movingForward =\n\t\t\t\tpressedInitialValue != null && nextValue > pressedInitialValue;\n\t\t\tconst movingBackward =\n\t\t\t\tpressedInitialValue != null && nextValue < pressedInitialValue;\n\n\t\t\tconst shouldSwapForward =\n\t\t\t\tmovingForward &&\n\t\t\t\tnextNeighbor != null &&\n\t\t\t\tnextValue >= nextNeighbor - epsilon;\n\t\t\tconst shouldSwapBackward =\n\t\t\t\tmovingBackward &&\n\t\t\t\tpreviousNeighbor != null &&\n\t\t\t\tnextValue <= previousNeighbor + epsilon;\n\n\t\t\tif (!shouldSwapForward && !shouldSwapBackward) {\n\t\t\t\treturn {\n\t\t\t\t\tvalue: candidateValues,\n\t\t\t\t\tthumbIndex: pressedIndex,\n\t\t\t\t\tdidSwap: false,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst targetIndex = shouldSwapForward\n\t\t\t\t? pressedIndex + 1\n\t\t\t\t: pressedIndex - 1;\n\n\t\t\tconst initialValuesForPush = candidateValues.map((_, index) => {\n\t\t\t\tif (index === pressedIndex) {\n\t\t\t\t\treturn pressedValueAfterClamp;\n\t\t\t\t}\n\n\t\t\t\tconst baseline = baselineValues[index];\n\t\t\t\tif (baseline != null) {\n\t\t\t\t\treturn baseline;\n\t\t\t\t}\n\n\t\t\t\t// biome-ignore lint/style/noNonNullAssertion: index is within activeValues bounds\n\t\t\t\treturn activeValues[index]!;\n\t\t\t});\n\n\t\t\tlet nextValueForTarget = nextValue;\n\t\t\tif (shouldSwapForward) {\n\t\t\t\t// biome-ignore lint/style/noNonNullAssertion: targetIndex = pressedIndex + 1 is in range for swap-forward\n\t\t\t\tnextValueForTarget = Math.max(nextValue, candidateValues[targetIndex]!);\n\t\t\t} else {\n\t\t\t\t// biome-ignore lint/style/noNonNullAssertion: targetIndex = pressedIndex - 1 is in range for swap-backward\n\t\t\t\tnextValueForTarget = Math.min(nextValue, candidateValues[targetIndex]!);\n\t\t\t}\n\n\t\t\tconst adjustedValues = getPushedThumbValues({\n\t\t\t\tvalues: candidateValues,\n\t\t\t\tindex: targetIndex,\n\t\t\t\tnextValue: nextValueForTarget,\n\t\t\t\tmin,\n\t\t\t\tmax,\n\t\t\t\tstep,\n\t\t\t\tminStepsBetweenValues,\n\t\t\t\tinitialValues: initialValuesForPush,\n\t\t\t});\n\n\t\t\tconst neighborIndex = shouldSwapForward\n\t\t\t\t? targetIndex - 1\n\t\t\t\t: targetIndex + 1;\n\n\t\t\tif (neighborIndex >= 0 && neighborIndex < adjustedValues.length) {\n\t\t\t\tconst previousValue = adjustedValues[neighborIndex - 1];\n\t\t\t\tconst nextValueAfter = adjustedValues[neighborIndex + 1];\n\n\t\t\t\tlet neighborLowerBound =\n\t\t\t\t\tpreviousValue != null ? previousValue + minValueDifference : min;\n\t\t\t\tneighborLowerBound = Math.max(\n\t\t\t\t\tneighborLowerBound,\n\t\t\t\t\tmin + neighborIndex * minValueDifference,\n\t\t\t\t);\n\n\t\t\t\tlet neighborUpperBound =\n\t\t\t\t\tnextValueAfter != null ? nextValueAfter - minValueDifference : max;\n\t\t\t\tneighborUpperBound = Math.min(\n\t\t\t\t\tneighborUpperBound,\n\t\t\t\t\tmax -\n\t\t\t\t\t\t(adjustedValues.length - 1 - neighborIndex) * minValueDifference,\n\t\t\t\t);\n\n\t\t\t\tconst restoredValue = clamp(\n\t\t\t\t\tpressedValueAfterClamp,\n\t\t\t\t\tneighborLowerBound,\n\t\t\t\t\tneighborUpperBound,\n\t\t\t\t);\n\t\t\t\tadjustedValues[neighborIndex] = Number(restoredValue.toFixed(12));\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tvalue: adjustedValues,\n\t\t\t\tthumbIndex: targetIndex,\n\t\t\t\tdidSwap: true,\n\t\t\t};\n\t\t}\n\t\tcase \"push\": {\n\t\t\tconst nextValues = getPushedThumbValues({\n\t\t\t\tvalues: activeValues,\n\t\t\t\tindex: pressedIndex,\n\t\t\t\tnextValue,\n\t\t\t\tmin,\n\t\t\t\tmax,\n\t\t\t\tstep,\n\t\t\t\tminStepsBetweenValues,\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\tvalue: nextValues,\n\t\t\t\tthumbIndex: pressedIndex,\n\t\t\t\tdidSwap: false,\n\t\t\t};\n\t\t}\n\t\tdefault: {\n\t\t\tconst candidateValues = activeValues.slice();\n\t\t\tconst previousNeighbor = candidateValues[pressedIndex - 1];\n\t\t\tconst nextNeighbor = candidateValues[pressedIndex + 1];\n\n\t\t\tconst lowerBound =\n\t\t\t\tpreviousNeighbor != null ? previousNeighbor + minValueDifference : min;\n\t\t\tconst upperBound =\n\t\t\t\tnextNeighbor != null ? nextNeighbor - minValueDifference : max;\n\n\t\t\tconst constrainedValue = clamp(nextValue, lowerBound, upperBound);\n\t\t\tcandidateValues[pressedIndex] = Number(constrainedValue.toFixed(12));\n\n\t\t\treturn {\n\t\t\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