/**
 * Jira Hierarchy Mapper
 *
 * Maps SpecWeave universal hierarchy to Jira issue types
 *
 * Supports:
 * - Jira Agile: Initiative → Epic → Story → Sub-task
 * - Jira CMMI: Epic → Feature → Requirement → Task
 * - Jira SAFe: Strategic Theme → Capability → User Story → Task
 *
 * @module integrations/jira/jira-hierarchy-mapper
 */
import chalk from 'chalk';
/**
 * Maps SpecWeave universal hierarchy to Jira issue types
 */
export class JiraHierarchyMapper {
    constructor(jiraClient) {
        this.jiraClient = jiraClient;
    }
    /**
     * Detect Jira project type based on available issue types
     *
     * Currently returns default Agile hierarchy.
     * TODO: Implement getIssueTypes in JiraClient for dynamic detection.
     *
     * @param projectKey - Jira project key
     * @returns Detected hierarchy mapping
     */
    async detectProjectType(projectKey) {
        // Default: Agile (has "Story" and "Sub-task")
        console.log(chalk.cyan(`📊 Using Jira Agile hierarchy for project: ${projectKey}`));
        return {
            epic: 'Initiative',
            feature: 'Epic',
            userStory: 'Story',
            task: 'Sub-task'
        };
    }
    /**
     * Map SpecWeave work item to Jira issue type
     *
     * @param level - SpecWeave hierarchy level
     * @param mapping - Jira hierarchy mapping
     * @returns Jira issue type name
     */
    mapToJiraIssueType(level, mapping) {
        return mapping[level];
    }
    /**
     * Create SpecWeave Epic in Jira
     *
     * @param epic - SpecWeave epic metadata
     * @param projectKey - Jira project key
     * @returns Created Jira issue key
     */
    async syncEpicToJira(epic, projectKey) {
        const mapping = await this.detectProjectType(projectKey);
        const jiraIssueType = this.mapToJiraIssueType('epic', mapping);
        console.log(chalk.cyan(`📝 Creating ${jiraIssueType} in Jira: ${epic.title}`));
        // Create Initiative/Theme/Epic in Jira
        const issue = await this.jiraClient.createIssue({
            issueType: jiraIssueType,
            summary: epic.title,
            description: epic.description,
            priority: epic.priority,
            labels: [`specweave-epic-${epic.id}`]
        }, projectKey);
        console.log(chalk.green(`✅ Created ${jiraIssueType}: ${issue.key}\n`));
        return issue.key;
    }
    /**
     * Create SpecWeave Feature in Jira
     *
     * @param feature - SpecWeave feature metadata
     * @param projectKey - Jira project key
     * @param epicKey - Parent epic key (optional)
     * @returns Created Jira issue key
     */
    async syncFeatureToJira(feature, projectKey, epicKey) {
        const mapping = await this.detectProjectType(projectKey);
        const jiraIssueType = this.mapToJiraIssueType('feature', mapping);
        console.log(chalk.cyan(`📝 Creating ${jiraIssueType} in Jira: ${feature.title}`));
        // Create Epic/Capability/Feature in Jira
        const issue = await this.jiraClient.createIssue({
            issueType: jiraIssueType,
            summary: feature.title,
            description: feature.description,
            priority: feature.priority,
            labels: [`specweave-feature-${feature.id}`],
            epicKey // Link to parent epic (if exists)
        }, projectKey);
        console.log(chalk.green(`✅ Created ${jiraIssueType}: ${issue.key}\n`));
        return issue.key;
    }
    /**
     * Create SpecWeave User Story in Jira
     *
     * @param userStory - SpecWeave user story metadata
     * @param projectKey - Jira project key
     * @param featureKey - Parent feature key (optional)
     * @returns Created Jira issue key
     */
    async syncUserStoryToJira(userStory, projectKey, featureKey) {
        const mapping = await this.detectProjectType(projectKey);
        const jiraIssueType = this.mapToJiraIssueType('userStory', mapping);
        console.log(chalk.cyan(`📝 Creating ${jiraIssueType} in Jira: ${userStory.title}`));
        // Build description with acceptance criteria
        let description = userStory.description;
        if (userStory.acceptanceCriteria && userStory.acceptanceCriteria.length > 0) {
            description += '\n\n**Acceptance Criteria:**\n';
            userStory.acceptanceCriteria.forEach((ac, index) => {
                description += `\n${index + 1}. ${ac}`;
            });
        }
        // Create Story/User Story/Requirement in Jira
        const issue = await this.jiraClient.createIssue({
            issueType: jiraIssueType,
            summary: userStory.title,
            description,
            priority: userStory.priority,
            labels: [`specweave-us-${userStory.id}`],
            epicKey: featureKey // Link to parent feature
        }, projectKey);
        console.log(chalk.green(`✅ Created ${jiraIssueType}: ${issue.key}\n`));
        return issue.key;
    }
}
//# sourceMappingURL=jira-hierarchy-mapper.js.map