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

//#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";
}
/**
* Execute a package manager command with a spinner for progress indication.
*/
async function execWithSpinner(projectDir, pkgManager, options) {
	const { onDataHandle, args = ["install"], stdout = "pipe" } = options;
	const spinner = ora(`Running ${pkgManager} install...`).start();
	const subprocess = execa(pkgManager, args, {
		cwd: projectDir,
		stdout
	});
	await new Promise((res, rej) => {
		if (onDataHandle) subprocess.stdout?.on("data", onDataHandle(spinner));
		subprocess.on("error", (e) => rej(e));
		subprocess.on("close", () => res());
	});
	return spinner;
}
/**
* Run the appropriate install command for the detected package manager.
*/
async function runInstallCommand(pkgManager, projectDir) {
	switch (pkgManager) {
		case "npm":
			await execa(pkgManager, ["install"], {
				cwd: projectDir,
				stderr: "inherit"
			});
			return null;
		case "pnpm": return execWithSpinner(projectDir, pkgManager, { onDataHandle: (spinner) => (data) => {
			const text = data.toString();
			if (text.includes("Progress")) spinner.text = text.includes("|") ? text.split(" | ")[1] ?? "" : text;
		} });
		case "yarn": return execWithSpinner(projectDir, pkgManager, { onDataHandle: (spinner) => (data) => {
			spinner.text = data.toString().trim();
		} });
		case "bun": return execWithSpinner(projectDir, pkgManager, { stdout: "ignore" });
	}
}
/**
* Install dependencies in the project directory.
*/
async function installDependencies(projectDir) {
	const pkgManager = getUserPkgManager();
	try {
		(await runInstallCommand(pkgManager, projectDir) ?? ora()).succeed(chalk.green("Successfully installed dependencies!"));
		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) {
	const spinner = ora("Installing AI agent skills...").start();
	try {
		await execa("npx", [
			"ai-agent-skills",
			"install",
			"editframe/skills",
			...agent === "all" ? [] : ["--agent", agent]
		], { cwd: projectDir });
		spinner.succeed(chalk.green(`AI agent skills installed for ${agent}!`));
		return true;
	} catch (error) {
		spinner.fail(chalk.yellow("Failed to install agent skills"));
		process.stderr.write(chalk.dim("\nYou 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 dev command for the package manager.
*/
function getDevCommand(pkgManager) {
	return pkgManager === "npm" ? "npm run dev" : `${pkgManager} dev`;
}

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