import { TokenState } from '@specifyapp/specify-design-token-format'; import { Ast, Matcher, tokenToAstTokenValue } from './tokenToAstToken.js'; export function tokensToAst(tokens: Array, matcher: Matcher) { const ast: Ast = { dataByType: {}, collectionModes: {}, collectionPaths: {} }; tokens.forEach(token => { const astTokenValue = tokenToAstTokenValue(token, matcher); if (!astTokenValue) return; if (!ast.dataByType[token.type]) { ast.dataByType[token.type] = { metadata: { paths: new Set(), modes: new Set(), }, tokens: new Map(), }; } const collection = token.getCollection(); if (!!collection) { ast.collectionModes[collection.name] = collection.allowedModes; } const value = astTokenValue.value; const modes = Object.keys(value); modes.forEach(mode => { ast.dataByType[token.type]!.metadata.modes.add(mode); }); ast.dataByType[token.type]!.tokens.set(astTokenValue.path, value); ast.dataByType[token.type]!.metadata.paths.add(astTokenValue.path); if (!!collection) { ast.collectionPaths[collection.name] ??= []; ast.collectionPaths[collection.name].push(astTokenValue.path); } }); return ast; }