/**
 * Smart Filter Module
 *
 * Provides intelligent filtering for project imports with JQL/WIQL support,
 * preview mode, and dry-run capabilities.
 *
 * @module cli/helpers/smart-filter
 */
import chalk from 'chalk';
import { select, input, confirm } from '@inquirer/prompts';
/**
 * Validate JQL query syntax (JIRA)
 *
 * Basic validation:
 * - Check for common JQL keywords (project, type, status, etc.)
 * - Check for balanced parentheses
 * - Check for valid operators (=, !=, IN, NOT IN, etc.)
 *
 * @param jql - JQL query string
 * @returns Validation result with error message if invalid
 */
export function validateJQL(jql) {
    if (!jql || jql.trim().length === 0) {
        return { valid: false, error: 'JQL query cannot be empty' };
    }
    // Check balanced parentheses
    const openCount = (jql.match(/\(/g) || []).length;
    const closeCount = (jql.match(/\)/g) || []).length;
    if (openCount !== closeCount) {
        return { valid: false, error: 'Unbalanced parentheses in JQL query' };
    }
    // Check for valid JQL keywords
    const validKeywords = [
        'project', 'type', 'status', 'priority', 'assignee', 'reporter',
        'created', 'updated', 'resolved', 'labels', 'component', 'fixVersion',
        'affectsVersion', 'sprint', 'epic', 'parent', 'key'
    ];
    const hasValidKeyword = validKeywords.some(keyword => jql.toLowerCase().includes(keyword.toLowerCase()));
    if (!hasValidKeyword) {
        return {
            valid: false,
            error: 'JQL query must include at least one valid field (project, type, status, etc.)'
        };
    }
    // Check for valid operators
    const validOperators = ['=', '!=', 'IN', 'NOT IN', '~', '!~', '>', '<', '>=', '<=', 'IS', 'IS NOT'];
    const hasValidOperator = validOperators.some(op => jql.includes(op));
    if (!hasValidOperator) {
        return {
            valid: false,
            error: 'JQL query must include at least one valid operator (=, IN, etc.)'
        };
    }
    return { valid: true };
}
/**
 * Validate WIQL query syntax (Azure DevOps)
 *
 * Basic validation for WIQL (Work Item Query Language)
 *
 * @param wiql - WIQL query string
 * @returns Validation result with error message if invalid
 */
export function validateWIQL(wiql) {
    if (!wiql || wiql.trim().length === 0) {
        return { valid: false, error: 'WIQL query cannot be empty' };
    }
    // WIQL must start with SELECT
    if (!wiql.trim().toUpperCase().startsWith('SELECT')) {
        return {
            valid: false,
            error: 'WIQL query must start with SELECT'
        };
    }
    // WIQL must contain FROM
    if (!wiql.toUpperCase().includes('FROM')) {
        return {
            valid: false,
            error: 'WIQL query must include FROM clause'
        };
    }
    return { valid: true };
}
/**
 * Prompt user for filter criteria
 *
 * Interactive prompts for:
 * - Filter type (JQL/WIQL, project keys, or none)
 * - Exclude archived projects
 * - Exclude deleted projects
 * - Only active projects
 *
 * @param provider - Filter provider (jira or ado)
 * @returns Filter criteria
 */
export async function promptFilterCriteria(provider) {
    console.log(chalk.cyan(`\n🔍 ${provider.toUpperCase()} Filter Configuration\n`));
    const criteria = { provider };
    // Step 1: Filter type
    const filterType = await select({
        message: 'How would you like to filter projects?',
        default: 'none',
        choices: [
            {
                name: chalk.green('No filter') + chalk.gray(' - Import all projects'),
                value: 'none'
            },
            {
                name: `Custom ${provider === 'jira' ? 'JQL' : 'WIQL'} query` + chalk.gray(' - Advanced filtering'),
                value: 'query'
            },
            {
                name: 'Specific project keys' + chalk.gray(' - Manual selection'),
                value: 'keys'
            }
        ]
    });
    if (filterType === 'query') {
        // Custom query
        const query = await input({
            message: `Enter ${provider === 'jira' ? 'JQL' : 'WIQL'} query:`,
            validate: (value) => {
                const validation = provider === 'jira'
                    ? validateJQL(value)
                    : validateWIQL(value);
                return validation.valid || chalk.red(validation.error || 'Invalid query');
            }
        });
        criteria.query = query;
    }
    else if (filterType === 'keys') {
        // Specific project keys
        const keys = await input({
            message: 'Enter project keys (comma-separated):',
            validate: (value) => {
                const trimmed = value.trim();
                if (!trimmed) {
                    return chalk.red('Project keys cannot be empty');
                }
                const keysList = trimmed.split(',').map(k => k.trim()).filter(k => k.length > 0);
                if (keysList.length === 0) {
                    return chalk.red('At least one project key required');
                }
                return true;
            }
        });
        criteria.projectKeys = keys.split(',').map(k => k.trim()).filter(k => k.length > 0);
    }
    // Step 2: Additional filters
    const excludeArchived = await confirm({
        message: 'Exclude archived projects?',
        default: true
    });
    const excludeDeleted = await confirm({
        message: 'Exclude deleted projects?',
        default: true
    });
    const onlyActive = await confirm({
        message: 'Only import active projects?',
        default: false
    });
    criteria.excludeArchived = excludeArchived;
    criteria.excludeDeleted = excludeDeleted;
    criteria.onlyActive = onlyActive;
    return criteria;
}
/**
 * Apply filters to project list
 *
 * @param projects - List of projects to filter
 * @param criteria - Filter criteria
 * @returns Filtered project list
 */
export function applyFilters(projects, criteria) {
    const total = projects.length;
    let filtered = projects;
    // Filter by project keys
    if (criteria.projectKeys && criteria.projectKeys.length > 0) {
        filtered = filtered.filter(p => criteria.projectKeys.includes(p.key) || criteria.projectKeys.includes(p.name));
    }
    // Exclude archived (p.archived is falsy or explicitly false)
    if (criteria.excludeArchived) {
        filtered = filtered.filter(p => !p.archived);
    }
    // Exclude deleted (p.deleted is falsy or explicitly false)
    if (criteria.excludeDeleted) {
        filtered = filtered.filter(p => !p.deleted);
    }
    // Only active
    if (criteria.onlyActive) {
        filtered = filtered.filter(p => p.status === 'active' || p.state === 'active');
    }
    return {
        total,
        filtered: filtered.length,
        excluded: total - filtered.length,
        items: filtered,
        criteria
    };
}
/**
 * Preview filter results
 *
 * Shows summary of what would be imported with current filter criteria.
 *
 * @param result - Filter result
 * @returns User confirmation to proceed
 */
export async function previewFilterResults(result) {
    console.log(chalk.cyan('\n📊 Filter Preview\n'));
    console.log(chalk.white(`Total projects:    ${result.total}`));
    console.log(chalk.green(`Filtered (import): ${result.filtered}`));
    console.log(chalk.gray(`Excluded:          ${result.excluded}`));
    if (result.criteria.query) {
        console.log(chalk.gray(`\nQuery: ${result.criteria.query}`));
    }
    if (result.criteria.projectKeys && result.criteria.projectKeys.length > 0) {
        console.log(chalk.gray(`\nProject keys: ${result.criteria.projectKeys.join(', ')}`));
    }
    console.log(chalk.gray(`\nFilters applied:`));
    console.log(chalk.gray(`  - Exclude archived: ${result.criteria.excludeArchived ? 'Yes' : 'No'}`));
    console.log(chalk.gray(`  - Exclude deleted:  ${result.criteria.excludeDeleted ? 'Yes' : 'No'}`));
    console.log(chalk.gray(`  - Only active:      ${result.criteria.onlyActive ? 'Yes' : 'No'}`));
    // Show sample projects (first 5)
    if (result.items.length > 0) {
        console.log(chalk.cyan('\n📋 Sample projects to import (first 5):\n'));
        result.items.slice(0, 5).forEach((p, i) => {
            console.log(chalk.gray(`   ${i + 1}. ${p.key || p.name} - ${p.name}`));
        });
        if (result.items.length > 5) {
            console.log(chalk.gray(`   ... and ${result.items.length - 5} more\n`));
        }
    }
    // Confirmation
    const proceed = await confirm({
        message: 'Proceed with import?',
        default: true
    });
    return proceed;
}
//# sourceMappingURL=smart-filter.js.map