import { useState, useEffect } from 'react' import { create } from 'react-modal-promise' import { toolTips, useStore } from '../../useStore' import { BinaryPart, Value, VariableDeclarator } from '../../lang/wasm' import { getNodePathFromSourceRange, getNodeFromPath, } from '../../lang/queryAst' import { isSketchVariablesLinked } from '../../lang/std/sketchConstraints' import { TransformInfo, transformSecondarySketchLinesTagFirst, getTransformInfos, } from '../../lang/std/sketchcombos' import { GetInfoModal } from '../SetHorVertDistanceModal' import { createVariableDeclaration } from '../../lang/modifyAst' import { removeDoubleNegatives } from '../AvailableVarsHelpers' import { updateCursors } from '../../lang/util' const getModalInfo = create(GetInfoModal as any) export const SetAngleBetween = () => { const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } = useStore((s) => ({ guiMode: s.guiMode, ast: s.ast, updateAst: s.updateAst, selectionRanges: s.selectionRanges, programMemory: s.programMemory, setCursor: s.setCursor, })) const [enable, setEnable] = useState(false) const [transformInfos, setTransformInfos] = useState() useEffect(() => { if (!ast) return const paths = selectionRanges.codeBasedSelections.map(({ range }) => getNodePathFromSourceRange(ast, range) ) const nodes = paths.map( (pathToNode) => getNodeFromPath(ast, pathToNode).node ) const varDecs = paths.map( (pathToNode) => getNodeFromPath( ast, pathToNode, 'VariableDeclarator' )?.node ) const primaryLine = varDecs[0] const secondaryVarDecs = varDecs.slice(1) const isOthersLinkedToPrimary = secondaryVarDecs.every((secondary) => isSketchVariablesLinked(secondary, primaryLine, ast) ) const isAllTooltips = nodes.every( (node) => node?.type === 'CallExpression' && toolTips.includes(node.callee.name as any) ) const theTransforms = getTransformInfos( { ...selectionRanges, codeBasedSelections: selectionRanges.codeBasedSelections.slice(1), }, ast, 'setAngleBetween' ) setTransformInfos(theTransforms) const _enableEqual = secondaryVarDecs.length === 1 && isAllTooltips && isOthersLinkedToPrimary && theTransforms.every(Boolean) setEnable(_enableEqual) }, [guiMode, selectionRanges]) if (guiMode.mode !== 'sketch') return null return ( ) }