import { built_default } from "../config/dist/esm/built.mjs";

//#region ../@intlayer/chokidar/dist/esm/core/dist/esm/dictionaryManipulator/orderDictionaries.mjs
/**
* Orders dictionaries based on the dictionary priority strategy.
*
* @param dictionaries - Array of dictionaries to order
* @param priorityStrategy - The priority strategy ('local_first' or 'distant_first')
* @returns Ordered array of dictionaries
*/
const orderDictionaries = (dictionaries, configuration = built_default) => {
	const { editor } = configuration;
	const { dictionaryPriorityStrategy } = editor;
	if (dictionaries.length <= 1) return dictionaries;
	const withIndex = dictionaries.map((dictionary, index) => ({
		dictionary,
		index
	}));
	const getPriority = (dictionary) => {
		const p = dictionary.priority ?? 0;
		return Number.isFinite(p) ? p : 0;
	};
	const getLocationWeight = (d) => {
		const location = d.location;
		if (location === void 0) return 2;
		if (dictionaryPriorityStrategy === "distant_first") return location === "remote" ? 0 : 1;
		return location === "local" ? 0 : 1;
	};
	withIndex.sort((a, b) => {
		const aAuto = a.dictionary.filled ? 1 : 0;
		const bAuto = b.dictionary.filled ? 1 : 0;
		if (aAuto !== bAuto) return aAuto - bAuto;
		const aP = getPriority(a.dictionary);
		const bP = getPriority(b.dictionary);
		if (aP !== bP) return bP - aP;
		const aLocation = getLocationWeight(a.dictionary);
		const bLocation = getLocationWeight(b.dictionary);
		if (aLocation !== bLocation) return aLocation - bLocation;
		return a.index - b.index;
	});
	return withIndex.map(({ dictionary }) => dictionary);
};

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