const require_logger = require('../../../config/dist/esm/logger.cjs');
const require_getConfiguration = require('../../../config/dist/esm/configFile/getConfiguration.cjs');
const require_parallelize = require('./utils/parallelize.cjs');
const require_proxy = require('./api/dist/esm/proxy.cjs');

//#region ../@intlayer/chokidar/dist/esm/fetchDistantDictionaries.mjs
/**
* Fetch distant dictionaries and update the logger with their statuses.
*/
const fetchDistantDictionaries = async (options, onStatusUpdate) => {
	const config = require_getConfiguration.getConfiguration();
	const appLogger = require_logger.getAppLogger(config);
	try {
		const intlayerAPI = require_proxy.getIntlayerAPIProxy(void 0, config);
		const distantDictionariesKeys = options.dictionaryKeys;
		const processDictionary = async (dictionaryKey) => {
			onStatusUpdate?.([{
				dictionaryKey,
				type: "remote",
				status: "fetching"
			}]);
			try {
				const distantDictionary = (await intlayerAPI.dictionary.getDictionary(dictionaryKey)).data;
				if (!distantDictionary) throw new Error(`Dictionary ${dictionaryKey} not found on remote`);
				onStatusUpdate?.([{
					dictionaryKey,
					type: "remote",
					status: "fetched"
				}]);
				return distantDictionary;
			} catch (error) {
				onStatusUpdate?.([{
					dictionaryKey,
					type: "remote",
					status: "error",
					error: `Error fetching dictionary ${dictionaryKey}: ${error}`
				}]);
				return;
			}
		};
		return (await require_parallelize.parallelize(distantDictionariesKeys, async (dictionaryKey) => await processDictionary(dictionaryKey))).filter((dict) => dict !== void 0);
	} catch (_error) {
		appLogger(`${require_logger.x} Failed to fetch distant dictionaries`, { level: "error" });
		return [];
	}
};

//#endregion
exports.fetchDistantDictionaries = fetchDistantDictionaries;
//# sourceMappingURL=fetchDistantDictionaries.cjs.map