import chalk from "chalk";
import { execa } from "execa";

//#region src/utils.ts
/**
* Detect which package manager was used to invoke the create script.
* Uses the npm_config_user_agent environment variable set by package managers.
*/
function getUserPkgManager() {
	const userAgent = process.env.npm_config_user_agent;
	if (userAgent) {
		if (userAgent.startsWith("yarn")) return "yarn";
		if (userAgent.startsWith("pnpm")) return "pnpm";
		if (userAgent.startsWith("bun")) return "bun";
	}
	return "npm";
}
/**
* Run the appropriate install command for the detected package manager.
* Shows full output to the user so they can see progress.
*/
async function runInstallCommand(pkgManager, projectDir) {
	await execa(pkgManager, ["install"], {
		cwd: projectDir,
		stdout: "inherit",
		stderr: "inherit"
	});
}
/**
* Install dependencies in the project directory.
*/
async function installDependencies(projectDir) {
	const pkgManager = getUserPkgManager();
	try {
		process.stderr.write(chalk.bold(`\nInstalling dependencies with ${pkgManager}...\n\n`));
		await runInstallCommand(pkgManager, projectDir);
		process.stderr.write(chalk.green("\n✓ Dependencies installed successfully!\n"));
		return true;
	} catch (error) {
		process.stderr.write(chalk.yellow("\n⚠ Dependency installation failed\n"));
		process.stderr.write(chalk.dim("You can install manually:\n"));
		process.stderr.write(chalk.cyan(`  cd ${projectDir.split("/").pop()}\n`));
		process.stderr.write(chalk.cyan(`  ${pkgManager} install\n\n`));
		return false;
	}
}
/**
* Install AI agent skills using the ai-agent-skills CLI.
*/
async function installAgentSkills(projectDir, agent) {
	try {
		process.stderr.write(chalk.bold(`\nInstalling AI agent skills for ${agent}...\n\n`));
		await execa("npx", [
			"ai-agent-skills",
			"install",
			"editframe/skills",
			...agent === "all" ? [] : ["--agent", agent]
		], {
			cwd: projectDir,
			stdout: "inherit",
			stderr: "inherit"
		});
		process.stderr.write(chalk.green(`\n✓ Agent skills installed for ${agent}!\n`));
		return true;
	} catch (error) {
		process.stderr.write(chalk.yellow("\n⚠ Failed to install agent skills\n"));
		process.stderr.write(chalk.dim("You can install manually:\n"));
		process.stderr.write(chalk.cyan(`  npx ai-agent-skills install editframe/skills --agent ${agent}\n\n`));
		return false;
	}
}
/**
* Get the appropriate start command for the package manager.
*/
function getStartCommand(pkgManager) {
	return pkgManager === "npm" ? "npm start" : `${pkgManager} start`;
}

//#endregion
export { getStartCommand, getUserPkgManager, installAgentSkills, installDependencies };
//# sourceMappingURL=utils.js.map