import c from "chalk";
import sortArray from "sort-array";
import { JiraProvider } from "./jiraProvider.js";
import { uxLog } from "../utils/index.js";
import { GenericTicketingProvider } from "./genericProvider.js";
import { AzureBoardsProvider } from "./azureBoardsProvider.js";
export const allTicketProviders = [JiraProvider, GenericTicketingProvider, AzureBoardsProvider];
export class TicketProvider {
    static getInstances() {
        const ticketProviders = [];
        for (const provider of allTicketProviders) {
            if (provider.isAvailable()) {
                ticketProviders.push(new provider());
            }
        }
        return ticketProviders;
    }
    // Returns all providers ticket references from input string
    static async getProvidersTicketsFromString(text, options) {
        const tickets = [];
        for (const ticketProvider of allTicketProviders) {
            const providerTickets = await ticketProvider.getTicketsFromString(text, options);
            tickets.push(...providerTickets);
        }
        const ticketsSorted = sortArray(tickets, { by: ["id"], order: ["asc"] });
        return ticketsSorted;
    }
    // Adds ticket info by calling ticket providers APIs when possible
    static async collectTicketsInfo(tickets) {
        const ticketProviders = this.getInstances();
        if (ticketProviders.length === 0) {
            uxLog("error", this, c.grey(`[TicketProvider] No ticket provider has been configured`));
        }
        for (const ticketProvider of ticketProviders) {
            if (ticketProvider.isActive) {
                await ticketProvider.collectTicketsInfo(tickets);
            }
        }
        return tickets;
    }
    // Process Ticket providers actions after a deployment.
    // Can be comments on JIRA, and maybe later status changes ? :)
    static async postDeploymentActions(tickets, org, pullRequestInfo) {
        const ticketProviders = this.getInstances();
        for (const ticketProvider of ticketProviders) {
            if (ticketProvider.isActive) {
                await ticketProvider.postDeploymentComments(tickets, org, pullRequestInfo);
            }
        }
        return tickets;
    }
}
//# sourceMappingURL=index.js.map