// Planu — Web Fetcher: Stack recommendation logic
const FRAMEWORK_PACKAGES = {
    'next.js': 'next',
    react: 'react',
    vue: 'vue',
    angular: '@angular/core',
    svelte: 'svelte',
    astro: 'astro',
    remix: '@remix-run/node',
    hono: 'hono',
    express: 'express',
    expo: 'expo',
    commander: 'commander',
    tsup: 'tsup',
    django: 'django',
    fastapi: 'fastapi',
    flask: 'flask',
    gin: 'gin',
    cobra: 'cobra',
    cargo: 'cargo',
    flutter: 'flutter',
};
export async function verifyFrameworkVersion(framework) {
    const { fetchLatestVersion } = await import('../package-registry.js');
    const packageName = FRAMEWORK_PACKAGES[framework.toLowerCase()];
    if (!packageName) {
        return null;
    }
    const result = await fetchLatestVersion(packageName, 'npm');
    return result?.version ?? null;
}
function getBaseRecommendations() {
    return {
        web: {
            language: 'typescript',
            framework: 'next.js',
            version: '14+',
            database: 'postgresql',
            orm: 'prisma',
            auth: 'next-auth',
            deployment: 'vercel',
            reasoning: 'Next.js offers the best DX for web apps with SSR/SSG, API routes, and a mature ecosystem.',
            ecosystem: ['react', 'tailwind', 'shadcn/ui', 'zod', 'tanstack-query'],
        },
        fullstack: {
            language: 'typescript',
            framework: 'next.js',
            version: '14+',
            database: 'postgresql',
            orm: 'drizzle',
            auth: 'next-auth',
            deployment: 'vercel',
            reasoning: 'Full-stack TypeScript with Next.js provides end-to-end type safety and excellent DX.',
            ecosystem: ['react', 'tailwind', 'trpc', 'zod', 'drizzle'],
        },
        backend: {
            language: 'typescript',
            framework: 'hono',
            version: '4+',
            database: 'postgresql',
            orm: 'drizzle',
            deployment: 'railway',
            reasoning: 'Hono is lightweight, fast, and works across runtimes (Node, Bun, Cloudflare Workers).',
            ecosystem: ['zod', 'drizzle', 'vitest'],
        },
        mobile: {
            language: 'typescript',
            framework: 'expo',
            version: '50+',
            database: 'supabase',
            auth: 'supabase-auth',
            deployment: 'eas-build',
            reasoning: 'Expo simplifies React Native development with OTA updates and managed workflow.',
            ecosystem: ['react-native', 'expo-router', 'tamagui', 'zustand'],
        },
        cli: {
            language: 'typescript',
            framework: 'commander',
            version: '12+',
            database: 'sqlite',
            deployment: 'npm',
            reasoning: 'TypeScript CLI tools with Commander provide type safety and excellent developer experience.',
            ecosystem: ['commander', 'inquirer', 'chalk', 'zod'],
        },
        library: {
            language: 'typescript',
            framework: 'tsup',
            version: '8+',
            database: 'none',
            deployment: 'npm',
            reasoning: 'tsup provides fast bundling with zero config for TypeScript libraries.',
            ecosystem: ['tsup', 'vitest', 'changesets'],
        },
    };
}
function applyRequirementOverrides(rec, reqStr, constStr) {
    let result = rec;
    if (reqStr.includes('python') || reqStr.includes('ml') || reqStr.includes('ai')) {
        result = {
            ...result,
            language: 'python',
            framework: reqStr.includes('api') ? 'fastapi' : 'django',
            version: reqStr.includes('api') ? '0.100+' : '5+',
            ecosystem: reqStr.includes('api')
                ? ['fastapi', 'pydantic', 'sqlalchemy', 'alembic']
                : ['django', 'django-rest-framework', 'celery', 'pytest'],
        };
    }
    if (reqStr.includes('go') || reqStr.includes('golang') || reqStr.includes('performance')) {
        result = {
            ...result,
            language: 'go',
            framework: 'gin',
            version: '1.22+',
            ecosystem: ['gin', 'gorm', 'wire', 'testify'],
        };
    }
    if (reqStr.includes('supabase')) {
        result = {
            ...result,
            database: 'supabase',
            orm: '@supabase/supabase-js',
            auth: 'supabase-auth',
        };
    }
    if (reqStr.includes('firebase')) {
        result = { ...result, database: 'firebase', auth: 'firebase-auth' };
    }
    if (constStr.includes('no-vendor-lock') || constStr.includes('self-host')) {
        result = { ...result, deployment: 'docker + railway', database: 'postgresql' };
    }
    return result;
}
export function recommendStack(projectType, requirements, constraints) {
    const reqStr = requirements.join(' ').toLowerCase();
    const constStr = constraints.join(' ').toLowerCase();
    const recommendations = getBaseRecommendations();
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- 'web' key is defined in getBaseRecommendations
    const defaultRec = recommendations.web;
    const base = recommendations[projectType] ?? defaultRec;
    return applyRequirementOverrides(base, reqStr, constStr);
}
export function recommendAlternativeStack(projectType, _requirements) {
    const alternatives = {
        web: {
            language: 'typescript',
            framework: 'astro',
            version: '4+',
            database: 'supabase',
            deployment: 'vercel',
            reasoning: 'Astro provides excellent performance with islands architecture for content-heavy sites.',
            ecosystem: ['astro', 'react', 'tailwind', 'supabase'],
        },
        fullstack: {
            language: 'typescript',
            framework: 'remix',
            version: '2+',
            database: 'postgresql',
            orm: 'prisma',
            deployment: 'fly.io',
            reasoning: 'Remix embraces web standards with progressive enhancement and nested routing.',
            ecosystem: ['react', 'tailwind', 'prisma', 'zod'],
        },
        backend: {
            language: 'go',
            framework: 'gin',
            version: '1.22+',
            database: 'postgresql',
            deployment: 'docker',
            reasoning: 'Go offers excellent performance and lower resource usage for high-throughput APIs.',
            ecosystem: ['gin', 'pgx', 'testify'],
        },
        mobile: {
            language: 'dart',
            framework: 'flutter',
            version: '3+',
            database: 'supabase',
            deployment: 'app-store',
            reasoning: 'Flutter provides true native performance with a single codebase and rich widget library.',
            ecosystem: ['flutter', 'riverpod', 'freezed', 'go_router'],
        },
        cli: {
            language: 'go',
            framework: 'cobra',
            version: '1.22+',
            database: 'sqlite',
            deployment: 'goreleaser',
            reasoning: 'Go CLI tools compile to a single binary with zero dependencies.',
            ecosystem: ['cobra', 'viper', 'bubbletea'],
        },
        library: {
            language: 'rust',
            framework: 'cargo',
            version: '2021+',
            database: 'none',
            deployment: 'crates.io',
            reasoning: 'Rust provides memory safety and excellent performance for system-level libraries.',
            ecosystem: ['cargo', 'serde', 'tokio'],
        },
    };
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- 'web' key is defined in the literal above
    const defaultAlt = alternatives.web;
    return alternatives[projectType] ?? defaultAlt;
}
export function buildWhyNotOthers(_projectType, chosenFramework) {
    const allAlternatives = [
        {
            name: 'Ruby on Rails',
            reason: 'Great for prototyping but TypeScript ecosystem is larger and has better type safety.',
        },
        {
            name: 'PHP/Laravel',
            reason: 'Solid framework but less modern tooling and TypeScript interop.',
        },
        {
            name: 'Java/Spring',
            reason: 'Enterprise-grade but heavier setup and verbose for smaller projects.',
        },
        {
            name: 'C#/.NET',
            reason: 'Excellent for enterprise but tighter coupling to Microsoft ecosystem.',
        },
        {
            name: 'Elixir/Phoenix',
            reason: 'Great for real-time but smaller ecosystem and talent pool.',
        },
    ];
    return allAlternatives
        .filter((a) => !a.name.toLowerCase().includes(chosenFramework.toLowerCase()))
        .slice(0, 3);
}
export function buildMcpCompatibility(stack) {
    const results = [];
    if (stack.database.includes('supabase')) {
        results.push({
            mcp: 'supabase',
            compatible: true,
            notes: 'Full MCP support for SQL, migrations, auth.',
        });
    }
    if (stack.database.includes('postgresql')) {
        results.push({ mcp: 'postgres', compatible: true, notes: 'Direct SQL access via MCP.' });
    }
    if (stack.deployment.includes('vercel')) {
        results.push({
            mcp: 'vercel',
            compatible: true,
            notes: 'Deploy previews and production via MCP.',
        });
    }
    if (stack.deployment.includes('railway')) {
        results.push({
            mcp: 'railway',
            compatible: true,
            notes: 'Full deployment management via MCP.',
        });
    }
    results.push({ mcp: 'github', compatible: true, notes: 'Issues, PRs, and code management.' });
    results.push({ mcp: 'puppeteer', compatible: true, notes: 'Browser automation for testing.' });
    return results;
}
export async function buildSuggestStackResult(projectType, requirements, constraints) {
    const primary = recommendStack(projectType, requirements, constraints);
    const alternative = recommendAlternativeStack(projectType, requirements);
    let confidence = 'low';
    try {
        const verifiedVersion = await verifyFrameworkVersion(primary.framework);
        if (verifiedVersion) {
            primary.version = verifiedVersion;
            confidence = 'high';
        }
        const altVersion = await verifyFrameworkVersion(alternative.framework);
        if (altVersion) {
            alternative.version = altVersion;
        }
    }
    catch {
        // Offline: keep static versions, mark confidence as low
        confidence = 'low';
    }
    return {
        primary,
        alternative,
        whyNotOthers: buildWhyNotOthers(projectType, primary.framework),
        mcpCompatibility: buildMcpCompatibility(primary),
        lastChecked: new Date().toISOString(),
        confidence,
    };
}
//# sourceMappingURL=stack-advisor.js.map