{"version":3,"sources":["../../../src/loadDictionaries/loadDictionaries.ts"],"sourcesContent":["// @ts-ignore @intlayer/backend is not build yet\nimport type { DictionaryAPI } from '@intlayer/backend';\nimport { appLogger, ESMxCJSRequire, getConfiguration } from '@intlayer/config';\nimport type { Dictionary } from '@intlayer/core';\nimport merge from 'deepmerge';\nimport { fetchDistantDictionaryKeys } from '../fetchDistantDictionaryKeys';\nimport { logger } from '../log';\nimport { mergeByKey } from '../mergeDictionaries';\nimport { sortAlphabetically } from '../utils';\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 { 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    const localDictionaryKeys = localDictionaries\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      localDictionaries.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    let mergedDictionaries = localDictionaries;\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        if (editor.dictionaryPriorityStrategy === 'distant_first') {\n          // Merge the dictionaries\n          mergedDictionaries = merge(localDictionaries, distantDictionaries, {\n            arrayMerge: mergeByKey('key'),\n          });\n        } else {\n          // Merge the dictionaries\n          mergedDictionaries = merge(distantDictionaries, localDictionaries, {\n            arrayMerge: mergeByKey('key'),\n          });\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 mergedDictionaries;\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":"AAEA,SAAS,WAAW,gBAAgB,wBAAwB;AAE5D,OAAO,WAAW;AAClB,SAAS,kCAAkC;AAC3C,SAAS,cAAc;AACvB,SAAS,kBAAkB;AAC3B,SAAS,0BAA0B;AACnC,SAAS,+BAA+B;AACxC,SAAS,+BAA+B;AAEjC,MAAM,mBAAmB,OAC9B,0BACA,gBAAgB,iBAAiB,GACjC,iBAAiB,mBACS;AAC1B,MAAI;AACF,UAAM,EAAE,OAAO,IAAI;AAEnB,cAAU,iBAAiB,EAAE,WAAW,KAAK,CAAC;AAE9C,UAAM,QAAQ,MAAM,QAAQ,wBAAwB,IAChD,2BACA,CAAC,wBAAwB;AAE7B,UAAM,oBAAkC,MAAM;AAAA,MAC5C;AAAA,MACA;AAAA,IACF;AACA,UAAM,sBAAsB,kBACzB,IAAI,CAAC,SAAS,KAAK,GAAG,EACtB,OAAO,OAAO;AAGjB,WAAO,KAAK,qBAAqB,CAAC,CAAC;AAGnC,WAAO;AAAA,MACL,kBAAkB,IAAI,CAAC,UAAU;AAAA,QAC/B,eAAe,KAAK;AAAA,QACpB,MAAM;AAAA,QACN,QAAQ,EAAE,QAAQ,QAAQ;AAAA,MAC5B,EAAE;AAAA,IACJ;AAEA,QAAI,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