{"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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,oBAA4D;AAE5D,uBAAkB;AAClB,wCAA2C;AAC3C,iBAAuB;AACvB,+BAA2B;AAC3B,mBAAmC;AACnC,oCAAwC;AACxC,qCAAwC;AAEjC,MAAM,mBAAmB,OAC9B,0BACA,oBAAgB,gCAAiB,GACjC,iBAAiB,iCACS;AAC1B,MAAI;AACF,UAAM,EAAE,OAAO,IAAI;AAEnB,iCAAU,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;AACA,UAAM,sBAAsB,kBACzB,IAAI,CAAC,SAAS,KAAK,GAAG,EACtB,OAAO,OAAO;AAGjB,sBAAO,KAAK,qBAAqB,CAAC,CAAC;AAGnC,sBAAO;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;AAE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