import { type SDTFEngine, createSDTFEngine } from '@specifyapp/specify-design-token-format'; import { DeriveBuiltInParserHandlerFromDefinition } from '../../internals/createBuiltInParserDefinition.js'; import type { ToCssFontImportParserDefinition } from './definition.js'; import { SpecifyError, specifyErrors } from '../../../errors/index.js'; import { merge } from 'lodash-es'; import { convertTokens } from './convertTokens.js'; import { convertToFontFaces } from './convertToFontFaces.js'; export const toCssFontImportHandler: DeriveBuiltInParserHandlerFromDefinition< ToCssFontImportParserDefinition > = async (previous, toolbox, parserOptions, outputOptions, _context) => { let sdtfEngine: SDTFEngine; switch (previous.type) { case 'SDTF': { sdtfEngine = createSDTFEngine(previous.graph); break; } case 'SDTF Engine': { sdtfEngine = previous.engine; break; } default: { throw new SpecifyError({ errorKey: specifyErrors.PARSERS_ENGINE_INVALID_PARSER_INPUT.errorKey, publicMessage: `${ (previous as any).type } is not a valid input for the to-css-custom-properties 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.`, }); const defaultOptions = { formats: ['woff', 'woff2'], fontsPath: '', includeFontWeight: true, genericFamily: '', fontDisplay: 'swap', }; const finalOptions = merge(defaultOptions, parserOptions); const { outputs, warningMessages } = convertTokens( sdtfEngine.query.getAllTokenStates(), finalOptions, ); if (warningMessages.length > 0) { warningMessages.forEach(warningMessage => { toolbox.populateMessage(warningMessage); }); } toolbox.populateOutput({ type: 'files', files: [ { path: outputOptions.filePath, content: { type: 'text', text: convertToFontFaces(outputs), }, }, ], }); return { type: 'SDTF Engine', engine: sdtfEngine, }; };