"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageManager = getPackageManager;
exports.getPackageRunner = getPackageRunner;
const { detect } = require("@antfu/ni");
function getPackageManager(targetDir_1) {
    return __awaiter(this, arguments, void 0, function* (targetDir, // cwd for the root of users project
    { withFallback } = {
        withFallback: false,
    }) {
        const packageManager = yield detect({ programmatic: true, cwd: targetDir });
        if (packageManager === "yarn@berry")
            return "yarn";
        if (packageManager === "pnpm@6")
            return "pnpm";
        if (packageManager === "bun")
            return "bun";
        if (packageManager === "deno")
            return "deno";
        if (!withFallback) {
            return packageManager !== null && packageManager !== void 0 ? packageManager : "npm";
        }
        // Fallback to user agent if not detected.
        const userAgent = process.env.npm_config_user_agent || "";
        if (userAgent.startsWith("yarn")) {
            return "yarn";
        }
        if (userAgent.startsWith("pnpm")) {
            return "pnpm";
        }
        if (userAgent.startsWith("bun")) {
            return "bun";
        }
        return "npm";
    });
}
function getPackageRunner(cwd) {
    return __awaiter(this, void 0, void 0, function* () {
        const packageManager = yield getPackageManager(cwd);
        if (packageManager === "pnpm")
            return "pnpm dlx";
        if (packageManager === "bun")
            return "bunx";
        return "npx";
    });
}
