import { SpecifyDesignTokenTypeName, TokenState } from '@specifyapp/specify-design-token-format'; import { jsonValueMatcher, matchJsonValue } from '../../utils/jsonValueMatcher.js'; export type AstMetadataPaths = { [type in SpecifyDesignTokenTypeName]?: Set }; export type AstMetadata = { types?: Set; paths: Set; modes: Set; }; export type ValueWithMode = { [mode: string]: Value }; export type AstToken = { path: string; value: ValueWithMode; }; export type AstData = { metadata: AstMetadata; tokens: Map; }; export type Ast = { dataByType: { [tokenType in SpecifyDesignTokenTypeName]?: AstData }; collectionModes: { [collectionName: string]: Array }; collectionPaths: { [collectionName: string]: Array }; }; export type PrimitiveValue = | string | number | Array>> | Record; isCode?: true }>; export type Value = { value: PrimitiveValue; isCode?: true }; export type Matcher = jsonValueMatcher; export function tokenToAstTokenValue(token: TokenState, matcher: Matcher): AstToken | undefined { if (!token.isFullyResolvable) { return undefined; } const outputByMode = token.modes.reduce((acc, mode) => { const output = matchJsonValue( matcher, _ => { return undefined; }, token, mode, ); if (!output) return acc; acc[mode] = output; return acc; }, {} as { [mode: string]: Value }); if (Object.values(outputByMode).every(v => !v)) return undefined; return { path: token.path.toString(), value: outputByMode }; }