/**
 * Import Configuration Loader
 *
 * Loads import configuration from .specweave/config.json and environment variables.
 * Environment variables override config file settings.
 */
import * as fs from '../utils/fs-native.js';
import path from 'path';
/**
 * Default import configuration
 */
export const DEFAULT_IMPORT_CONFIG = {
    enabled: true,
    timeRangeMonths: 1,
    pageSize: 100
};
/**
 * Load import configuration from project
 * Merges config file with environment variables (env vars take precedence)
 */
export function loadImportConfig(projectRoot) {
    // Start with defaults
    let config = { ...DEFAULT_IMPORT_CONFIG };
    // Load from config file
    const configPath = path.join(projectRoot, '.specweave', 'config.json');
    if (fs.existsSync(configPath)) {
        try {
            const projectConfig = fs.readJsonSync(configPath);
            if (projectConfig.externalImport) {
                config = { ...config, ...projectConfig.externalImport };
            }
        }
        catch (error) {
            // Invalid config file - use defaults
        }
    }
    // Override with environment variables
    const envConfig = loadFromEnvironment();
    config = { ...config, ...envConfig };
    return config;
}
/**
 * Load configuration from environment variables
 * Environment variables take precedence over config file
 */
export function loadFromEnvironment() {
    const config = {};
    const enabledEnv = process.env.SPECWEAVE_IMPORT_ENABLED;
    if (enabledEnv !== undefined) {
        config.enabled = enabledEnv === 'true';
    }
    const monthsEnv = process.env.SPECWEAVE_IMPORT_TIME_RANGE_MONTHS;
    if (monthsEnv !== undefined) {
        const months = parseInt(monthsEnv, 10);
        if (!isNaN(months) && months > 0) {
            config.timeRangeMonths = months;
        }
    }
    const pageSizeEnv = process.env.SPECWEAVE_IMPORT_PAGE_SIZE;
    if (pageSizeEnv !== undefined) {
        const pageSize = parseInt(pageSizeEnv, 10);
        if (!isNaN(pageSize) && pageSize > 0 && pageSize <= 1000) {
            config.pageSize = pageSize;
        }
    }
    return config;
}
/**
 * Validate import configuration
 * Throws error if configuration is invalid
 */
export function validateImportConfig(config) {
    // Check time range
    if (config.timeRangeMonths < 1 || config.timeRangeMonths > 120) {
        throw new Error(`Invalid timeRangeMonths: ${config.timeRangeMonths}. Must be between 1 and 120 months.`);
    }
    // Check page size
    if (config.pageSize < 1 || config.pageSize > 1000) {
        throw new Error(`Invalid pageSize: ${config.pageSize}. Must be between 1 and 1000.`);
    }
    // Validate GitHub config
    if (config.github) {
        if (config.github.stateFilter) {
            const validStates = ['open', 'closed'];
            const invalidStates = config.github.stateFilter.filter(state => !validStates.includes(state));
            if (invalidStates.length > 0) {
                throw new Error(`Invalid GitHub stateFilter: ${invalidStates.join(', ')}. ` +
                    `Valid values: ${validStates.join(', ')}`);
            }
        }
    }
}
/**
 * Save import configuration to project config file
 */
export function saveImportConfig(projectRoot, importConfig) {
    const configPath = path.join(projectRoot, '.specweave', 'config.json');
    // Read existing config or create new
    let projectConfig = {};
    if (fs.existsSync(configPath)) {
        try {
            projectConfig = fs.readJsonSync(configPath);
        }
        catch (error) {
            // Invalid config - start fresh
        }
    }
    // Update import config section
    projectConfig.externalImport = importConfig;
    // Write back
    fs.writeJsonSync(configPath, projectConfig, { spaces: 2 });
}
/**
 * Get example import configuration
 * Useful for documentation and init templates
 */
export function getExampleImportConfig() {
    return {
        enabled: true,
        timeRangeMonths: 3,
        pageSize: 100,
        github: {
            labelFilter: ['enhancement', 'feature'],
            stateFilter: ['open', 'closed']
        },
        jira: {
            jqlFilter: 'project = MYPROJECT AND type IN (Epic, Story)',
            issueTypes: ['Epic', 'Story']
        },
        ado: {
            wiqlFilter: `SELECT [System.Id] FROM WorkItems WHERE [System.WorkItemType] IN ('Epic', 'User Story')`,
            workItemTypes: ['Epic', 'User Story']
        }
    };
}
//# sourceMappingURL=import-config.js.map