import { listGitFiles } from "@intlayer/chokidar";
import { getConfiguration } from "@intlayer/config";
import { join, relative } from "node:path";
import { getUnmergedDictionaries } from "@intlayer/unmerged-dictionaries-entry";

//#region src/getTargetDictionary.ts
const ensureArray = (value) => [value].flat();
const getTargetUnmergedDictionaries = async (options) => {
	const configuration = getConfiguration(options?.configOptions);
	const { baseDir } = configuration.content;
	const unmergedDictionariesRecord = getUnmergedDictionaries(configuration);
	let result = Object.values(unmergedDictionariesRecord).flat();
	if (typeof options?.file !== "undefined") {
		const relativeFilePaths = ensureArray(options?.file).map((file) => file.startsWith("/") ? relative(baseDir, file) : join("./", file));
		result = result.filter((dict) => dict.filePath && relativeFilePaths.includes(dict.filePath));
	}
	if (typeof options?.keys !== "undefined") result = result.filter((dict) => ensureArray(options?.keys)?.includes(dict.key));
	if (typeof options?.excludedKeys !== "undefined") result = result.filter((dict) => !ensureArray(options?.excludedKeys)?.includes(dict.key));
	if (typeof options?.pathFilter !== "undefined") result = result.filter((dict) => ensureArray(options?.pathFilter)?.includes(dict.filePath ?? ""));
	if (typeof options?.filter !== "undefined") result = result.filter(options?.filter);
	const gitOptions = options?.gitOptions;
	if (gitOptions) {
		const gitChangedFiles = await listGitFiles(gitOptions);
		if (gitChangedFiles) result = result.filter((dict) => {
			if (!dict.filePath) return false;
			return gitChangedFiles.some((gitFile) => dict.filePath === gitFile);
		});
	}
	return result;
};

//#endregion
export { ensureArray, getTargetUnmergedDictionaries };
//# sourceMappingURL=getTargetDictionary.mjs.map