{"version":3,"file":"fetchDistantDictionaries.mjs","names":[],"sources":["../../src/fetchDistantDictionaries.ts"],"sourcesContent":["import { getIntlayerAPIProxy } from '@intlayer/api';\n// @ts-ignore @intlayer/backend is not build yet\nimport type { DictionaryAPI } from '@intlayer/backend';\nimport { getAppLogger, x } from '@intlayer/config/logger';\nimport { getConfiguration } from '@intlayer/config/node';\nimport type { DictionariesStatus } from './loadDictionaries';\nimport { parallelize } from './utils/parallelize';\n\ntype FetchDistantDictionariesOptions = {\n  dictionaryKeys: string[];\n  newDictionariesPath?: string;\n  logPrefix?: string;\n};\n\n/**\n * Fetch distant dictionaries and update the logger with their statuses.\n */\nexport const fetchDistantDictionaries = async (\n  options: FetchDistantDictionariesOptions,\n  onStatusUpdate?: (status: DictionariesStatus[]) => void\n): Promise<DictionaryAPI[]> => {\n  const config = getConfiguration();\n  const appLogger = getAppLogger(config);\n  try {\n    const intlayerAPI = getIntlayerAPIProxy(undefined, config);\n\n    const distantDictionariesKeys = options.dictionaryKeys;\n    // Process dictionaries in parallel with a concurrency limit\n    const processDictionary = async (\n      dictionaryKey: string\n    ): Promise<DictionaryAPI | undefined> => {\n      onStatusUpdate?.([\n        {\n          dictionaryKey,\n          type: 'remote',\n          status: 'fetching',\n        },\n      ]);\n\n      try {\n        // Fetch the dictionary\n        const getDictionaryResult =\n          await intlayerAPI.dictionary.getDictionary(dictionaryKey);\n\n        const distantDictionary = getDictionaryResult.data;\n\n        if (!distantDictionary) {\n          throw new Error(`Dictionary ${dictionaryKey} not found on remote`);\n        }\n\n        onStatusUpdate?.([\n          { dictionaryKey, type: 'remote', status: 'fetched' },\n        ]);\n\n        return distantDictionary;\n      } catch (error) {\n        onStatusUpdate?.([\n          {\n            dictionaryKey,\n            type: 'remote',\n            status: 'error',\n            error: `Error fetching dictionary ${dictionaryKey}: ${error}`,\n          },\n        ]);\n        return undefined;\n      }\n    };\n\n    const result = await parallelize(\n      distantDictionariesKeys,\n      async (dictionaryKey) => await processDictionary(dictionaryKey)\n    );\n\n    // Remove undefined values\n    const filteredResult = result.filter(\n      (dict: DictionaryAPI | undefined): dict is DictionaryAPI =>\n        dict !== undefined\n    );\n\n    return filteredResult;\n  } catch (_error) {\n    appLogger(`${x} Failed to fetch distant dictionaries`, { level: 'error' });\n    return [];\n  }\n};\n"],"mappings":";;;;;;;;;AAiBA,MAAa,2BAA2B,OACtC,SACA,mBAC6B;CAC7B,MAAM,SAAS,iBAAiB;CAChC,MAAM,YAAY,aAAa,MAAM;CACrC,IAAI;EACF,MAAM,cAAc,oBAAoB,QAAW,MAAM;EAEzD,MAAM,0BAA0B,QAAQ;EAExC,MAAM,oBAAoB,OACxB,kBACuC;GACvC,iBAAiB,CACf;IACE;IACA,MAAM;IACN,QAAQ;GACV,CACF,CAAC;GAED,IAAI;IAKF,MAAM,qBAAoB,MAFlB,YAAY,WAAW,cAAc,aAAa,GAEZ;IAE9C,IAAI,CAAC,mBACH,MAAM,IAAI,MAAM,cAAc,cAAc,qBAAqB;IAGnE,iBAAiB,CACf;KAAE;KAAe,MAAM;KAAU,QAAQ;IAAU,CACrD,CAAC;IAED,OAAO;GACT,SAAS,OAAO;IACd,iBAAiB,CACf;KACE;KACA,MAAM;KACN,QAAQ;KACR,OAAO,6BAA6B,cAAc,IAAI;IACxD,CACF,CAAC;IACD;GACF;EACF;EAaA,QALuB,MANF,YACnB,yBACA,OAAO,kBAAkB,MAAM,kBAAkB,aAAa,CAChE,GAG8B,QAC3B,SACC,SAAS,MAGO;CACtB,SAAS,QAAQ;EACf,UAAU,GAAG,EAAE,wCAAwC,EAAE,OAAO,QAAQ,CAAC;EACzE,OAAO,CAAC;CACV;AACF"}