// @bun
// src/utils/package-manager.ts
import { execSync } from "child_process";
import { existsSync } from "fs";
import { join } from "path";
function detectPackageManagers() {
  const managers = [
    {
      name: "Bun",
      command: "bun",
      installCommand: "bun install"
    },
    {
      name: "pnpm",
      command: "pnpm",
      installCommand: "pnpm install"
    },
    {
      name: "Yarn",
      command: "yarn",
      installCommand: "yarn install"
    },
    {
      name: "npm",
      command: "npm",
      installCommand: "npm install"
    }
  ];
  return managers.map((manager) => ({
    ...manager,
    available: isCommandAvailable(manager.command)
  }));
}
function isCommandAvailable(command) {
  try {
    const checkCommand = process.platform === "win32" ? "where" : "which";
    execSync(`${checkCommand} ${command}`, { stdio: "ignore" });
    return true;
  } catch {
    return false;
  }
}
function getPreferredPackageManager(projectPath, availableManagers) {
  const lockFiles = {
    "bun.lockb": "bun",
    "pnpm-lock.yaml": "pnpm",
    "yarn.lock": "yarn",
    "package-lock.json": "npm"
  };
  for (const [lockFile, command] of Object.entries(lockFiles)) {
    const lockPath = join(projectPath, lockFile);
    if (existsSync(lockPath)) {
      const manager = availableManagers.find((m) => m.command === command);
      if (manager?.available) {
        return manager;
      }
    }
  }
  return availableManagers.find((m) => m.available) || null;
}

export { detectPackageManagers, getPreferredPackageManager };
