{"version":3,"file":"dictionary.controller.mjs","names":["writeContentDeclarationEditor"],"sources":["../../src/controllers/dictionary.controller.ts"],"sourcesContent":["import { join } from 'node:path';\nimport { getConfiguration } from '@intlayer/config/node';\nimport { clearModuleCache } from '@intlayer/config/utils';\nimport {\n  type DictionaryStatus,\n  writeContentDeclaration as writeContentDeclarationEditor,\n} from '@intlayer/engine/build';\nimport type { Dictionary } from '@intlayer/types/dictionary';\nimport {\n  getUnmergedDictionaries,\n  type UnmergedDictionaries,\n} from '@intlayer/unmerged-dictionaries-entry';\nimport { formatResponse, type ResponseData } from '@utils/responseData';\nimport type { FastifyReply, FastifyRequest } from 'fastify';\nimport { t } from 'fastify-intlayer';\n\nexport type GetEditorDictionariesResult = ResponseData<UnmergedDictionaries>;\n\n/**\n * Get the Intlayer configuration\n */\nexport const getDictionaries = async (\n  _req: FastifyRequest,\n  res: FastifyReply\n): Promise<void> => {\n  try {\n    const formattedResponse = formatResponse<UnmergedDictionaries>({\n      data: getUnmergedDictionaries(),\n    });\n\n    return res.send(formattedResponse);\n  } catch (err) {\n    const errorMessage = (err as { message?: string; status?: number }) ?? {\n      message: 'Internal Server Error',\n      status: 500,\n    };\n\n    const formattedErrorResponse = formatResponse<UnmergedDictionaries>({\n      error: {\n        message: errorMessage.message ?? 'Internal Server Error',\n        code: 'INTERNAL_SERVER_ERROR',\n        title: 'Internal Server Error',\n      },\n      status: errorMessage.status ?? 500,\n    });\n\n    return res.send(formattedErrorResponse);\n  }\n};\n\nexport type WriteContentDeclarationBody = { dictionary: Dictionary };\ntype WriteContentDeclarationResultData = {\n  status: DictionaryStatus;\n  path: string;\n};\nexport type WriteContentDeclarationResult =\n  ResponseData<WriteContentDeclarationResultData>;\n\n/**\n * Adds a new dictionary to the database.\n */\nexport const writeContentDeclaration = async (\n  req: FastifyRequest<{ Body: WriteContentDeclarationBody }>,\n  res: FastifyReply\n): Promise<void> => {\n  try {\n    const dictionaryData = req.body.dictionary;\n\n    const config = getConfiguration();\n\n    const result = await writeContentDeclarationEditor(dictionaryData, config);\n\n    // Clear cache to hot reload the dictionaries\n    const dictionariesPath = join(\n      config.system.mainDir,\n      `unmerged_dictionaries.cjs`\n    );\n    clearModuleCache(dictionariesPath);\n\n    let description = '';\n\n    switch (result.status) {\n      case 'created': {\n        description = t({\n          en: 'Content declaration created successfully',\n          fr: 'Déclaration de contenu créée avec succès',\n          es: 'Declaración de contenido creada con éxito',\n        });\n        break;\n      }\n\n      case 'updated': {\n        description = t({\n          en: 'Content declaration updated successfully',\n          fr: 'Déclaration de contenu mise à jour avec succès',\n          es: 'Declaración de contenido actualizada con éxito',\n        });\n        break;\n      }\n      case 'reimported in JSON': {\n        description = t({\n          en: 'Content declaration reimported in JSON successfully',\n          fr: 'Déclaration de contenu réimportée en JSON avec succès',\n          es: 'Declaración de contenido reimportada en JSON con éxito',\n        });\n        break;\n      }\n      case 'new content file': {\n        description = t({\n          en: 'Content declaration new content file successfully',\n          fr: 'Déclaration de contenu réimportée dans un nouveau emplacement avec succès',\n          es: 'Declaración de contenido reimportada en un nuevo lugar con éxito',\n        });\n        break;\n      }\n      default: {\n        description = t({\n          en: 'Content declaration written successfully',\n          fr: 'Déclaration de contenu écrite avec succès',\n          es: 'Declaración de contenido escrita con éxito',\n        });\n        break;\n      }\n    }\n\n    const formattedResponse = formatResponse<WriteContentDeclarationResultData>(\n      {\n        data: result,\n        message: t({\n          en: 'Content declaration written',\n          fr: 'Déclaration de contenu écrite',\n          es: 'Declaración de contenido escrita',\n        }),\n        description,\n      }\n    );\n\n    return res.send(formattedResponse);\n  } catch (err) {\n    const errorMessage = (err as { message?: string; status?: number }) ?? {\n      message: 'Internal Server Error',\n      status: 500,\n    };\n\n    console.error(errorMessage);\n\n    const formattedErrorResponse =\n      formatResponse<WriteContentDeclarationResultData>({\n        error: {\n          message: errorMessage.message ?? 'Internal Server Error',\n          code: 'INTERNAL_SERVER_ERROR',\n          title: 'Internal Server Error',\n        },\n        status: errorMessage.status ?? 500,\n      });\n\n    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