"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.FRAMEWORK_PACKAGES = void 0;
exports.detectJsPackageManager = detectJsPackageManager;
exports.detectBundler = detectBundler;
exports.hasIndexHtml = hasIndexHtml;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const zod_1 = require("zod");
const package_manager_detection_light_1 = require("../../lib/package-manager-detection-light");
const INDEX_HTML_MAX_DEPTH = 6;
const INDEX_HTML_IGNORE_DIRS = new Set([
    // Dependencies & VCS
    'node_modules',
    '.git',
    // Build outputs
    '.next',
    '.output',
    'dist',
    'build',
    'out',
    // Caches
    '.turbo',
    '.cache',
    'coverage',
    // Test directories
    'e2e-tests',
    'e2e',
    'test-applications',
    'test-apps',
    '__tests__',
    'tests',
    'test',
    'fixtures',
    '__fixtures__',
    // Examples
    'examples',
    'example',
    // Storybook
    'storybook-static',
    '.storybook',
]);
/**
 * Packages that indicate a specific framework integration exists.
 * If any of these are in package.json, we should NOT match as generic JavaScript.
 *
 * When adding a new JS framework integration to the wizard,
 * add its detection package here too.
 */
exports.FRAMEWORK_PACKAGES = [
    'next',
    'nuxt',
    'vue',
    'react-router',
    '@tanstack/react-start',
    '@tanstack/react-router',
    'react-native',
    '@angular/core',
    'astro',
    '@sveltejs/kit',
];
/**
 * Detect the JS package manager for the project by checking lockfiles.
 *
 * Uses the lightweight detector to keep this file out of the cold-start
 * import graph for `setup-utils` / analytics — the framework `detect()`
 * path imports this module transitively.
 */
async function detectJsPackageManager(options) {
    const result = await (0, package_manager_detection_light_1.detectNodePackageManagersLight)(options.installDir);
    return result.primary?.label ?? 'unknown';
}
/**
 * Detect the bundler used in the project by checking package.json dependencies.
 */
function detectBundler(options) {
    try {
        const content = fs.readFileSync(path.join(options.installDir, 'package.json'), 'utf-8');
        const pkg = zod_1.z
            .object({
            dependencies: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
            devDependencies: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
            optionalDependencies: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
        })
            .passthrough()
            .parse(JSON.parse(content));
        const allDeps = {
            ...pkg.optionalDependencies,
            ...pkg.devDependencies,
            ...pkg.dependencies,
        };
        if (allDeps['vite'])
            return 'vite';
        if (allDeps['webpack'])
            return 'webpack';
        if (allDeps['esbuild'])
            return 'esbuild';
        if (allDeps['parcel'])
            return 'parcel';
        if (allDeps['rollup'])
            return 'rollup';
        return undefined;
    }
    catch {
        return undefined;
    }
}
/**
 * Heuristic: check if there is an index.html anywhere in the project,
 * ignoring common build and dependency directories.
 */
function hasIndexHtml(options) {
    const root = options.installDir;
    function search(dir, depth) {
        if (depth > INDEX_HTML_MAX_DEPTH) {
            return false;
        }
        const base = path.basename(dir);
        if (INDEX_HTML_IGNORE_DIRS.has(base)) {
            return false;
        }
        let entries;
        try {
            entries = fs.readdirSync(dir, { withFileTypes: true });
        }
        catch {
            return false;
        }
        for (const entry of entries) {
            if (entry.isFile() && entry.name.toLowerCase() === 'index.html') {
                return true;
            }
        }
        for (const entry of entries) {
            if (entry.isDirectory()) {
                if (search(path.join(dir, entry.name), depth + 1)) {
                    return true;
                }
            }
        }
        return false;
    }
    return search(root, 0);
}
