import { SDTFError, TokenState } from '@specifyapp/specify-design-token-format'; import type { PossibleTextStylesKeysSchemaType } from '../definition.js'; import { dataOfToken, makeRenderer } from './template.js'; import { textStyleValueToScssMixin } from './tokenTypes/textStyle.js'; import { ParsersEngineWarningMessage } from '../../../parsersEngine/index.js'; import { specifyErrors } from '../../../errors/index.js'; export const convertTokens = ( tokens: Array, { tokenNameTemplate, exclude, include, genericFamily, }: { tokenNameTemplate: string; exclude: PossibleTextStylesKeysSchemaType; include: PossibleTextStylesKeysSchemaType; genericFamily: string; }, ) => { const renderVariable = makeRenderer(tokenNameTemplate); const outputs = [] as Array<{ variableName: string; value: Record }>; const warningMessages = [] as Array; for (const token of tokens) { try { const valueByMode = token.matchJSONValueByType( { textStyle: v => textStyleValueToScssMixin(v, { exclude, include, genericFamily, }), }, () => undefined, ); if (!valueByMode) continue; const modes = Object.keys(valueByMode); for (const mode of modes) { const variableName = renderVariable( dataOfToken(token, modes.length > 1 ? mode : undefined), ); outputs.push({ variableName, value: valueByMode[mode] }); } } catch (error) { if ((error as SDTFError).errorKey === 'SDTF_HAS_UNRESOLVABLE_ALIASES') { warningMessages.push({ type: 'warning', content: `The token ${token.path.toString()} has unresolvable aliases.`, errorKey: specifyErrors.PARSERS_ENGINE_PARSER_EXECUTION_FAILED.errorKey, }); } } } return { outputs, warningMessages }; };