import { build } from "./build.mjs";
import { startEditor } from "./editor.mjs";
import { fill } from "./fill/fill.mjs";
import { listContentDeclaration } from "./listContentDeclaration.mjs";
import { liveSync } from "./liveSync.mjs";
import { pull } from "./pull.mjs";
import { push } from "./push/push.mjs";
import { pushConfig } from "./pushConfig.mjs";
import { reviewDoc } from "./reviewDoc.mjs";
import { testMissingTranslations } from "./test/index.mjs";
import { translateDoc } from "./translateDoc.mjs";
import { getConfig } from "./config.mjs";
import { getParentPackageJSON } from "./utils/getParentPackageJSON.mjs";
import { watchContentDeclaration } from "./watch.mjs";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { getConfiguration } from "@intlayer/config";
import { Command } from "commander";

//#region ../@intlayer/cli/dist/esm/cli.mjs
const dirname$1 = typeof import.meta.url === "string" ? dirname(fileURLToPath(import.meta.url)) : __dirname;
const packageJson = getParentPackageJSON(dirname$1);
const configurationOptions = [
	["--env-file [envFile]", "Environment file"],
	["-e, --env [env]", "Environment"],
	["--base-dir [baseDir]", "Base directory"],
	["--no-cache [noCache]", "No cache"],
	...[["--verbose", "Verbose (default to true using CLI)"], ["--prefix [prefix]", "Prefix"]]
];
const aiOptions = [
	["--provider [provider]", "Provider"],
	["--temperature [temperature]", "Temperature"],
	["--model [model]", "Model"],
	["--api-key [apiKey]", "Provider API key"],
	["--custom-prompt [prompt]", "Custom prompt"],
	["--application-context [applicationContext]", "Application context"]
];
const gitOptions = [
	["--git-diff [gitDiff]", "Git diff mode - Check git diff between two refs"],
	["--git-diff-base [gitDiffBase]", "Git diff base ref"],
	["--git-diff-current [gitDiffCurrent]", "Git diff current ref"],
	["--uncommitted [uncommitted]", "Uncommitted"],
	["--unpushed [unpushed]", "Unpushed"],
	["--untracked [untracked]", "Untracked"]
];
const extractKeysFromOptions = (options, keys) => keys.filter((key) => options[key]);
/**
* Helper functions to apply common options to commands
*/
const applyOptions = (command, options) => {
	options.forEach(([flag, description]) => {
		command.option(flag, description);
	});
	return command;
};
const removeUndefined = (obj) => Object.fromEntries(Object.entries(obj).filter(([_, value]) => value !== void 0));
const applyConfigOptions = (command) => applyOptions(command, configurationOptions);
const applyAIOptions = (command) => applyOptions(command, aiOptions);
const applyGitOptions = (command) => applyOptions(command, gitOptions);
const extractAiOptions = (options) => {
	const { apiKey, provider, model, temperature, applicationContext, customPrompt } = options;
	const configuration = getConfiguration();
	return removeUndefined({
		apiKey: apiKey ?? configuration.ai?.apiKey,
		provider: provider ?? configuration.ai?.provider,
		model: model ?? configuration.ai?.model,
		temperature: temperature ?? configuration.ai?.temperature,
		applicationContext: applicationContext ?? configuration.ai?.applicationContext,
		customPrompt: customPrompt ?? configuration.ai?.customPrompt
	});
};
const gitOptionKeys = [
	"gitDiff",
	"gitDiffBase",
	"gitDiffCurrent",
	"uncommitted",
	"unpushed",
	"untracked"
];
const extractGitOptions = (options) => {
	const filteredOptions = extractKeysFromOptions(options, gitOptionKeys);
	if (!Object.values(filteredOptions).some(Boolean)) return void 0;
	const { gitDiff, gitDiffBase, gitDiffCurrent, uncommitted, unpushed, untracked } = options;
	return removeUndefined({
		mode: [
			gitDiff && "gitDiff",
			uncommitted && "uncommitted",
			unpushed && "unpushed",
			untracked && "untracked"
		].filter(Boolean),
		baseRef: gitDiffBase,
		currentRef: gitDiffCurrent,
		absolute: true
	});
};
const configurationOptionKeys = [
	"baseDir",
	"env",
	"envFile",
	"verbose",
	"prefix"
];
const extractConfigOptions = (options) => {
	const configuration = getConfiguration(options);
	const filteredOptions = extractKeysFromOptions(options, configurationOptionKeys);
	if (!Object.values(filteredOptions).some(Boolean)) return;
	const { baseDir, env, envFile, verbose, prefix, noCache } = options;
	const addPrefix = Boolean(options.with);
	return removeUndefined({
		baseDir,
		env,
		envFile,
		override: { log: {
			prefix: prefix ?? addPrefix ? configuration.log.prefix : "",
			verbose: verbose ?? true
		} },
		cache: !noCache
	});
};
/**
* Set the API for the CLI
*
* Example of commands:
*
* npm run intlayer build --watch
* npm run intlayer push --dictionaries id1 id2 id3 --deleteLocaleDir
*/
const setAPI = () => {
	const program = new Command();
	program.version(packageJson.version).description("Intlayer CLI");
	program.command("version").description("Print the Intlayer CLI version").action(() => {
		console.log(packageJson.version ?? "unknown");
	});
	/**
	* DICTIONARIES
	*/
	const dictionariesProgram = program.command("dictionary").alias("dictionaries").alias("dic").description("Dictionaries operations");
	const buildOptions = {
		description: "Build the dictionaries",
		options: [
			["-w, --watch", "Watch for changes"],
			["--skip-prepare", "Skip the prepare step"],
			["--with [with...]", "Start command in parallel with the build"]
		]
	};
	const dictionariesBuildCmd = dictionariesProgram.command("build").description(buildOptions.description);
	applyOptions(dictionariesBuildCmd, buildOptions.options);
	applyConfigOptions(dictionariesBuildCmd);
	dictionariesBuildCmd.action((options) => {
		build({
			...options,
			configOptions: extractConfigOptions(options)
		});
	});
	const rootBuildCmd = program.command("build").description(buildOptions.description);
	applyOptions(rootBuildCmd, buildOptions.options);
	applyConfigOptions(rootBuildCmd);
	rootBuildCmd.action((options) => {
		build({
			...options,
			configOptions: extractConfigOptions(options)
		});
	});
	const watchOptions = {
		description: "Watch the dictionaries changes",
		options: [["--with [with...]", "Start command in parallel with the build"]]
	};
	const dictionariesWatchCmd = dictionariesProgram.command("watch").description(buildOptions.description);
	applyOptions(dictionariesWatchCmd, watchOptions.options);
	applyConfigOptions(dictionariesWatchCmd);
	dictionariesWatchCmd.action((options) => {
		watchContentDeclaration({
			...options,
			configOptions: extractConfigOptions(options)
		});
	});
	const rootWatchCmd = program.command("watch").description(buildOptions.description);
	applyOptions(rootWatchCmd, watchOptions.options);
	applyConfigOptions(rootWatchCmd);
	rootWatchCmd.action((options) => {
		watchContentDeclaration({
			...options,
			configOptions: extractConfigOptions(options)
		});
	});
	const pullOptions = {
		description: "Pull dictionaries from the server",
		options: [
			["-d, --dictionaries [ids...]", "List of dictionary IDs to pull"],
			["--dictionary [ids...]", "List of dictionary IDs to pull (alias for --dictionaries)"],
			["--new-dictionaries-path [path]", "Path to save the new dictionaries"],
			["--newDictionariesPath [path]", "[alias] Path to save the new dictionaries"]
		]
	};
	const dictionariesPullCmd = dictionariesProgram.command("pull").description(pullOptions.description);
	applyOptions(dictionariesPullCmd, pullOptions.options);
	applyConfigOptions(dictionariesPullCmd);
	dictionariesPullCmd.action((options) => {
		const dictionaries = [...options.dictionaries ?? [], ...options.dictionary ?? []];
		pull({
			...options,
			dictionaries: dictionaries.length > 0 ? dictionaries : void 0,
			configOptions: {
				...options.configOptions,
				baseDir: options.baseDir
			}
		});
	});
	const rootPullCmd = program.command("pull").description(pullOptions.description);
	applyOptions(rootPullCmd, pullOptions.options);
	applyConfigOptions(rootPullCmd);
	rootPullCmd.action((options) => {
		const dictionaries = [...options.dictionaries ?? [], ...options.dictionary ?? []];
		pull({
			...options,
			dictionaries: dictionaries.length > 0 ? dictionaries : void 0,
			configOptions: extractConfigOptions(options)
		});
	});
	const pushOptions = {
		description: "Push all dictionaries. Create or update the pushed dictionaries",
		options: [
			["-d, --dictionaries [ids...]", "List of dictionary IDs to push"],
			["--dictionary [ids...]", "List of dictionary IDs to push (alias for --dictionaries)"],
			["-r, --delete-locale-dictionary", "Delete the local dictionaries after pushing"],
			["-k, --keep-locale-dictionary", "Keep the local dictionaries after pushing"],
			["--deleteLocaleDictionary", "[alias] Delete the local dictionaries after pushing"],
			["--keepLocaleDictionary", "[alias] Keep the local dictionaries after pushing"],
			["--build [build]", "Build the dictionaries before pushing to ensure the content is up to date. True will force the build, false will skip the build, undefined will allow using the cache of the build"]
		]
	};
	const dictionariesPushCmd = dictionariesProgram.command("push").description(pushOptions.description);
	applyOptions(dictionariesPushCmd, pushOptions.options);
	applyConfigOptions(dictionariesPushCmd);
	applyGitOptions(dictionariesPushCmd);
	dictionariesPushCmd.action((options) => {
		const dictionaries = [...options.dictionaries || [], ...options.dictionary || []];
		return push({
			...options,
			dictionaries: dictionaries.length > 0 ? dictionaries : void 0,
			gitOptions: extractGitOptions(options),
			configOptions: extractConfigOptions(options)
		});
	});
	const rootPushCmd = program.command("push").description(pushOptions.description);
	applyOptions(rootPushCmd, pushOptions.options);
	applyConfigOptions(rootPushCmd);
	applyGitOptions(rootPushCmd);
	rootPushCmd.action((options) => {
		const dictionaries = [...options.dictionaries || [], ...options.dictionary || []];
		return push({
			...options,
			dictionaries: dictionaries.length > 0 ? dictionaries : void 0,
			gitOptions: extractGitOptions(options),
			configOptions: extractConfigOptions(options)
		});
	});
	/**
	* CONFIGURATION
	*/
	const configurationProgram = program.command("configuration").alias("config").alias("conf").description("Configuration operations");
	const configGetCmd = configurationProgram.command("get").description("Get the configuration");
	applyConfigOptions(configGetCmd);
	configGetCmd.action((options) => {
		getConfig({
			...options,
			configOptions: extractConfigOptions(options)
		});
	});
	const configPushCmd = configurationProgram.command("push").description("Push the configuration");
	applyConfigOptions(configPushCmd);
	configPushCmd.action((options) => {
		pushConfig({
			...options,
			configOptions: extractConfigOptions(options)
		});
	});
	/**
	* CONTENT DECLARATION
	*/
	const contentProgram = program.command("content").description("Content declaration operations");
	contentProgram.command("list").description("List the content declaration files").action(listContentDeclaration);
	program.command("list").description("List the content declaration files").action(listContentDeclaration);
	const testProgram = contentProgram.command("test").description("Test if there are missing translations").option("--build [build]", "Build the dictionaries before testing to ensure the content is up to date. True will force the build, false will skip the build, undefined will allow using the cache of the build");
	applyConfigOptions(testProgram);
	testProgram.action((options) => {
		testMissingTranslations({
			...options,
			configOptions: extractConfigOptions(options)
		});
	});
	const rootTestCmd = program.command("test").description("Test if there are missing translations").option("--build [build]", "Build the dictionaries before testing to ensure the content is up to date. True will force the build, false will skip the build, undefined will allow using the cache of the build");
	applyConfigOptions(rootTestCmd);
	rootTestCmd.action((options) => {
		testMissingTranslations({
			...options,
			configOptions: extractConfigOptions(options)
		});
	});
	const fillProgram = program.command("fill").description("Fill the dictionaries").option("-f, --file [files...]", "List of Dictionary files to fill").option("--source-locale [sourceLocale]", "Source locale to translate from").option("--output-locales [outputLocales...]", "Target locales to translate to").option("--mode [mode]", "Fill mode: complete, review. Complete will fill all missing content, review will fill missing content and review existing keys", "complete").option("-k, --keys [keys...]", "Filter dictionaries based on keys").option("--key [keys...]", "Filter dictionaries based on keys (alias for --keys)").option("--excluded-keys [excludedKeys...]", "Filter out dictionaries based on keys").option("--excluded-key [excludedKeys...]", "Filter out dictionaries based on keys (alias for --excluded-keys)").option("--path-filter [pathFilters...]", "Filter dictionaries based on glob pattern").option("--build [build]", "Build the dictionaries before filling to ensure the content is up to date. True will force the build, false will skip the build, undefined will allow using the cache of the build").option("--skip-metadata", "Skip filling missing metadata (description, title, tags) for dictionaries");
	applyConfigOptions(fillProgram);
	applyAIOptions(fillProgram);
	applyGitOptions(fillProgram);
	fillProgram.action((options) => {
		const keys = [...options.keys ?? [], ...options.key ?? []];
		const excludedKeys = [...options.excludedKeys ?? [], ...options.excludedKey ?? []];
		const dictionaries = [...options.dictionaries ?? [], ...options.dictionary ?? []];
		return fill({
			...options,
			keys: keys.length > 0 ? keys : void 0,
			excludedKeys: excludedKeys.length > 0 ? excludedKeys : void 0,
			dictionaries: dictionaries.length > 0 ? dictionaries : void 0,
			aiOptions: extractAiOptions(options),
			gitOptions: extractGitOptions(options),
			configOptions: extractConfigOptions(options)
		});
	});
	/**
	* DOCS
	*/
	const docParams = [
		["--doc-pattern [docPattern...]", "Documentation pattern"],
		["--excluded-glob-pattern [excludedGlobPattern...]", "Excluded glob pattern"],
		["--nb-simultaneous-file-processed [nbSimultaneousFileProcessed]", "Number of simultaneous file processed"],
		["--locales [locales...]", "Locales"],
		["--base-locale [baseLocale]", "Base locale"],
		["--custom-instructions [customInstructions]", "Custom instructions added to the prompt. Usefull to apply specific rules regarding formatting, urls translation, etc."],
		["--skip-if-modified-before [skipIfModifiedBefore]", "Skip the file if it has been modified before the given time. Can be an absolute time as \"2025-12-05\" (string or Date) or a relative time in ms `1 * 60 * 60 * 1000` (1 hour). This option check update time of the file using the `fs.stat` method. So it could be impacted by Git or other tools that modify the file."],
		["--skip-if-modified-after [skipIfModifiedAfter]", "Skip the file if it has been modified within the given time. Can be an absolute time as \"2025-12-05\" (string or Date) or a relative time in ms `1 * 60 * 60 * 1000` (1 hour). This option check update time of the file using the `fs.stat` method. So it could be impacted by Git or other tools that modify the file."],
		["--skip-if-exists", "Skip the file if it already exists"]
	];
	const docProgram = program.command("doc").description("Documentation operations");
	const translateProgram = docProgram.command("translate").description("Translate the documentation");
	applyConfigOptions(translateProgram);
	applyAIOptions(translateProgram);
	applyGitOptions(translateProgram);
	applyOptions(translateProgram, docParams);
	translateProgram.action((options) => translateDoc({
		docPattern: options.docPattern,
		excludedGlobPattern: options.excludedGlobPattern,
		locales: options.locales,
		baseLocale: options.baseLocale,
		aiOptions: extractAiOptions(options),
		gitOptions: extractGitOptions(options),
		nbSimultaneousFileProcessed: options.nbSimultaneousFileProcessed,
		configOptions: extractConfigOptions(options),
		customInstructions: options.customInstructions,
		skipIfModifiedBefore: options.skipIfModifiedBefore,
		skipIfModifiedAfter: options.skipIfModifiedAfter,
		skipIfExists: options.skipIfExists
	}));
	const reviewProgram = docProgram.command("review").description("Review the documentation");
	applyConfigOptions(reviewProgram);
	applyAIOptions(reviewProgram);
	applyGitOptions(reviewProgram);
	applyOptions(reviewProgram, docParams);
	reviewProgram.action((options) => reviewDoc({
		docPattern: options.docPattern,
		excludedGlobPattern: options.excludedGlobPattern,
		locales: options.locales,
		baseLocale: options.baseLocale,
		aiOptions: extractAiOptions(options),
		gitOptions: extractGitOptions(options),
		nbSimultaneousFileProcessed: options.nbSimultaneousFileProcessed,
		configOptions: extractConfigOptions(options),
		customInstructions: options.customInstructions,
		skipIfModifiedBefore: options.skipIfModifiedBefore,
		skipIfModifiedAfter: options.skipIfModifiedAfter,
		skipIfExists: options.skipIfExists
	}));
	/**
	* LIVE SYNC
	*/
	const liveOptions = [["--with [with...]", "Start command in parallel with the live sync"]];
	const liveCmd = program.command("live").description("Live sync - Watch for changes made on the CMS and update the application content accordingly");
	applyOptions(liveCmd, liveOptions);
	applyConfigOptions(liveCmd);
	liveCmd.action((options) => liveSync(options));
	const editorStartCmd = program.command("editor").description("Visual editor operations").command("start").description("Start the Intlayer visual editor");
	applyConfigOptions(editorStartCmd);
	editorStartCmd.action((options) => {
		startEditor({
			env: options.env,
			envFile: options.envFile
		});
	});
	program.parse(process.argv);
	return program;
};

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