{"version":3,"sources":["../../../src/loadDictionaries/loadDictionaries.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport type { DictionaryAPI } from '@intlayer/backend';\nimport {\n  ESMxCJSRequire,\n  getAppLogger,\n  getConfiguration,\n} from '@intlayer/config';\nimport type { Dictionary } from '@intlayer/core';\nimport { fetchDistantDictionaryKeys } from '../fetchDistantDictionaryKeys';\nimport { logger } from '../log';\nimport { sortAlphabetically } from '../utils/sortAlphabetically';\nimport { loadContentDeclarations } from './loadContentDeclaration';\nimport { loadDistantDictionaries } from './loadDistantDictionaries';\n\nexport const loadDictionaries = async (\n  contentDeclarationsPaths: string[] | string,\n  configuration = getConfiguration(),\n  projectRequire = ESMxCJSRequire\n): Promise<Dictionary[]> => {\n  try {\n    const appLogger = getAppLogger(configuration);\n    const { editor } = configuration;\n\n    appLogger('Dictionaries:', { isVerbose: true });\n\n    const files = Array.isArray(contentDeclarationsPaths)\n      ? contentDeclarationsPaths\n      : [contentDeclarationsPaths];\n\n    const localDictionaries: Dictionary[] = await loadContentDeclarations(\n      files,\n      projectRequire\n    );\n\n    const filteredLocalDictionaries = localDictionaries.filter((dict) => {\n      const hasKey = Boolean(dict.key);\n      const hasContent = Boolean(dict.content);\n\n      if (!hasContent) {\n        console.error(\n          'Content declaration has no exported content',\n          dict.filePath\n        );\n      } else if (!hasKey) {\n        console.error('Content declaration has no key', dict.filePath);\n      }\n\n      return hasKey && hasContent;\n    });\n\n    const localDictionaryKeys = filteredLocalDictionaries\n      .map((dict) => dict.key)\n      .filter(Boolean); // Remove empty or undefined keys\n\n    // Initialize the logger with both local and distant dictionaries\n    logger.init(localDictionaryKeys, []);\n\n    // Update logger statuses for local dictionaries\n    logger.updateStatus(\n      filteredLocalDictionaries.map((dict) => ({\n        dictionaryKey: dict.key,\n        type: 'local',\n        status: { status: 'built' },\n      }))\n    );\n\n    let distantDictionaries: DictionaryAPI[] = [];\n    let distantDictionaryKeys: string[] = [];\n\n    if (editor.clientId && editor.clientSecret) {\n      try {\n        // Fetch distant dictionary keys\n        distantDictionaryKeys = await fetchDistantDictionaryKeys();\n\n        const orderedDistantDictionaryKeys =\n          distantDictionaryKeys.sort(sortAlphabetically);\n\n        // Add distant dictionaries to the logger\n        logger.addDictionaryKeys('distant', orderedDistantDictionaryKeys);\n\n        // Fetch distant dictionaries\n        distantDictionaries = await loadDistantDictionaries({\n          dictionaryKeys: orderedDistantDictionaryKeys,\n        });\n      } catch (_error) {\n        appLogger('Error during fetching distant dictionaries', {\n          level: 'error',\n        });\n      }\n    }\n\n    // Ensure the logger is stopped\n    logger.stop();\n\n    return [...filteredLocalDictionaries, ...distantDictionaries];\n  } catch (error) {\n    // Ensure the logger is stopped\n    logger.stop();\n\n    throw error; // Re-throw the error after logging\n  }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,oBAIO;AAEP,wCAA2C;AAC3C,iBAAuB;AACvB,gCAAmC;AACnC,oCAAwC;AACxC,qCAAwC;AAEjC,MAAM,mBAAmB,OAC9B,0BACA,oBAAgB,gCAAiB,GACjC,iBAAiB,iCACS;AAC1B,MAAI;AACF,UAAM,gBAAY,4BAAa,aAAa;AAC5C,UAAM,EAAE,OAAO,IAAI;AAEnB,cAAU,iBAAiB,EAAE,WAAW,KAAK,CAAC;AAE9C,UAAM,QAAQ,MAAM,QAAQ,wBAAwB,IAChD,2BACA,CAAC,wBAAwB;AAE7B,UAAM,oBAAkC,UAAM;AAAA,MAC5C;AAAA,MACA;AAAA,IACF;AAEA,UAAM,4BAA4B,kBAAkB,OAAO,CAAC,SAAS;AACnE,YAAM,SAAS,QAAQ,KAAK,GAAG;AAC/B,YAAM,aAAa,QAAQ,KAAK,OAAO;AAEvC,UAAI,CAAC,YAAY;AACf,gBAAQ;AAAA,UACN;AAAA,UACA,KAAK;AAAA,QACP;AAAA,MACF,WAAW,CAAC,QAAQ;AAClB,gBAAQ,MAAM,kCAAkC,KAAK,QAAQ;AAAA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