import { parallelize } from "./utils/parallelize.mjs";
import { getIntlayerAPIProxy } from "./api/dist/esm/proxy.mjs";
import { getAppLogger, getConfiguration, x } from "@intlayer/config";

//#region ../@intlayer/webpack/dist/esm/chokidar/dist/esm/fetchDistantDictionaries.mjs
/**
* Fetch distant dictionaries and update the logger with their statuses.
*/
const fetchDistantDictionaries = async (options, onStatusUpdate) => {
	const config = getConfiguration();
	const appLogger = getAppLogger(config);
	try {
		const intlayerAPI = 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 parallelize(distantDictionariesKeys, async (dictionaryKey) => await processDictionary(dictionaryKey))).filter((dict) => dict !== void 0);
	} catch (_error) {
		appLogger(`${x} Failed to fetch distant dictionaries`, { level: "error" });
		return [];
	}
};

//#endregion
export { fetchDistantDictionaries };
//# sourceMappingURL=fetchDistantDictionaries.mjs.map