import { SDTFEngine, SpecifyDesignTokenFormat, createSDTFEngine, } from '@specifyapp/specify-design-token-format'; import type { DeriveBuiltInParserHandlerFromDefinition } from '../../internals/createBuiltInParserDefinition.js'; import type { ToSdtfParserDefinition } from './definition.js'; import { SpecifyError, specifyErrors } from '../../../errors/index.js'; export const toSdtfHandler: DeriveBuiltInParserHandlerFromDefinition< ToSdtfParserDefinition > = async (previousDataBox, toolbox, _parserOptions, outputConfiguration, _context) => { /* v8 ignore start */ let sdtfEngine: SDTFEngine; let sdtfTokenTree: SpecifyDesignTokenFormat; switch (previousDataBox.type) { case 'SDTF': { sdtfEngine = createSDTFEngine(previousDataBox.graph, previousDataBox.metadata); sdtfTokenTree = previousDataBox.graph; break; } case 'SDTF Engine': { sdtfEngine = previousDataBox.engine; sdtfTokenTree = sdtfEngine.renderJSONTree(); break; } default: { throw new SpecifyError({ errorKey: specifyErrors.PARSERS_ENGINE_INVALID_INPUT_TYPE.errorKey, publicMessage: `The input type ${ (previousDataBox as any).type } is not supported by the to-sdtf parser.`, }); } } switch (outputConfiguration?.type) { case 'file': { toolbox.populateOutput({ type: 'files', files: [ { path: outputConfiguration.filePath, content: { type: 'text', text: JSON.stringify(sdtfTokenTree, null, 2), }, }, ], }); break; } case 'text': { toolbox.populateOutput({ type: 'text', text: JSON.stringify(sdtfTokenTree, null, 2), }); break; } case 'SDTF': { toolbox.populateOutput({ type: 'SDTF', graph: sdtfTokenTree, }); break; } case 'JSON': { toolbox.populateOutput({ type: 'JSON', json: sdtfTokenTree, }); } default: { throw new SpecifyError({ errorKey: specifyErrors.PARSERS_ENGINE_INVALID_OUTPUT_TYPE.errorKey, publicMessage: `The output type ${outputConfiguration?.type} is not supported by the to-sdtf parser.`, }); } } return { type: 'SDTF Engine', engine: sdtfEngine, }; /* v8 ignore stop */ };