"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectNodePackageManagersLight = detectNodePackageManagersLight;
/**
 * Lightweight Node.js package manager detection used by framework detect()
 * callbacks.
 *
 * The full `package-manager-detection.ts` module pulls in `traceStep` (→
 * `analytics` → `@amplitude/analytics-node`), Python detector helpers, and
 * static metadata for every supported ecosystem. None of that is needed
 * during framework detection — the cold path just needs to look for
 * lockfiles. This module has zero non-stdlib dependencies so frameworks can
 * import it without paying the full graph cost.
 *
 * Behavior must stay identical to `detectNodePackageManagers` in
 * `package-manager-detection.ts`. The full module re-exports
 * `detectNodePackageManagers` from here so non-detection callers
 * (diagnostics, agent runner) keep their analytics side effects.
 */
const fs = __importStar(require("node:fs"));
const node_path_1 = require("node:path");
const BUN = {
    name: 'bun',
    label: 'Bun',
    installCommand: 'bun add',
    runScriptCommand: 'bun run',
    detect: (installDir) => ['bun.lockb', 'bun.lock'].some((lockFile) => fs.existsSync((0, node_path_1.join)(installDir, lockFile))),
};
const YARN_V1 = {
    name: 'yarn',
    label: 'Yarn V1',
    installCommand: 'yarn add',
    runScriptCommand: 'yarn',
    detect: (installDir) => {
        try {
            return fs
                .readFileSync((0, node_path_1.join)(installDir, 'yarn.lock'), 'utf-8')
                .slice(0, 500)
                .includes('yarn lockfile v1');
        }
        catch {
            return false;
        }
    },
};
const YARN_V2 = {
    name: 'yarn',
    label: 'Yarn V2/3/4',
    installCommand: 'yarn add',
    runScriptCommand: 'yarn',
    detect: (installDir) => {
        try {
            return fs
                .readFileSync((0, node_path_1.join)(installDir, 'yarn.lock'), 'utf-8')
                .slice(0, 500)
                .includes('__metadata');
        }
        catch {
            return false;
        }
    },
};
const PNPM = {
    name: 'pnpm',
    label: 'pnpm',
    installCommand: 'pnpm add',
    runScriptCommand: 'pnpm',
    detect: (installDir) => fs.existsSync((0, node_path_1.join)(installDir, 'pnpm-lock.yaml')),
};
const NPM = {
    name: 'npm',
    label: 'npm',
    installCommand: 'npm add',
    runScriptCommand: 'npm run',
    detect: (installDir) => fs.existsSync((0, node_path_1.join)(installDir, 'package-lock.json')),
};
const EXPO = {
    name: 'expo',
    label: 'Expo',
    installCommand: 'npx expo install',
    runScriptCommand: 'npx expo run',
    detect: () => false,
};
// Order matters — must match `packageManagers` in `src/utils/package-manager.ts`.
const PACKAGE_MANAGERS = [
    BUN,
    YARN_V1,
    YARN_V2,
    PNPM,
    NPM,
    EXPO,
];
function serialize(pm) {
    return {
        name: pm.name,
        label: pm.label,
        installCommand: pm.installCommand,
        runCommand: pm.runScriptCommand,
    };
}
/**
 * Detect Node.js package managers via lockfiles, with no analytics or
 * telemetry side effects. Identical return shape to the heavyweight
 * `detectNodePackageManagers`.
 */
function detectNodePackageManagersLight(installDir) {
    const detected = [];
    for (const pm of PACKAGE_MANAGERS) {
        if (pm.detect(installDir)) {
            detected.push(serialize(pm));
        }
    }
    if (detected.length === 0) {
        return Promise.resolve({
            detected: [],
            primary: null,
            recommendation: 'No lockfile found. Default to npm (npm add, npm run).',
        });
    }
    const primary = detected[0];
    return Promise.resolve({
        detected,
        primary,
        recommendation: detected.length === 1
            ? `Use ${primary.label} (${primary.installCommand}).`
            : `Multiple package managers detected. Prefer ${primary.label} (${primary.installCommand}).`,
    });
}
