/* eslint-disable no-console */
/**
 * Init command — scaffolds a new flow-weaver project interactively.
 * Supports persona-aware onboarding for nocode, low-code, vibe-coder, and expert users.
 */
import * as fs from 'fs';
import * as path from 'path';
import { execSync, spawn } from 'child_process';
import input from '@inquirer/input';
import select, { Separator } from '@inquirer/select';
import confirm from '@inquirer/confirm';
import { ExitPromptError } from '@inquirer/core';
import { getWorkflowTemplate, getAllWorkflowTemplates } from '../templates/index.js';
import { loadPackTemplates } from '../templates/pack-loader.js';
import { logger } from '../utils/logger.js';
import { compileCommand } from './compile.js';
import { runMcpSetupFromInit, CLI_TOOL_BINARY, detectCliTools } from './mcp-setup.js';
import { PERSONA_CHOICES, PERSONA_CONFIRMATIONS, USE_CASE_CHOICES, selectTemplateForPersona, getTemplateSubChoices, printNextSteps, generateReadme, generateExampleWorkflow, generateAgentPrompt, generateEditorPrompt, generateSetupPromptFile, printCopyablePrompt, AGENT_LAUNCH_DEFAULTS, } from './init-personas.js';
// ── Utilities ────────────────────────────────────────────────────────────────
const PROJECT_NAME_RE = /^[a-zA-Z0-9][-a-zA-Z0-9_.]*$/;
export function validateProjectName(name) {
    if (!name)
        return 'Project name cannot be empty';
    if (name.length > 214)
        return 'Project name must be at most 214 characters';
    if (!PROJECT_NAME_RE.test(name)) {
        return 'Project name must start with a letter or digit and contain only letters, digits, hyphens, dots, and underscores';
    }
    return true;
}
export function toWorkflowName(projectName) {
    const camel = projectName
        .replace(/[-_.]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ''))
        .replace(/^[^a-zA-Z_$]+/, '')
        .replace(/^./, (c) => c.toLowerCase());
    return (camel || 'myProject') + 'Workflow';
}
export function isNonInteractive() {
    return !process.stdin.isTTY;
}
// Dynamic: includes core templates plus any registered by extensions/packs
function getValidTemplates() {
    return getAllWorkflowTemplates().map((t) => t.id);
}
const VALID_PERSONAS = ['nocode', 'vibecoder', 'lowcode', 'expert'];
// Dynamic: includes core use cases plus any registered by extensions/packs
function getValidUseCases() {
    return USE_CASE_CHOICES.map((c) => c.value);
}
// ── Config resolution (prompts) ──────────────────────────────────────────────
export async function resolveInitConfig(dirArg, options) {
    const skipPrompts = options.yes || isNonInteractive();
    const force = options.force ?? false;
    const hasExplicitTemplate = !!options.template;
    // 1. Project name (unchanged)
    let projectName;
    if (options.name) {
        projectName = options.name;
    }
    else if (dirArg) {
        projectName = path.basename(dirArg);
    }
    else if (skipPrompts) {
        projectName = 'my-project';
    }
    else {
        projectName = await input({
            message: 'Project name:',
            default: 'my-project',
            validate: (v) => validateProjectName(v),
        });
    }
    const valid = validateProjectName(projectName);
    if (valid !== true) {
        throw new Error(valid);
    }
    const targetDir = path.resolve(dirArg ?? projectName);
    // 2. Persona
    let persona;
    if (options.preset) {
        if (!VALID_PERSONAS.includes(options.preset)) {
            throw new Error(`Unknown preset "${options.preset}". Available: ${VALID_PERSONAS.join(', ')}`);
        }
        persona = options.preset;
    }
    else if (skipPrompts || hasExplicitTemplate) {
        persona = 'expert';
    }
    else {
        persona = await select({
            message: 'How do you plan to build?',
            choices: PERSONA_CHOICES,
            default: 'vibecoder',
        });
    }
    // Print persona confirmation (interactive only)
    if (!skipPrompts) {
        const confirmation = PERSONA_CONFIRMATIONS[persona];
        if (confirmation) {
            logger.log(`  ${logger.dim(confirmation)}`);
            logger.newline();
        }
    }
    // 3. Template selection (persona-dependent)
    let template;
    let useCase;
    if (hasExplicitTemplate) {
        // Direct --template flag bypasses everything
        template = options.template;
        if (!getValidTemplates().includes(template)) {
            throw new Error(`Unknown template "${template}". Available: ${getValidTemplates().join(', ')}`);
        }
    }
    else if (persona === 'expert') {
        // Expert: show today's flat template list
        if (skipPrompts) {
            template = 'sequential';
        }
        else {
            template = await select({
                message: 'Workflow template:',
                choices: [
                    new Separator('── Data Processing ──'),
                    { value: 'sequential', name: 'sequential', description: 'Linear pipeline' },
                    { value: 'foreach', name: 'foreach', description: 'Batch iteration' },
                    { value: 'aggregator', name: 'aggregator', description: 'Collect and aggregate results' },
                    new Separator('── Automation ──'),
                    { value: 'conditional', name: 'conditional', description: 'Route by condition' },
                    new Separator('── AI ──'),
                    { value: 'ai-agent', name: 'ai-agent', description: 'LLM agent with tool calling' },
                    { value: 'ai-react', name: 'ai-react', description: 'ReAct pattern' },
                    { value: 'ai-rag', name: 'ai-rag', description: 'Retrieval-Augmented Generation' },
                    { value: 'ai-chat', name: 'ai-chat', description: 'Conversational AI' },
                    new Separator('── Integration ──'),
                    { value: 'webhook', name: 'webhook', description: 'HTTP webhook handler' },
                    new Separator('── Utility ──'),
                    { value: 'error-handler', name: 'error-handler', description: 'Error handling and recovery' },
                ],
                default: 'sequential',
            });
        }
    }
    else {
        // Non-expert: use-case categories
        if (options.useCase) {
            if (!getValidUseCases().includes(options.useCase)) {
                throw new Error(`Unknown use case "${options.useCase}". Available: ${getValidUseCases().join(', ')}`);
            }
            useCase = options.useCase;
        }
        else if (skipPrompts) {
            useCase = 'data';
        }
        else {
            useCase = await select({
                message: 'What are you building?',
                choices: USE_CASE_CHOICES,
                default: 'data',
            });
        }
        const selection = selectTemplateForPersona(persona, useCase);
        if (selection.choices && !skipPrompts) {
            // Lowcode with multiple choices: show sub-select
            template = await select({
                message: 'Pick a template:',
                choices: getTemplateSubChoices(selection.choices),
                default: selection.template,
            });
        }
        else {
            template = selection.template;
        }
    }
    // 3b. "Something else" follow-up: ask what they're building
    let useCaseDescription;
    if (useCase === 'minimal' && !skipPrompts && persona !== 'expert') {
        useCaseDescription = await input({
            message: 'Briefly describe what you want to build:',
        });
        if (useCaseDescription) {
            useCaseDescription = useCaseDescription.trim();
        }
        if (!useCaseDescription)
            useCaseDescription = undefined;
    }
    // 3c. Weaver AI assistant opt-in
    let installWeaver;
    if (options.withWeaver) {
        installWeaver = true;
    }
    else if (options.weaver === false) {
        installWeaver = false;
    }
    else if (skipPrompts) {
        installWeaver = true;
    }
    else {
        installWeaver = await confirm({
            message: 'Install Weaver AI assistant? (Recommended)\n  Weaver helps you create, modify, and manage workflows with AI.',
            default: true,
        });
    }
    // 4. MCP setup (nocode, vibecoder, lowcode: prompt; expert: skip unless --mcp)
    let mcp;
    if (options.mcp !== undefined) {
        mcp = options.mcp;
    }
    else if (skipPrompts) {
        mcp = false;
    }
    else if (persona === 'nocode' || persona === 'vibecoder' || persona === 'lowcode') {
        mcp = await confirm({
            message: 'Set up AI editor integration? (Claude Code, Cursor, VS Code, etc.)',
            default: true,
        });
    }
    else {
        mcp = false;
    }
    // 5. Install deps (expert: prompt, others: auto-yes)
    let installDeps;
    if (options.install !== undefined) {
        installDeps = options.install;
    }
    else if (skipPrompts || persona !== 'expert') {
        installDeps = true;
    }
    else {
        installDeps = await confirm({ message: 'Install dependencies (npm install)?', default: true });
    }
    // 6. Git init (expert: prompt, others: auto-yes)
    let gitInit;
    if (options.git !== undefined) {
        gitInit = options.git;
    }
    else if (skipPrompts || persona !== 'expert') {
        gitInit = true;
    }
    else {
        gitInit = await confirm({ message: 'Initialize a git repository?', default: true });
    }
    // 7. Module format (expert only)
    let format;
    if (options.format) {
        format = options.format;
        if (format !== 'esm' && format !== 'cjs') {
            throw new Error(`Invalid format "${format}". Use "esm" or "cjs".`);
        }
    }
    else if (skipPrompts || persona !== 'expert') {
        format = 'esm';
    }
    else {
        format = await select({
            message: 'Module format:',
            choices: [
                { value: 'esm', name: 'ESM (Recommended)', description: 'ECMAScript modules (import/export)' },
                { value: 'cjs', name: 'CommonJS', description: 'CommonJS modules (require/module.exports)' },
            ],
            default: 'esm',
        });
    }
    return {
        projectName,
        targetDir,
        template,
        format,
        install: installDeps,
        git: gitInit,
        force,
        persona,
        useCase,
        useCaseDescription,
        mcp,
        installWeaver,
    };
}
// ── Pure file generation ─────────────────────────────────────────────────────
export function generateProjectFiles(projectName, template, format = 'esm', persona = 'expert', installWeaver = false) {
    const workflowName = toWorkflowName(projectName);
    const workflowFile = `${projectName}-workflow.ts`;
    const tmpl = getWorkflowTemplate(template);
    if (!tmpl) {
        throw new Error(`Unknown template "${template}"`);
    }
    let workflowCode = tmpl.generate({ workflowName });
    // Add onboarding comment when weaver is installed
    if (installWeaver) {
        workflowCode = `// This is a Flow Weaver workflow. The JSDoc annotations define the DAG.\n// Don't worry about the syntax — use 'weaver assistant' to modify it.\n// Run: npx flow-weaver weaver assistant\n\n${workflowCode}`;
    }
    // Package.json
    const scripts = {
        dev: `npx fw compile src/${workflowFile} -o src && npx tsx src/main.ts`,
        start: 'npx tsx src/main.ts',
        compile: `npx fw compile src/${workflowFile} -o src`,
        validate: `npx fw validate src/${workflowFile}`,
        doctor: 'npx fw doctor',
    };
    // Add diagram script for non-expert personas
    if (persona !== 'expert') {
        scripts.diagram = `npx fw diagram src/${workflowFile} --format ascii-compact`;
    }
    const packageJsonContent = {
        name: projectName,
        version: '1.0.0',
        scripts,
        dependencies: {
            '@synergenius/flow-weaver': 'latest',
            ...(installWeaver ? { '@synergenius/flow-weaver-pack-weaver': 'latest' } : {}),
        },
        devDependencies: {
            typescript: '^5.3.0',
            '@types/node': '^20.11.0',
            tsx: '^4.21.0',
        },
    };
    if (format === 'esm') {
        packageJsonContent.type = 'module';
    }
    const packageJson = JSON.stringify(packageJsonContent, null, 2);
    // tsconfig.json
    const tsconfigContent = {
        compilerOptions: {
            target: 'ES2020',
            module: format === 'esm' ? 'ES2020' : 'CommonJS',
            moduleResolution: format === 'esm' ? 'bundler' : 'node',
            strict: true,
            esModuleInterop: true,
            skipLibCheck: true,
            outDir: 'dist',
            rootDir: 'src',
            types: ['node'],
        },
        include: ['src'],
    };
    const tsconfigJson = JSON.stringify(tsconfigContent, null, 2);
    // main.ts
    const workflowJsFile = workflowFile.replace(/\.ts$/, '.js');
    let mainTs;
    if (format === 'esm') {
        mainTs = [
            '/**',
            ` * ${projectName} — workflow runner`,
            ' *',
            ' * Usage:',
            ' *   npm run dev      compile workflow + run this file',
            ' *   npm start        run without recompiling',
            ' *   npm run compile  compile only',
            ' */',
            '',
            `import { ${workflowName} } from './${workflowJsFile}';`,
            '',
            'try {',
            `  const result = ${workflowName}(true, { data: { message: 'hello world' } });`,
            '  console.log(result);',
            '} catch (e) {',
            "  if (e instanceof Error && e.message.startsWith('Compile with:')) {",
            "    console.error('Workflow not compiled yet. Run: npm run dev');",
            '    process.exit(1);',
            '  }',
            '  throw e;',
            '}',
            '',
        ].join('\n');
    }
    else {
        mainTs = [
            '/**',
            ` * ${projectName} — workflow runner`,
            ' *',
            ' * Usage:',
            ' *   npm run dev      compile workflow + run this file',
            ' *   npm start        run without recompiling',
            ' *   npm run compile  compile only',
            ' */',
            '',
            `const { ${workflowName} } = require('./${workflowJsFile}');`,
            '',
            'try {',
            `  const result = ${workflowName}(true, { data: { message: 'hello world' } });`,
            '  console.log(result);',
            '} catch (e) {',
            "  if (e instanceof Error && e.message.startsWith('Compile with:')) {",
            "    console.error('Workflow not compiled yet. Run: npm run dev');",
            '    process.exit(1);',
            '  }',
            '  throw e;',
            '}',
            '',
        ].join('\n');
    }
    const gitignore = `node_modules/\ndist/\n.tsbuildinfo\n`;
    const configYaml = `defaultFileType: ts\n`;
    const files = {
        'package.json': packageJson,
        'tsconfig.json': tsconfigJson,
        [`src/${workflowFile}`]: workflowCode,
        'src/main.ts': mainTs,
        '.gitignore': gitignore,
        '.flowweaver/config.yaml': configYaml,
    };
    // Add README for all personas
    files['README.md'] = generateReadme(projectName, persona, template);
    // Add example workflow for lowcode persona
    if (persona === 'lowcode') {
        files['examples/example-workflow.ts'] = generateExampleWorkflow(projectName);
    }
    // Add Weaver config + getting started guide if opted in
    if (installWeaver) {
        files['.weaver.json'] = JSON.stringify({ provider: 'auto', approval: 'auto' }, null, 2) + '\n';
        files['GETTING_STARTED.md'] = generateGettingStarted(projectName, workflowFile);
    }
    return files;
}
// ── Filesystem writer ────────────────────────────────────────────────────────
export function scaffoldProject(targetDir, files, options) {
    const filesCreated = [];
    const filesSkipped = [];
    for (const [relativePath, content] of Object.entries(files)) {
        const absPath = path.join(targetDir, relativePath);
        const dir = path.dirname(absPath);
        fs.mkdirSync(dir, { recursive: true });
        if (fs.existsSync(absPath) && !options.force) {
            filesSkipped.push(relativePath);
            continue;
        }
        fs.writeFileSync(absPath, content, 'utf8');
        filesCreated.push(relativePath);
    }
    return { filesCreated, filesSkipped };
}
// ── Post-scaffold actions ────────────────────────────────────────────────────
export function runNpmInstall(targetDir) {
    try {
        execSync('npm install', { cwd: targetDir, stdio: 'pipe', timeout: 120_000 });
        return { success: true };
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        return { success: false, error: message };
    }
}
export function runGitInit(targetDir) {
    try {
        execSync('git init', { cwd: targetDir, stdio: 'pipe', timeout: 10_000 });
        return { success: true };
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        return { success: false, error: message };
    }
}
/**
 * After init + MCP setup, offer to launch a CLI agent or generate a prompt for GUI editors.
 * Returns true if a CLI agent was spawned (init should exit and let the agent take over).
 */
export async function handleAgentHandoff(opts) {
    const { projectName, persona, template, targetDir, cliTools, guiTools, filesCreated, useCaseDescription } = opts;
    // Step 1: If CLI agent available, offer to launch it
    if (cliTools.length > 0) {
        const toolId = cliTools[0]; // Prefer first (claude > codex)
        const binary = CLI_TOOL_BINARY[toolId];
        const displayName = toolId === 'claude' ? 'Claude Code' : 'Codex';
        const launchDefault = AGENT_LAUNCH_DEFAULTS[persona];
        const shouldLaunch = await confirm({
            message: `Launch ${displayName} to set up your project?`,
            default: launchDefault,
        });
        if (shouldLaunch && binary) {
            const prompt = generateAgentPrompt(projectName, persona, template, useCaseDescription);
            logger.newline();
            logger.log(`  ${logger.dim(`Starting ${displayName}...`)}`);
            logger.newline();
            const child = spawn(binary, [prompt], {
                cwd: targetDir,
                stdio: 'inherit',
                env: { ...process.env },
            });
            child.on('error', (err) => {
                logger.error(`Failed to start ${displayName}: ${err.message}`);
            });
            return true;
        }
    }
    // Step 2: If GUI editors configured (or user declined CLI), offer prompt options
    if (guiTools.length > 0 || cliTools.length > 0) {
        const promptAction = await select({
            message: 'Generate a setup prompt for your editor?',
            choices: [
                { value: 'terminal', name: 'Print to terminal', description: 'Copy and paste into your editor' },
                { value: 'file', name: 'Save as file', description: 'Write PROJECT_SETUP.md to your project' },
                { value: 'both', name: 'Both' },
                { value: 'skip', name: 'Skip' },
            ],
            default: 'terminal',
        });
        if (promptAction === 'skip')
            return false;
        const editorPrompt = generateEditorPrompt(projectName, persona, template, useCaseDescription);
        if (promptAction === 'terminal' || promptAction === 'both') {
            printCopyablePrompt(editorPrompt);
        }
        if (promptAction === 'file' || promptAction === 'both') {
            const setupContent = generateSetupPromptFile(projectName, persona, template, filesCreated, useCaseDescription);
            const setupPath = path.join(targetDir, 'PROJECT_SETUP.md');
            fs.writeFileSync(setupPath, setupContent, 'utf8');
            // Add to .gitignore
            const gitignorePath = path.join(targetDir, '.gitignore');
            if (fs.existsSync(gitignorePath)) {
                const existing = fs.readFileSync(gitignorePath, 'utf8');
                if (!existing.includes('PROJECT_SETUP.md')) {
                    fs.appendFileSync(gitignorePath, 'PROJECT_SETUP.md\n', 'utf8');
                }
            }
            logger.newline();
            logger.success(`Wrote ${logger.highlight('PROJECT_SETUP.md')} ${logger.dim('(delete after first setup)')}`);
        }
    }
    return false;
}
// ── CLI entrypoint ───────────────────────────────────────────────────────────
export async function initCommand(dirArg, options) {
    try {
        // Load templates contributed by installed marketplace packs
        await loadPackTemplates(process.cwd());
        const config = await resolveInitConfig(dirArg, options);
        // Validate target directory
        const pkgPath = path.join(config.targetDir, 'package.json');
        if (fs.existsSync(pkgPath) && !config.force) {
            throw new Error(`${config.targetDir} already contains a package.json. Use --force to overwrite.`);
        }
        // Generate and scaffold
        const files = generateProjectFiles(config.projectName, config.template, config.format, config.persona, config.installWeaver);
        const { filesCreated, filesSkipped } = scaffoldProject(config.targetDir, files, {
            force: config.force,
        });
        // Post-scaffold actions
        let installResult;
        if (config.install) {
            const spinner = !options.json ? logger.spinner('Installing dependencies...') : null;
            installResult = runNpmInstall(config.targetDir);
            if (spinner) {
                if (installResult.success)
                    spinner.stop('Dependencies installed');
                else
                    spinner.fail(`npm install failed: ${installResult.error}`);
            }
        }
        let gitResult;
        if (config.git) {
            gitResult = runGitInit(config.targetDir);
        }
        // Auto-compile the workflow so `npm start` works immediately
        const workflowFile = `${config.projectName}-workflow.ts`;
        const workflowPath = path.join(config.targetDir, 'src', workflowFile);
        let compileResult;
        if (!options.json && fs.existsSync(workflowPath)) {
            try {
                await compileCommand(workflowPath, { format: config.format });
                compileResult = { success: true };
            }
            catch (err) {
                const message = err instanceof Error ? err.message : String(err);
                compileResult = { success: false, error: message };
            }
        }
        // MCP setup
        let mcpConfigured;
        let mcpResult;
        if (config.mcp && !options.json) {
            const spinner = logger.spinner('Detecting AI editors...');
            try {
                mcpResult = await runMcpSetupFromInit();
                mcpConfigured = mcpResult.configured;
                spinner.stop();
                // Per-tool status lines
                for (const t of mcpResult.detected) {
                    if (!t.detected)
                        continue;
                    const wasConfigured = mcpConfigured.includes(t.displayName);
                    if (wasConfigured) {
                        logger.success(`${t.displayName} configured`);
                    }
                }
                if (mcpResult.failed.length > 0) {
                    for (const name of mcpResult.failed) {
                        logger.warn(`${name} failed to configure`);
                    }
                }
                if (mcpResult.detected.every((t) => !t.detected)) {
                    logger.log(`  ${logger.dim('No AI editors detected')}`);
                }
            }
            catch {
                spinner.fail('MCP setup failed');
            }
        }
        // Agent handoff
        let agentLaunched = false;
        let mcpCliTools = [];
        let mcpGuiTools = [];
        if (mcpResult) {
            mcpCliTools = mcpResult.cliTools;
            mcpGuiTools = mcpResult.guiTools;
        }
        // Build report
        const report = {
            projectDir: config.targetDir,
            filesCreated,
            filesSkipped,
            template: config.template,
            format: config.format,
            persona: config.persona,
            installResult,
            gitResult,
            mcpConfigured,
            agentLaunched: false,
        };
        if (options.json) {
            console.log(JSON.stringify(report, null, 2));
            return;
        }
        // Human output — status lines
        logger.newline();
        logger.success(`Created ${logger.highlight(config.projectName)} ${logger.dim(`(${config.template}, ${config.format.toUpperCase()})`)}`);
        if (filesSkipped.length > 0) {
            logger.warn(`Skipped ${filesSkipped.length} existing file(s)`);
        }
        if (installResult) {
            if (installResult.success) {
                logger.success('Dependencies installed');
            }
            else {
                logger.warn(`npm install failed: ${installResult.error}`);
            }
        }
        if (gitResult) {
            if (gitResult.success) {
                logger.success('Git initialized');
            }
            else {
                logger.warn(`git init failed: ${gitResult.error}`);
            }
        }
        if (compileResult) {
            if (compileResult.success) {
                logger.success('Workflow compiled');
            }
            else {
                logger.warn(`Compile failed: ${compileResult.error}`);
            }
        }
        if (config.installWeaver) {
            logger.success('Weaver AI assistant installed');
            logger.newline();
            logger.log('  Weaver installed. Try:');
            logger.log('    flow-weaver weaver assistant      # AI assistant');
            logger.log('    flow-weaver weaver bot "..."      # create workflows with AI');
            logger.log('    flow-weaver weaver examples       # see what\'s possible');
            logger.newline();
            logger.log('  VS Code extension:');
            logger.log('    Search "Flow Weaver" in Extensions (Ctrl+Shift+X)');
            logger.log('    IntelliSense + diagrams + Weaver commands in your editor');
            logger.newline();
        }
        // Read the workflow code for preview
        const workflowCode = files[`src/${workflowFile}`] ?? null;
        // Persona-specific rich output
        const relDir = path.relative(process.cwd(), config.targetDir);
        const displayDir = !relDir || relDir === '.' ? null : relDir.startsWith('../../') ? config.targetDir : relDir;
        // Agent handoff: offer to launch CLI agent or generate prompt for GUI editors.
        // Decoupled from MCP: even if MCP wasn't run, check for available CLI tools.
        const skipAgent = options.agent === false || options.yes || isNonInteractive();
        if (!skipAgent) {
            // If MCP didn't run or found no CLI tools, do a quick binary check
            if (mcpCliTools.length === 0) {
                try {
                    mcpCliTools = await detectCliTools();
                }
                catch {
                    // Non-fatal
                }
            }
            const hasTools = mcpCliTools.length > 0 || mcpGuiTools.length > 0;
            if (hasTools) {
                try {
                    agentLaunched = await handleAgentHandoff({
                        projectName: config.projectName,
                        persona: config.persona,
                        template: config.template,
                        targetDir: config.targetDir,
                        cliTools: mcpCliTools,
                        guiTools: mcpGuiTools,
                        filesCreated,
                        useCaseDescription: config.useCaseDescription,
                    });
                    report.agentLaunched = agentLaunched;
                }
                catch (err) {
                    if (err instanceof ExitPromptError)
                        return;
                    // Non-fatal: just skip agent handoff
                }
            }
        }
        // If an agent was spawned, it takes over. Print minimal output.
        if (agentLaunched) {
            return;
        }
        printNextSteps({
            projectName: config.projectName,
            persona: config.persona,
            template: config.template,
            displayDir,
            installSkipped: !config.install,
            workflowCode,
            workflowFile,
            mcpConfigured,
            agentLaunched,
            compiled: compileResult?.success,
        });
    }
    catch (err) {
        // Clean exit on Ctrl+C during prompts
        if (err instanceof ExitPromptError) {
            return;
        }
        throw err;
    }
}
// ── Getting Started guide (generated when Weaver is installed) ───────────────
function generateGettingStarted(projectName, workflowFile) {
    return `# Getting Started with ${projectName}

Your project has Flow Weaver + Weaver AI assistant installed.

## Quick Start

    npx flow-weaver weaver assistant     # Ask the AI anything about your project
    npx flow-weaver weaver examples      # See what you can do
    npx flow-weaver weaver doctor        # Check your setup

## Your First Workflow

Open \`src/${workflowFile}\` — that's your workflow file.

Don't worry about the annotations. Ask the assistant:

    npx flow-weaver weaver assistant
    > explain my workflow
    > add error handling to it
    > show me a diagram

The AI writes the annotations for you. You focus on what you want, not how to write it.

## Useful Commands

    npx flow-weaver validate src/        # check for errors
    npx flow-weaver compile src/         # compile to standalone
    npx flow-weaver diagram src/         # visual diagram
    npx fw deploy src/${workflowFile}    # deploy to cloud (requires fw login)

## Autonomous Mode

    npx flow-weaver weaver session --continuous    # process task queue
    npx flow-weaver weaver queue add "Fix all validation errors"

## Cloud Features

    npx fw login                         # connect to Flow Weaver platform
    npx fw deploy src/${workflowFile}    # deploy workflow
    npx fw cloud-status                  # see deployments + usage

## VS Code Extension

Install the Flow Weaver extension for the best editing experience:
- IntelliSense for annotations (completions, hover, go-to-definition)
- Real-time validation (errors show as you type)
- Interactive diagram panel
- Weaver commands in the command palette (Ctrl+Shift+P → "Flow Weaver")

Search "Flow Weaver" in the VS Code extensions marketplace.

## Learn More

    npx flow-weaver weaver examples      # concrete task examples
    npx flow-weaver docs concepts        # core concepts
    npx flow-weaver docs jsdoc-grammar   # annotation reference
`;
}
//# sourceMappingURL=init.js.map