/**
 * JIRA/ADO Auto-Detection for Init
 *
 * Analyzes external tool structure and recommends optimal organization.
 * Supports 2-level folder structure: specs/{provider-project}/{board-or-area}/
 *
 * @module cli/helpers/init/jira-ado-auto-detect
 */
import chalk from 'chalk';
import ora from 'ora';
import { select } from '@inquirer/prompts';
import { JiraClient } from '../../../integrations/jira/jira-client.js';
import { fetchBoardsForProject } from '../../../../plugins/specweave/lib/integrations/jira/jira-board-resolver.js';
import { fetchAreaPathsForProject } from '../../../../plugins/specweave/lib/integrations/ado/ado-board-resolver.js';
import { normalizeToProjectId } from '../../../utils/project-id-generator.js';
import { DEFAULT_ADO_HIERARCHY_MAPPING, ADO_SAFE_HIERARCHY_MAPPING, ADO_SCRUM_HIERARCHY_MAPPING, DEFAULT_JIRA_HIERARCHY_MAPPING, JIRA_SAFE_HIERARCHY_MAPPING, } from '../../../core/types/sync-profile.js';
// =============================================================================
// SMART ANALYSIS - Pattern Detection
// =============================================================================
/** Keywords for detecting team patterns */
const PATTERN_KEYWORDS = {
    'tech-stack': ['frontend', 'backend', 'mobile', 'ios', 'android', 'web', 'api', 'ui', 'ux', 'server', 'client', 'infra'],
    'squad-based': ['team', 'squad', 'pod', 'tribe', 'crew', 'group', 'alpha', 'beta', 'gamma', 'delta'],
    'domain-based': ['payment', 'user', 'order', 'catalog', 'checkout', 'shipping', 'inventory', 'customer', 'billing', 'auth'],
    'service-based': ['platform', 'infrastructure', 'devops', 'sre', 'core', 'shared', 'common', 'tools', 'ops'],
    'feature-based': ['feature', 'epic', 'initiative', 'project', 'release', 'sprint'],
    'mixed': [],
    'unknown': [],
};
/**
 * Analyze names to detect team organization pattern
 */
function detectTeamPattern(names) {
    const normalizedNames = names.map(n => n.toLowerCase());
    const matches = new Map();
    // Check each pattern
    for (const [pattern, keywords] of Object.entries(PATTERN_KEYWORDS)) {
        if (pattern === 'mixed' || pattern === 'unknown')
            continue;
        const matchedNames = [];
        for (const name of normalizedNames) {
            for (const keyword of keywords) {
                if (name.includes(keyword)) {
                    matchedNames.push(names[normalizedNames.indexOf(name)]);
                    break;
                }
            }
        }
        if (matchedNames.length > 0) {
            matches.set(pattern, matchedNames);
        }
    }
    // Determine primary pattern
    if (matches.size === 0) {
        return { pattern: 'unknown', confidence: 'low', matches };
    }
    if (matches.size > 2) {
        return { pattern: 'mixed', confidence: 'medium', matches };
    }
    // Find pattern with most matches
    let bestPattern = 'unknown';
    let bestCount = 0;
    for (const [pattern, matched] of matches) {
        if (matched.length > bestCount) {
            bestCount = matched.length;
            bestPattern = pattern;
        }
    }
    const coverage = bestCount / names.length;
    const confidence = coverage > 0.7 ? 'high' : coverage > 0.4 ? 'medium' : 'low';
    return { pattern: bestPattern, confidence, matches };
}
/**
 * Generate reasoning explanation for detected pattern
 */
function generateReasoning(pattern, matches, scrumCount, kanbanCount) {
    const parts = [];
    // Pattern explanation
    const patternDescriptions = {
        'tech-stack': 'Teams organized by technology layer (Frontend/Backend/Mobile)',
        'squad-based': 'Cross-functional squads or pods',
        'domain-based': 'Teams organized by business domain',
        'service-based': 'Platform/infrastructure focused teams',
        'feature-based': 'Teams organized around features or initiatives',
        'mixed': 'Multiple organization patterns detected',
        'unknown': 'No clear organizational pattern detected',
    };
    parts.push(patternDescriptions[pattern]);
    // Show matched keywords
    if (matches.size > 0) {
        const examples = [];
        for (const [, matched] of matches) {
            examples.push(...matched.slice(0, 2));
        }
        if (examples.length > 0) {
            parts.push(`Found: ${examples.slice(0, 4).join(', ')}`);
        }
    }
    // Board type insight
    if (scrumCount > 0 || kanbanCount > 0) {
        const boardInfo = [];
        if (scrumCount > 0)
            boardInfo.push(`${scrumCount} Scrum`);
        if (kanbanCount > 0)
            boardInfo.push(`${kanbanCount} Kanban`);
        parts.push(`Board types: ${boardInfo.join(', ')}`);
    }
    return parts.join('. ');
}
/**
 * Analyze JIRA structure and create detailed analysis
 */
function analyzeJiraStructureDeep(projects) {
    // Collect all board names
    const allBoardNames = [];
    let scrumCount = 0;
    let kanbanCount = 0;
    for (const project of projects) {
        for (const board of project.boards) {
            allBoardNames.push(board.name);
            if (board.type === 'scrum')
                scrumCount++;
            else if (board.type === 'kanban')
                kanbanCount++;
        }
    }
    // Detect pattern
    const { pattern, confidence, matches } = detectTeamPattern(allBoardNames);
    // Generate example mappings
    const suggestedMapping = [];
    for (const project of projects.slice(0, 2)) {
        for (const board of project.boards.slice(0, 2)) {
            const folder = `JIRA-${normalizeToProjectId(project.key)}/${normalizeToProjectId(board.name)}`;
            suggestedMapping.push(folder);
        }
    }
    return {
        pattern,
        confidence,
        reasoning: generateReasoning(pattern, matches, scrumCount, kanbanCount),
        scrumBoardCount: scrumCount,
        kanbanBoardCount: kanbanCount,
        suggestedMapping,
    };
}
/**
 * Analyze ADO structure and create detailed analysis
 */
function analyzeAdoStructureDeep(projects) {
    // Collect all area path names
    const allAreaNames = [];
    for (const project of projects) {
        for (const area of project.areaPaths) {
            allAreaNames.push(area.name);
        }
    }
    // Detect pattern
    const { pattern, confidence, matches } = detectTeamPattern(allAreaNames);
    // Generate example mappings
    const suggestedMapping = [];
    for (const project of projects.slice(0, 2)) {
        for (const area of project.areaPaths.slice(0, 2)) {
            const folder = `${normalizeToProjectId(project.name)}/${normalizeToProjectId(area.name)}`;
            suggestedMapping.push(folder);
        }
    }
    return {
        pattern,
        confidence,
        reasoning: generateReasoning(pattern, matches, 0, 0),
        scrumBoardCount: 0,
        kanbanBoardCount: 0,
        suggestedMapping,
    };
}
// =============================================================================
// HIERARCHY MAPPING SELECTION (v0.33.0+ - Universal 3/4/5 Level Support)
// =============================================================================
/**
 * Hierarchy preset definitions for JIRA
 *
 * Maps JIRA work item types to SpecWeave hierarchy levels.
 */
export const JIRA_HIERARCHY_PRESETS = {
    'standard': {
        name: 'Standard (3 levels: Epic → Story → Sub-task)',
        description: 'Most JIRA projects use this structure',
        config: DEFAULT_JIRA_HIERARCHY_MAPPING,
    },
    'safe': {
        name: 'SAFe (4 levels: Initiative → Epic → Story → Sub-task)',
        description: 'Scaled Agile Framework with strategic Initiatives',
        config: JIRA_SAFE_HIERARCHY_MAPPING,
    },
    'custom': {
        name: 'Custom (configure manually)',
        description: 'Define your own work item type mappings',
        config: null,
    },
};
/**
 * Hierarchy preset definitions for ADO
 *
 * Maps ADO work item types to SpecWeave hierarchy levels.
 */
export const ADO_HIERARCHY_PRESETS = {
    'agile': {
        name: 'Agile (4 levels: Epic → Feature → User Story → Task)',
        description: 'Default ADO Agile process template',
        config: DEFAULT_ADO_HIERARCHY_MAPPING,
    },
    'scrum': {
        name: 'Scrum (3 levels: Epic → Product Backlog Item → Task)',
        description: 'ADO Scrum process template',
        config: ADO_SCRUM_HIERARCHY_MAPPING,
    },
    'safe': {
        name: 'SAFe (5 levels: Capability → Epic → Feature → User Story → Task)',
        description: 'Scaled Agile Framework with Capabilities',
        config: ADO_SAFE_HIERARCHY_MAPPING,
    },
    'custom': {
        name: 'Custom (configure manually)',
        description: 'Define your own work item type mappings',
        config: null,
    },
};
/**
 * Prompt user to select JIRA hierarchy mapping
 *
 * Explains the 3/4/5 level hierarchy options and lets user choose
 * how their JIRA work items map to SpecWeave structure.
 */
export async function selectJiraHierarchyMapping() {
    console.log(chalk.cyan('\n📊 Work Item Hierarchy Mapping\n'));
    console.log(chalk.gray('   SpecWeave maps your JIRA work items to a universal structure:'));
    console.log(chalk.gray('   • Epic level (optional) → _epics/EP-XXX/'));
    console.log(chalk.gray('   • Feature level → FS-XXX/ folders'));
    console.log(chalk.gray('   • User Story level → us-XXX.md files'));
    console.log(chalk.gray('   • Task level → checkboxes in User Stories'));
    console.log('');
    const preset = await select({
        message: 'What hierarchy does your JIRA project use?',
        choices: [
            {
                name: `● ${JIRA_HIERARCHY_PRESETS.standard.name}`,
                value: 'standard',
                description: JIRA_HIERARCHY_PRESETS.standard.description,
            },
            {
                name: `○ ${JIRA_HIERARCHY_PRESETS.safe.name}`,
                value: 'safe',
                description: JIRA_HIERARCHY_PRESETS.safe.description,
            },
            {
                name: `○ ${JIRA_HIERARCHY_PRESETS.custom.name}`,
                value: 'custom',
                description: JIRA_HIERARCHY_PRESETS.custom.description,
            },
        ],
        default: 'standard',
    });
    if (preset === 'custom') {
        return await configureCustomJiraHierarchy();
    }
    const selectedConfig = JIRA_HIERARCHY_PRESETS[preset].config;
    // Show mapping preview
    console.log(chalk.cyan('\n📁 Hierarchy Mapping Preview:\n'));
    if (selectedConfig.epicLevelTypes.length > 0) {
        console.log(chalk.gray(`   ${selectedConfig.epicLevelTypes.join(', ')} → _epics/EP-XXX/ (strategic)`));
    }
    console.log(chalk.gray(`   ${selectedConfig.featureLevelTypes.join(', ')} → FS-XXX/ (features)`));
    console.log(chalk.gray(`   Story, Bug, Task → us-XXX.md (user stories)`));
    console.log(chalk.gray(`   Sub-task → checkboxes in parent story`));
    console.log('');
    return selectedConfig;
}
/**
 * Prompt user to select ADO hierarchy mapping
 *
 * Explains the 3/4/5 level hierarchy options and lets user choose
 * how their ADO work items map to SpecWeave structure.
 */
export async function selectAdoHierarchyMapping() {
    console.log(chalk.cyan('\n📊 Work Item Hierarchy Mapping\n'));
    console.log(chalk.gray('   SpecWeave maps your ADO work items to a universal structure:'));
    console.log(chalk.gray('   • Epic level (optional) → _epics/EP-XXX/'));
    console.log(chalk.gray('   • Feature level → FS-XXX/ folders'));
    console.log(chalk.gray('   • User Story level → us-XXX.md files'));
    console.log(chalk.gray('   • Task level → checkboxes in User Stories'));
    console.log('');
    const preset = await select({
        message: 'What process template does your ADO project use?',
        choices: [
            {
                name: `● ${ADO_HIERARCHY_PRESETS.agile.name}`,
                value: 'agile',
                description: ADO_HIERARCHY_PRESETS.agile.description,
            },
            {
                name: `○ ${ADO_HIERARCHY_PRESETS.scrum.name}`,
                value: 'scrum',
                description: ADO_HIERARCHY_PRESETS.scrum.description,
            },
            {
                name: `○ ${ADO_HIERARCHY_PRESETS.safe.name}`,
                value: 'safe',
                description: ADO_HIERARCHY_PRESETS.safe.description,
            },
            {
                name: `○ ${ADO_HIERARCHY_PRESETS.custom.name}`,
                value: 'custom',
                description: ADO_HIERARCHY_PRESETS.custom.description,
            },
        ],
        default: 'agile',
    });
    if (preset === 'custom') {
        return await configureCustomAdoHierarchy();
    }
    const selectedConfig = ADO_HIERARCHY_PRESETS[preset].config;
    // Show mapping preview
    console.log(chalk.cyan('\n📁 Hierarchy Mapping Preview:\n'));
    if (selectedConfig.epicLevelTypes.length > 0) {
        console.log(chalk.gray(`   ${selectedConfig.epicLevelTypes.join(', ')} → _epics/EP-XXX/ (strategic)`));
    }
    console.log(chalk.gray(`   ${selectedConfig.featureLevelTypes.join(', ')} → FS-XXX/ (features)`));
    console.log(chalk.gray(`   User Story, Bug, PBI → us-XXX.md (user stories)`));
    console.log(chalk.gray(`   Task → checkboxes in parent story`));
    console.log('');
    return selectedConfig;
}
/**
 * Configure custom JIRA hierarchy mapping
 */
async function configureCustomJiraHierarchy() {
    console.log(chalk.cyan('\n🔧 Custom Hierarchy Configuration\n'));
    console.log(chalk.gray('   Enter comma-separated work item types for each level.'));
    console.log(chalk.gray('   Leave empty to skip a level.\n'));
    const { input } = await import('@inquirer/prompts');
    const epicTypes = await input({
        message: 'Epic-level types (strategic containers, optional):',
        default: '',
    });
    const featureTypes = await input({
        message: 'Feature-level types (required):',
        default: 'Epic',
    });
    if (!featureTypes.trim()) {
        console.log(chalk.yellow('   ⚠️  Feature-level types are required. Using "Epic" as default.'));
    }
    return {
        epicLevelTypes: epicTypes.split(',').map(t => t.trim()).filter(Boolean),
        featureLevelTypes: featureTypes.split(',').map(t => t.trim()).filter(Boolean) || ['Epic'],
    };
}
/**
 * Configure custom ADO hierarchy mapping
 */
async function configureCustomAdoHierarchy() {
    console.log(chalk.cyan('\n🔧 Custom Hierarchy Configuration\n'));
    console.log(chalk.gray('   Enter comma-separated work item types for each level.'));
    console.log(chalk.gray('   Leave empty to skip a level.\n'));
    const { input } = await import('@inquirer/prompts');
    const epicTypes = await input({
        message: 'Epic-level types (strategic containers, optional):',
        default: '',
    });
    const featureTypes = await input({
        message: 'Feature-level types (required):',
        default: 'Epic, Feature',
    });
    if (!featureTypes.trim()) {
        console.log(chalk.yellow('   ⚠️  Feature-level types are required. Using "Epic, Feature" as default.'));
    }
    return {
        epicLevelTypes: epicTypes.split(',').map(t => t.trim()).filter(Boolean),
        featureLevelTypes: featureTypes.split(',').map(t => t.trim()).filter(Boolean) || ['Epic', 'Feature'],
    };
}
// =============================================================================
// JIRA AUTO-DETECTION
// =============================================================================
/**
 * Auto-detect JIRA structure: projects and their boards
 */
export async function detectJiraStructure() {
    const spinner = ora('Analyzing JIRA structure...').start();
    try {
        const client = new JiraClient();
        // 1. Fetch all accessible projects
        const projects = await client.getProjects();
        if (!projects || projects.length === 0) {
            spinner.warn('No JIRA projects found');
            return null;
        }
        // 2. For each project, fetch boards
        const projectInfos = [];
        let totalBoards = 0;
        for (const project of projects.slice(0, 20)) { // Limit to 20 projects for performance
            try {
                const boards = await fetchBoardsForProject(client, project.key);
                projectInfos.push({
                    key: project.key,
                    name: project.name,
                    boards: boards || [],
                });
                totalBoards += boards?.length || 0;
            }
            catch {
                // Skip projects where we can't fetch boards
                projectInfos.push({
                    key: project.key,
                    name: project.name,
                    boards: [],
                });
            }
        }
        // 3. Analyze structure and recommend mode
        const recommendedMode = analyzeJiraStructure(projectInfos, totalBoards);
        const analysis = analyzeJiraStructureDeep(projectInfos);
        spinner.succeed(`Found ${projectInfos.length} projects with ${totalBoards} boards`);
        return {
            projects: projectInfos,
            totalBoards,
            recommendedMode,
            analysis,
        };
    }
    catch (error) {
        spinner.fail('Failed to analyze JIRA structure');
        console.log(chalk.gray(`   Error: ${error instanceof Error ? error.message : String(error)}`));
        return null;
    }
}
/**
 * Analyze JIRA structure and recommend organization mode
 */
function analyzeJiraStructure(projects, totalBoards) {
    const projectCount = projects.length;
    const avgBoardsPerProject = totalBoards / projectCount;
    // Single project
    if (projectCount === 1) {
        const boards = projects[0].boards.length;
        if (boards <= 1)
            return 'simple';
        return 'by-board'; // Multiple boards in single project → board-based
    }
    // Multiple projects
    if (avgBoardsPerProject <= 1.5) {
        return 'by-project'; // Most projects have 1 board → project-based
    }
    return 'hybrid'; // Multiple projects with multiple boards → 2-level
}
/**
 * Display JIRA structure and get user confirmation
 */
export async function confirmJiraMapping(structure) {
    console.log(chalk.cyan('\n📊 Detected JIRA Structure:\n'));
    // Show detected structure
    for (const project of structure.projects.slice(0, 10)) {
        const boardList = project.boards.length > 0
            ? project.boards.map(b => `${b.name}${b.type ? ` (${b.type})` : ''}`).join(', ')
            : '(no boards)';
        console.log(chalk.white(`   ${project.key}`), chalk.gray(`(${project.boards.length} boards): ${boardList}`));
    }
    if (structure.projects.length > 10) {
        console.log(chalk.gray(`   ... and ${structure.projects.length - 10} more projects`));
    }
    // Show analysis insight
    console.log('');
    const { analysis } = structure;
    const patternEmoji = {
        'tech-stack': '🔧',
        'squad-based': '👥',
        'domain-based': '📦',
        'service-based': '⚙️',
        'feature-based': '🎯',
        'mixed': '🔀',
        'unknown': '❓',
    };
    console.log(chalk.yellow(`${patternEmoji[analysis.pattern]} Team Pattern: ${analysis.pattern} (${analysis.confidence} confidence)`));
    console.log(chalk.gray(`   ${analysis.reasoning}`));
    if (analysis.scrumBoardCount > 0 || analysis.kanbanBoardCount > 0) {
        console.log(chalk.gray(`   Methodology: ${analysis.scrumBoardCount} Scrum boards, ${analysis.kanbanBoardCount} Kanban boards`));
    }
    // Show recommended organization
    console.log('');
    const modeDescriptions = {
        'simple': 'All items → specs/jira/',
        'by-project': 'specs/JIRA-{PROJECT}/',
        'by-board': 'specs/JIRA-{PROJECT}/{board}/',
        'hybrid': 'specs/JIRA-{PROJECT}/{board}/ (2-level)',
    };
    console.log(chalk.green(`💡 Recommended: ${modeDescriptions[structure.recommendedMode]}`));
    console.log('');
    // Let user choose
    const mode = await select({
        message: 'How do you want to organize JIRA items?',
        choices: [
            {
                name: `${structure.recommendedMode === 'simple' ? '● ' : '○ '}Simple (all items in one folder)`,
                value: 'simple',
            },
            {
                name: `${structure.recommendedMode === 'by-project' ? '● ' : '○ '}By Project (specs/JIRA-{PROJECT}/)`,
                value: 'by-project',
            },
            {
                name: `${structure.recommendedMode === 'by-board' || structure.recommendedMode === 'hybrid' ? '● ' : '○ '}By Board (specs/JIRA-{PROJECT}/{board}/) [2-level]`,
                value: 'hybrid',
            },
        ],
        default: structure.recommendedMode === 'by-board' ? 'hybrid' : structure.recommendedMode,
    });
    // Build mappings based on mode
    const mappings = new Map();
    for (const project of structure.projects) {
        const projectFolder = normalizeToProjectId(project.key);
        const boardMappings = new Map();
        if (mode === 'simple') {
            boardMappings.set('*', 'jira');
        }
        else if (mode === 'by-project') {
            boardMappings.set('*', `JIRA-${projectFolder}`);
        }
        else {
            // hybrid/by-board: each board gets its own folder
            if (project.boards.length === 0) {
                boardMappings.set('*', `JIRA-${projectFolder}/default`);
            }
            else {
                for (const board of project.boards) {
                    const boardFolder = normalizeToProjectId(board.name);
                    boardMappings.set(board.name, `JIRA-${projectFolder}/${boardFolder}`);
                }
            }
        }
        mappings.set(project.key, boardMappings);
    }
    // Show preview
    console.log(chalk.cyan('\n📁 Folder Structure Preview:\n'));
    let shown = 0;
    outer: for (const [projectKey, boardMap] of mappings) {
        for (const [, folder] of boardMap) {
            console.log(chalk.gray(`   specs/${folder}/FS-XXX/`));
            shown++;
            if (shown >= 6) {
                if (mappings.size > 3)
                    console.log(chalk.gray('   ...'));
                break outer;
            }
        }
    }
    // Step 2: Select hierarchy mapping (v0.33.0+ - Universal 3/4/5 Level Support)
    const hierarchyMapping = await selectJiraHierarchyMapping();
    return { mode, mappings, hierarchyMapping };
}
// =============================================================================
// ADO AUTO-DETECTION
// =============================================================================
/**
 * Auto-detect ADO structure: projects and their area paths
 */
export async function detectAdoStructure(organization, pat) {
    const spinner = ora('Analyzing Azure DevOps structure...').start();
    try {
        // Fetch projects using ADO API directly
        const projectsUrl = `https://dev.azure.com/${organization}/_apis/projects?api-version=7.0`;
        const projectsResponse = await fetch(projectsUrl, {
            headers: {
                'Authorization': `Basic ${Buffer.from(`:${pat}`).toString('base64')}`,
                'Content-Type': 'application/json',
            },
        });
        if (!projectsResponse.ok) {
            throw new Error(`ADO API error: ${projectsResponse.status}`);
        }
        const projectsData = await projectsResponse.json();
        const projects = projectsData.value || [];
        if (projects.length === 0) {
            spinner.warn('No ADO projects found');
            return null;
        }
        // For each project, fetch area paths
        const projectInfos = [];
        let totalAreaPaths = 0;
        for (const project of projects.slice(0, 10)) { // Limit to 10 projects
            try {
                const areaPaths = await fetchAreaPathsForProject(organization, project.name, pat);
                // Filter to top-level area paths only (direct children of project root)
                const topLevelPaths = areaPaths.filter(ap => {
                    const parts = ap.path.split('\\');
                    return parts.length === 2; // "Project\Area"
                });
                projectInfos.push({
                    name: project.name,
                    areaPaths: topLevelPaths,
                });
                totalAreaPaths += topLevelPaths.length;
            }
            catch {
                projectInfos.push({
                    name: project.name,
                    areaPaths: [],
                });
            }
        }
        // Analyze structure and recommend mode
        const recommendedMode = analyzeAdoStructure(projectInfos, totalAreaPaths);
        const analysis = analyzeAdoStructureDeep(projectInfos);
        spinner.succeed(`Found ${projectInfos.length} projects with ${totalAreaPaths} area paths`);
        return {
            projects: projectInfos,
            totalAreaPaths,
            recommendedMode,
            analysis,
        };
    }
    catch (error) {
        spinner.fail('Failed to analyze ADO structure');
        console.log(chalk.gray(`   Error: ${error instanceof Error ? error.message : String(error)}`));
        return null;
    }
}
/**
 * Analyze ADO structure and recommend organization mode
 */
function analyzeAdoStructure(projects, totalAreaPaths) {
    const projectCount = projects.length;
    const avgAreasPerProject = totalAreaPaths / projectCount;
    // Single project
    if (projectCount === 1) {
        const areas = projects[0].areaPaths.length;
        if (areas <= 1)
            return 'simple';
        return 'by-area'; // Multiple area paths → area-based (2-level)
    }
    // Multiple projects
    if (avgAreasPerProject <= 1.5) {
        return 'by-project'; // Most projects have few area paths → project-based
    }
    return 'by-area'; // Multiple projects with multiple areas → 2-level
}
/**
 * Display ADO structure and get user confirmation
 */
export async function confirmAdoMapping(structure) {
    console.log(chalk.cyan('\n📊 Detected Azure DevOps Structure:\n'));
    // Show detected structure
    for (const project of structure.projects.slice(0, 10)) {
        const areaList = project.areaPaths.length > 0
            ? project.areaPaths.map(a => a.name).join(', ')
            : '(root only)';
        console.log(chalk.white(`   ${project.name}`), chalk.gray(`(${project.areaPaths.length} areas): ${areaList}`));
    }
    if (structure.projects.length > 10) {
        console.log(chalk.gray(`   ... and ${structure.projects.length - 10} more projects`));
    }
    // Show analysis insight
    console.log('');
    const { analysis } = structure;
    const patternEmoji = {
        'tech-stack': '🔧',
        'squad-based': '👥',
        'domain-based': '📦',
        'service-based': '⚙️',
        'feature-based': '🎯',
        'mixed': '🔀',
        'unknown': '❓',
    };
    console.log(chalk.yellow(`${patternEmoji[analysis.pattern]} Team Pattern: ${analysis.pattern} (${analysis.confidence} confidence)`));
    console.log(chalk.gray(`   ${analysis.reasoning}`));
    // Show recommended organization
    console.log('');
    const modeDescriptions = {
        'simple': 'All items → specs/ado/',
        'by-project': 'specs/{PROJECT}/',
        'by-area': 'specs/{PROJECT}/{area}/ (2-level)',
    };
    console.log(chalk.green(`💡 Recommended: ${modeDescriptions[structure.recommendedMode]}`));
    console.log('');
    // Let user choose
    const mode = await select({
        message: 'How do you want to organize ADO items?',
        choices: [
            {
                name: `${structure.recommendedMode === 'simple' ? '● ' : '○ '}Simple (all items in one folder)`,
                value: 'simple',
            },
            {
                name: `${structure.recommendedMode === 'by-project' ? '● ' : '○ '}By Project (specs/{PROJECT}/)`,
                value: 'by-project',
            },
            {
                name: `${structure.recommendedMode === 'by-area' ? '● ' : '○ '}By Area Path (specs/{PROJECT}/{area}/) [2-level]`,
                value: 'by-area',
            },
        ],
        default: structure.recommendedMode,
    });
    // Build mappings based on mode
    const mappings = new Map();
    for (const project of structure.projects) {
        const projectFolder = normalizeToProjectId(project.name);
        const areaMappings = new Map();
        if (mode === 'simple') {
            areaMappings.set('*', 'ado');
        }
        else if (mode === 'by-project') {
            areaMappings.set('*', projectFolder);
        }
        else {
            // by-area: each area path gets its own folder
            if (project.areaPaths.length === 0) {
                areaMappings.set('*', `${projectFolder}/_default`);
            }
            else {
                for (const area of project.areaPaths) {
                    const areaFolder = normalizeToProjectId(area.name);
                    areaMappings.set(area.path, `${projectFolder}/${areaFolder}`);
                }
            }
        }
        mappings.set(project.name, areaMappings);
    }
    // Show preview
    console.log(chalk.cyan('\n📁 Folder Structure Preview:\n'));
    let shown = 0;
    outer: for (const [, areaMap] of mappings) {
        for (const [, folder] of areaMap) {
            console.log(chalk.gray(`   specs/${folder}/FS-XXX/`));
            shown++;
            if (shown >= 6) {
                if (mappings.size > 3)
                    console.log(chalk.gray('   ...'));
                break outer;
            }
        }
    }
    // Step 2: Select hierarchy mapping (v0.33.0+ - Universal 3/4/5 Level Support)
    const hierarchyMapping = await selectAdoHierarchyMapping();
    return { mode, mappings, hierarchyMapping };
}
// =============================================================================
// COORDINATOR CONFIG BUILDERS
// =============================================================================
/**
 * Build JIRA coordinator config from mapping result
 */
export function buildJiraCoordinatorConfig(baseConfig, mapping) {
    const projectMappings = [];
    for (const [projectKey, boardMap] of mapping.mappings) {
        const boardMappings = [];
        for (const [boardName, folder] of boardMap) {
            boardMappings.push({ boardName, specweaveFolder: folder });
        }
        projectMappings.push({ projectKey, boardMappings });
    }
    return {
        ...baseConfig,
        mode: mapping.mode,
        projectMappings,
        hierarchyMapping: mapping.hierarchyMapping,
    };
}
/**
 * Build ADO coordinator config from mapping result
 */
export function buildAdoCoordinatorConfig(baseConfig, mapping) {
    const projectMappings = [];
    for (const [projectName, areaMap] of mapping.mappings) {
        const areaMappings = [];
        for (const [areaPath, folder] of areaMap) {
            areaMappings.push({ areaPath, specweaveFolder: folder });
        }
        projectMappings.push({ projectName, areaMappings });
    }
    return {
        orgUrl: baseConfig.orgUrl,
        project: baseConfig.project,
        pat: baseConfig.pat,
        mode: mapping.mode,
        projectMappings,
        hierarchyMapping: mapping.hierarchyMapping,
    };
}
//# sourceMappingURL=jira-ado-auto-detect.js.map