import type { ConvertDimensionParserDefinition, ConvertDimensionParserOptions, } from './definition.js'; import { createSDTFEngine, SDTFEngine, SpecifyDesignTokenTypeName, } from '@specifyapp/specify-design-token-format'; import { SpecifyError, specifyErrors } from '../../../errors/index.js'; import { getSdtfQuery } from '../../utils/getSdtfQuery.js'; import { DeriveBuiltInParserHandlerFromDefinition } from '../../internals/createBuiltInParserDefinition.js'; import { ParserToolbox } from '../../../parsersEngine/index.js'; import { updateToken } from './updateToken.js'; export const AUTHORIZED_TOKENS: Array = [ 'border', 'shadow', 'shadows', 'textStyle', 'dimension', 'spacing', 'spacings', 'blur', 'breakpoint', 'radii', 'radius', ]; export const convertDimensionHandler: DeriveBuiltInParserHandlerFromDefinition< ConvertDimensionParserDefinition > = async (previousDataBox, toolbox, parserOptions, _, _context) => { let sdtfEngine: SDTFEngine; switch (previousDataBox.type) { case 'SDTF': { sdtfEngine = createSDTFEngine(previousDataBox.graph, previousDataBox.metadata); break; } case 'SDTF Engine': { sdtfEngine = previousDataBox.engine; break; } default: { throw new SpecifyError({ errorKey: specifyErrors.PARSERS_ENGINE_INVALID_PARSER_INPUT.errorKey, publicMessage: `${ (previousDataBox as any).type } is not a valid input for the convert-dimension parser.`, }); } } convertDimensionInner(sdtfEngine, parserOptions, toolbox); return { type: 'SDTF Engine', engine: sdtfEngine, }; }; export function convertDimensionInner( engine: SDTFEngine, options: ConvertDimensionParserOptions, toolbox: ParserToolbox, ) { const query = getSdtfQuery(options.applyTo, { authorizedTokens: AUTHORIZED_TOKENS, tokensOnly: true, }); engine.query.run(query).forEach(token => { if (!token.isToken || !token.isFullyResolvable) return; /* v8 ignore start */ if (!AUTHORIZED_TOKENS.includes(token.type)) { // Unauthorized tokens can be targeted by a custom query toolbox.populateMessage({ type: 'warning', content: `Tried to update token "${token.path.toString()}" of type "${token.type}" but it's not supported by convert-dimension`, errorKey: specifyErrors.PARSERS_ENGINE_PARSER_EXECUTION_FAILED.errorKey, }); return; } /* v8 ignore stop */ updateToken( token, { targetUnit: options.toFormat, baseValue: options.baseValue ?? {}, excludeFormats: options.excludeFormats, includeFormats: options.includeFormats, applyToKeys: options.applyToKeys ?? {}, }, toolbox, ); }); }