import { BaseCommand, FsLoader, HelpCommand as HelpCommand$1, IndexGenerator as IndexGenerator$1, Kernel, ListCommand, ListLoader as ListLoader$1, Parser, args as args$1, cliHelpers, cliui, errors as errors$1, flags, tracingChannels } from "@adonisjs/ace";
//#region modules/ace/commands.ts
/**
* The base command class for creating custom Ace commands in AdonisJS applications.
* This class extends the base Ace command with AdonisJS-specific functionality like
* dependency injection and application lifecycle management.
*
* @example
* ```ts
* export default class MakeUser extends BaseCommand {
*   static commandName = 'make:user'
*   static description = 'Create a new user'
*
*   async run() {
*     this.logger.info('Creating user...')
*     // Command implementation
*   }
* }
* ```
*/
var BaseCommand$1 = class extends BaseCommand {
	app;
	kernel;
	static options = {};
	get staysAlive() {
		return this.constructor.options.staysAlive;
	}
	get startApp() {
		return this.constructor.options.startApp;
	}
	constructor(app, kernel, parsed, ui, prompt) {
		super(kernel, parsed, ui, prompt);
		this.app = app;
		this.kernel = kernel;
	}
	/**
	* Creates the codemods module to modify source files programmatically.
	* This method provides access to AST-based code transformations.
	*
	* @example
	* ```ts
	* const codemods = await this.createCodemods()
	* await codemods.makeUsingStub(stubsRoot, 'controller.stub', {
	*   filename: 'UserController',
	*   entity: { name: 'User' }
	* })
	* ```
	*/
	async createCodemods() {
		const { Codemods } = await import("./modules/ace/codemods.js");
		const codemods = new Codemods(this.app, this.logger);
		codemods.on("error", () => {
			this.exitCode = 1;
		});
		return codemods;
	}
	/**
	* Executes the lifecycle hooks and the run method from the command
	*/
	async exec() {
		this.hydrate();
		try {
			/**
			* Executing the template methods
			*/
			this.prepare && await this.app.container.call(this, "prepare");
			this.interact && await this.app.container.call(this, "interact");
			const result = await this.app.container.call(this, "run");
			/**
			* Set exit code
			*/
			this.result = this.result === void 0 ? result : this.result;
			this.exitCode = this.exitCode ?? 0;
		} catch (error) {
			this.error = error;
			this.exitCode = this.exitCode ?? 1;
		}
		/**
		* Run the completed method (if exists) and check if has handled
		* the error
		*/
		let errorHandled = this.completed ? await this.app.container.call(this, "completed") : false;
		if (this.error && !errorHandled) await this.kernel.errorHandler.render(this.error, this.kernel);
		return this.result;
	}
	/**
	* Terminate the application gracefully. This method should be preferred over
	* calling `app.terminate()` directly as it only triggers termination when
	* the current command is the main command responsible for the process.
	*
	* @example
	* ```ts
	* export default class SomeCommand extends BaseCommand {
	*   async run() {
	*     // Do some work
	*     await this.terminate()
	*   }
	* }
	* ```
	*/
	async terminate() {
		if (this.kernel.getMainCommand() === this) await this.app.terminate();
	}
};
/**
* The List command is used to display a list of available commands.
* This command extends the base Ace ListCommand with AdonisJS-specific functionality.
*/
var ListCommand$1 = class extends ListCommand {
	app;
	kernel;
	static options = {};
	get staysAlive() {
		return this.constructor.options.staysAlive;
	}
	get startApp() {
		return this.constructor.options.startApp;
	}
	constructor(app, kernel, parsed, ui, prompt) {
		super(kernel, parsed, ui, prompt);
		this.app = app;
		this.kernel = kernel;
	}
	/**
	* Auto-select JSON output when running inside an AI agent
	* and no explicit format flag is provided.
	*/
	async run() {
		if (!this.json && this.app.runningInAIAgent) this.json = true;
		return super.run();
	}
	/**
	* Creates the codemods module to modify source files programmatically.
	* This method provides access to AST-based code transformations.
	*/
	async createCodemods() {
		const { Codemods } = await import("./modules/ace/codemods.js");
		return new Codemods(this.app, this.logger);
	}
	/**
	* Terminate the app. A command should prefer calling this method
	* over the "app.terminate", because this method only triggers
	* app termination when the current command is in the charge
	* of the process.
	*/
	async terminate() {
		if (this.kernel.getMainCommand() === this) await this.app.terminate();
	}
};
//#endregion
//#region modules/ace/kernel.ts
/**
* The Ace command kernel for AdonisJS applications. This kernel extends the base
* Ace kernel with AdonisJS-specific functionality like dependency injection and
* application lifecycle management.
*
* @example
* ```ts
* const app = new Application(new URL('../', import.meta.url))
* const kernel = new Kernel(app)
*
* await kernel.handle(['make:controller', 'UserController'])
* ```
*/
var Kernel$1 = class extends Kernel {
	app;
	/**
	* Create a new Ace kernel instance
	*
	* @param app - The AdonisJS application instance
	*/
	constructor(app) {
		super(ListCommand$1, {
			create: async (command, parsedOutput, $kernel) => {
				return app.container.make(command, [
					app,
					$kernel,
					parsedOutput,
					$kernel.ui,
					$kernel.prompt
				]);
			},
			run: (command) => command.exec()
		});
		this.app = app;
	}
};
//#endregion
export { Parser as a, cliui as c, tracingChannels as d, Kernel$1 as f, ListLoader$1 as i, errors$1 as l, ListCommand$1 as m, HelpCommand$1 as n, args$1 as o, BaseCommand$1 as p, IndexGenerator$1 as r, cliHelpers as s, FsLoader as t, flags as u };
