"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.detectFrameworkFromProject = detectFrameworkFromProject;
exports.detectFrameworkVersion = detectFrameworkVersion;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const RULES = [
    { kind: 'npm', framework: 'nextjs', pkg: 'next' },
    { kind: 'npm', framework: 'nuxt', pkg: 'nuxt' },
    { kind: 'npm', framework: 'nestjs', pkg: '@nestjs/core' },
    { kind: 'npm', framework: 'angular', pkg: '@angular/core' },
    { kind: 'npm', framework: 'sveltekit', pkg: '@sveltejs/kit' },
    { kind: 'npm', framework: 'astro', pkg: 'astro' },
    { kind: 'pyproject', framework: 'fastapi', pkg: 'fastapi' },
    { kind: 'pyproject', framework: 'django', pkg: 'django' },
    { kind: 'requirements', framework: 'fastapi', pkg: 'fastapi' },
    { kind: 'requirements', framework: 'django', pkg: 'django' },
    { kind: 'maven', framework: 'spring-boot' },
    { kind: 'gradle', framework: 'spring-boot' },
    { kind: 'csproj', framework: 'aspnet-core' },
];
/**
 * Try every rule in the registry against the project and return the first
 * match. Used when the caller does not know the framework upfront (e.g.
 * `flui deploy` running post-checks on an existing manifest).
 */
function detectFrameworkFromProject(cwd) {
    for (const rule of RULES) {
        const result = tryRule(rule, cwd);
        if (result)
            return result;
    }
    return undefined;
}
function detectFrameworkVersion(framework, cwd) {
    for (const rule of RULES.filter((r) => r.framework === framework)) {
        const result = tryRule(rule, cwd);
        if (result)
            return result;
    }
    return undefined;
}
function tryRule(rule, cwd) {
    switch (rule.kind) {
        case 'npm':
            return detectNpm(rule.framework, rule.pkg, cwd);
        case 'pyproject':
            return detectPyproject(rule.framework, rule.pkg, cwd);
        case 'requirements':
            return detectRequirements(rule.framework, rule.pkg, cwd);
        case 'maven':
            return detectMaven(rule.framework, cwd);
        case 'gradle':
            return detectGradle(rule.framework, cwd);
        case 'csproj':
            return detectCsproj(rule.framework, cwd);
    }
}
function readIfExists(file) {
    try {
        return fs.readFileSync(file, 'utf8');
    }
    catch {
        return undefined;
    }
}
function extractMajor(versionSpec) {
    const match = /(\d+)/.exec(versionSpec);
    return match ? match[1] : undefined;
}
function detectNpm(framework, pkg, cwd) {
    const file = path.join(cwd, 'package.json');
    const raw = readIfExists(file);
    if (!raw)
        return undefined;
    let parsed;
    try {
        parsed = JSON.parse(raw);
    }
    catch {
        return undefined;
    }
    const spec = parsed.dependencies?.[pkg] ?? parsed.devDependencies?.[pkg];
    if (!spec)
        return undefined;
    const major = extractMajor(spec);
    if (!major)
        return undefined;
    return { framework, major, source: `package.json (${pkg}@${spec})` };
}
function detectPyproject(framework, pkg, cwd) {
    const file = path.join(cwd, 'pyproject.toml');
    const raw = readIfExists(file);
    if (!raw)
        return undefined;
    const regex = new RegExp(String.raw `^\s*${escapeRegex(pkg)}\s*=\s*["']([^"']+)["']`, 'mi');
    const match = regex.exec(raw);
    if (!match)
        return undefined;
    const major = extractMajor(match[1]);
    if (!major)
        return undefined;
    return {
        framework,
        major,
        source: `pyproject.toml (${pkg}=${match[1]})`,
    };
}
function detectRequirements(framework, pkg, cwd) {
    const file = path.join(cwd, 'requirements.txt');
    const raw = readIfExists(file);
    if (!raw)
        return undefined;
    const regex = new RegExp(String.raw `^\s*${escapeRegex(pkg)}\s*[~=<>!]+\s*([\d.*]+)`, 'mi');
    const match = regex.exec(raw);
    if (!match)
        return undefined;
    const major = extractMajor(match[1]);
    if (!major)
        return undefined;
    return {
        framework,
        major,
        source: `requirements.txt (${pkg} ${match[1]})`,
    };
}
function detectMaven(framework, cwd) {
    const file = path.join(cwd, 'pom.xml');
    const raw = readIfExists(file);
    if (!raw)
        return undefined;
    const match = /spring-boot-starter-parent[\s\S]{0,500}?<version>([^<]+)<\/version>/i.exec(raw) ?? /<spring-boot\.version>([^<]+)<\/spring-boot\.version>/i.exec(raw);
    if (!match)
        return undefined;
    const major = extractMajor(match[1]);
    if (!major)
        return undefined;
    return {
        framework,
        major,
        source: `pom.xml (spring-boot ${match[1]})`,
    };
}
function detectGradle(framework, cwd) {
    const file = ['build.gradle.kts', 'build.gradle']
        .map((f) => path.join(cwd, f))
        .find((f) => fs.existsSync(f));
    if (!file)
        return undefined;
    const raw = readIfExists(file);
    if (!raw)
        return undefined;
    const match = /org\.springframework\.boot["']?\s*\)?\s*version\s*["']([^"']+)["']/i.exec(raw);
    if (!match)
        return undefined;
    const major = extractMajor(match[1]);
    if (!major)
        return undefined;
    return {
        framework,
        major,
        source: `${path.basename(file)} (spring-boot ${match[1]})`,
    };
}
function detectCsproj(framework, cwd) {
    const file = fs.readdirSync(cwd).find((f) => f.endsWith('.csproj'));
    if (!file)
        return undefined;
    const raw = readIfExists(path.join(cwd, file));
    if (!raw)
        return undefined;
    const match = /<TargetFramework>net(\d+)\.\d+<\/TargetFramework>/i.exec(raw);
    if (!match)
        return undefined;
    return {
        framework,
        major: match[1],
        source: `${file} (net${match[1]})`,
    };
}
function escapeRegex(s) {
    return s.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw `\$&`);
}
