import { SDTFEngine, createSDTFEngine } from '@specifyapp/specify-design-token-format'; import type { DeriveBuiltInParserHandlerFromDefinition } from '../../internals/createBuiltInParserDefinition.js'; import { SpecifyError, specifyErrors } from '../../../errors/index.js'; import type { ToFlutterParserDefinition } from './definition.js'; import { convertedToDart } from './convertedToDart.js'; import { convertTokens } from './convertTokens.js'; import { DEFAULT_TEMPLATE, validateTemplate } from './template.js'; export const toFlutterHandler: DeriveBuiltInParserHandlerFromDefinition< ToFlutterParserDefinition > = async (previousDataBox, toolbox, parserOptions, outputOptions, _context) => { let sdtfEngine: SDTFEngine; switch (previousDataBox.type) { case 'SDTF': { sdtfEngine = createSDTFEngine(previousDataBox.graph); 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 to-tailwind parser.`, }); } } if (outputOptions.type !== 'file') throw new SpecifyError({ errorKey: specifyErrors.PARSERS_ENGINE_INVALID_OUTPUT_TYPE.errorKey, publicMessage: `The output type ${outputOptions?.type} is not supported by the to-tailwind parser.`, }); // Will throw if template is not valid if (parserOptions?.tokenNameTemplate) validateTemplate(parserOptions.tokenNameTemplate); toolbox.populateOutput({ type: 'files', files: [ { path: outputOptions.filePath, content: { type: 'text', text: convertedToDart( convertTokens( sdtfEngine.query.getAllTokenStates(), parserOptions?.tokenNameTemplate ?? DEFAULT_TEMPLATE, ), ), }, }, ], }); return { type: 'SDTF Engine', engine: sdtfEngine, }; };