import { basename, dirname, isAbsolute, normalize, resolve } from "node:path";

//#region src/fill/formatAutoFilledFilePath.ts
const formatAutoFilledFilePath = (autoFillField, dictionaryKey, dictionaryFilePath, baseDir, locale) => {
	if (!autoFillField || typeof autoFillField !== "string") throw new Error("autoFillField must be a non-empty string");
	if (!dictionaryKey || typeof dictionaryKey !== "string") throw new Error("dictionaryKey must be a non-empty string");
	if (!dictionaryFilePath || typeof dictionaryFilePath !== "string") throw new Error("dictionaryFilePath must be a non-empty string");
	if (!baseDir || typeof baseDir !== "string") throw new Error("baseDir must be a non-empty string");
	const originalFileName = basename(dictionaryFilePath).split(".").slice(0, -2).join(".");
	let result = autoFillField.replace(/\{\{key\}\}/g, originalFileName).replace(/\{\{fileName\}\}/g, originalFileName);
	if (locale) result = result.replace(/\{\{locale\}\}/g, locale);
	const absoluteDictionaryPath = isAbsolute(dictionaryFilePath) ? dictionaryFilePath : resolve(baseDir, dictionaryFilePath);
	if (result.startsWith("./") || result.startsWith("../")) return resolve(dirname(absoluteDictionaryPath), result);
	if (isAbsolute(result)) {
		const normalizedResult = normalize(result);
		const normalizedBaseDir = normalize(baseDir);
		if (result.startsWith("/") && !normalizedResult.startsWith(normalizedBaseDir)) {
			const relativeToBase = resolve(baseDir, result.substring(1));
			if (!result.startsWith("/usr/") && !result.startsWith("/etc/") && !result.startsWith("/var/") && !result.startsWith("/home/") && !result.startsWith("/Users/")) return relativeToBase;
		}
		return normalizedResult;
	}
	return normalize(result);
};

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