{"version":3,"file":"fetchDistantDictionaries.cjs","names":["parallelize","x"],"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,sDAA2B;CACjC,MAAM,sDAAyB,OAAO;AACtC,KAAI;EACF,MAAM,qDAAkC,QAAW,OAAO;EAE1D,MAAM,0BAA0B,QAAQ;EAExC,MAAM,oBAAoB,OACxB,kBACuC;AACvC,oBAAiB,CACf;IACE;IACA,MAAM;IACN,QAAQ;IACT,CACF,CAAC;AAEF,OAAI;IAKF,MAAM,qBAAoB,MAFlB,YAAY,WAAW,cAAc,cAAc,EAEb;AAE9C,QAAI,CAAC,kBACH,OAAM,IAAI,MAAM,cAAc,cAAc,sBAAsB;AAGpE,qBAAiB,CACf;KAAE;KAAe,MAAM;KAAU,QAAQ;KAAW,CACrD,CAAC;AAEF,WAAO;YACA,OAAO;AACd,qBAAiB,CACf;KACE;KACA,MAAM;KACN,QAAQ;KACR,OAAO,6BAA6B,cAAc,IAAI;KACvD,CACF,CAAC;AACF;;;AAeJ,UALuB,MANFA,sCACnB,yBACA,OAAO,kBAAkB,MAAM,kBAAkB,cAAc,CAChE,EAG6B,QAC3B,SACC,SAAS,OAGQ;UACd,QAAQ;AACf,YAAU,GAAGC,0BAAE,wCAAwC,EAAE,OAAO,SAAS,CAAC;AAC1E,SAAO,EAAE"}