import { getIntlayerAPIProxy } from "../api/dist/esm/proxy.mjs";
import { fetchDistantDictionaries } from "../fetchDistantDictionaries.mjs";
import { sortAlphabetically } from "../utils/sortAlphabetically.mjs";
import { getRemoteDictionaries } from "../remote-dictionaries-entry/dist/esm/index.mjs";
import { getConfiguration } from "@intlayer/config";

//#region ../@intlayer/cli/dist/esm/chokidar/dist/esm/loadDictionaries/loadRemoteDictionaries.mjs
const formatDistantDictionaries = (dictionaries) => dictionaries.map((dict) => ({
	...dict,
	localId: `${dict.key}::remote::${dict.id}`,
	location: "remote"
}));
const loadRemoteDictionaries = async (configuration = getConfiguration(), onStatusUpdate, options) => {
	const { editor } = configuration;
	const remoteDictionariesRecord = getRemoteDictionaries(configuration);
	if (!Boolean(editor.clientId && editor.clientSecret)) return [];
	try {
		options?.onStartRemoteCheck?.();
		const distantDictionaryUpdateTimeStamp = (await getIntlayerAPIProxy(void 0, configuration).dictionary.getDictionariesUpdateTimestamp()).data;
		if (!distantDictionaryUpdateTimeStamp) throw new Error("No distant dictionaries found");
		const dictionariesIdToFetch = Object.entries(distantDictionaryUpdateTimeStamp).filter(([dictionaryId, data]) => {
			if (!data.updatedAt) return true;
			const local = remoteDictionariesRecord[data.key]?.find((dictionary) => dictionary.id === dictionaryId);
			if (!local) return true;
			const localUpdatedAtRaw = local?.updatedAt;
			const localUpdatedAt = typeof localUpdatedAtRaw === "number" ? localUpdatedAtRaw : localUpdatedAtRaw ? new Date(localUpdatedAtRaw).getTime() : void 0;
			if (typeof localUpdatedAt !== "number") return true;
			return data.updatedAt > localUpdatedAt;
		});
		const cachedDictionaries = Object.values(remoteDictionariesRecord).flat().filter((dictionary) => {
			const remoteUpdatedAt = distantDictionaryUpdateTimeStamp[dictionary.id].updatedAt;
			const localUpdatedAtRaw = dictionary.updatedAt;
			const localUpdatedAt = typeof localUpdatedAtRaw === "number" ? localUpdatedAtRaw : localUpdatedAtRaw ? new Date(localUpdatedAtRaw).getTime() : void 0;
			return typeof localUpdatedAt === "number" && typeof remoteUpdatedAt === "number" && localUpdatedAt >= remoteUpdatedAt;
		});
		if (cachedDictionaries.length > 0) onStatusUpdate?.(cachedDictionaries.map((dictionary) => ({
			dictionaryKey: dictionary.key,
			type: "remote",
			status: "imported"
		})));
		const orderedDistantDictionaryKeys = dictionariesIdToFetch.map(([, data]) => data.key).sort(sortAlphabetically);
		if (orderedDistantDictionaryKeys.length > 0) onStatusUpdate?.(orderedDistantDictionaryKeys.map((key) => ({
			dictionaryKey: key,
			type: "remote",
			status: "pending"
		})));
		const distantDictionaries = formatDistantDictionaries(await fetchDistantDictionaries({ dictionaryKeys: orderedDistantDictionaryKeys }, onStatusUpdate));
		return [...cachedDictionaries, ...distantDictionaries];
	} catch (error) {
		options?.onError?.(error);
		return [];
	} finally {
		options?.onStopRemoteCheck?.();
	}
};

//#endregion
export { formatDistantDictionaries, loadRemoteDictionaries };
//# sourceMappingURL=loadRemoteDictionaries.mjs.map