import sortArray from "sort-array";
import { extractRegexMatches } from "../utils/index.js";
import { TicketProviderRoot } from "./ticketProviderRoot.js";
import { getEnvVar } from "../../config/index.js";
export class GenericTicketingProvider extends TicketProviderRoot {
    ticketRefRegex;
    ticketUrlBuilder;
    constructor() {
        super();
        this.ticketRefRegex = getEnvVar("GENERIC_TICKETING_PROVIDER_REGEX"); // Example: ([R|I][0-9]+-[0-9]+)
        this.ticketUrlBuilder = getEnvVar("GENERIC_TICKETING_PROVIDER_URL_BUILDER"); // Example: https://instance.easyvista.com/index.php?ticket={REF}
        if (this.ticketRefRegex && this.ticketUrlBuilder) {
            this.isActive = true;
        }
    }
    static isAvailable() {
        return getEnvVar("GENERIC_TICKETING_PROVIDER_REGEX") && getEnvVar("GENERIC_TICKETING_PROVIDER_URL_BUILDER");
    }
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    static async getTicketsFromString(text, options = {}) {
        const tickets = [];
        if (!this.isAvailable()) {
            return tickets;
        }
        // Extract tickets using GENERIC_TICKETING_PROVIDER_REGEX regexp
        const ticketRefRegexExec = new RegExp(getEnvVar("GENERIC_TICKETING_PROVIDER_REGEX") || "", "g");
        const regexMatches = await extractRegexMatches(ticketRefRegexExec, text);
        const ticketUrlBuilder = getEnvVar("GENERIC_TICKETING_PROVIDER_URL_BUILDER") || "";
        for (const genericTicketRef of regexMatches) {
            const genericTicketUrl = ticketUrlBuilder.replace("{REF}", genericTicketRef);
            if (!tickets.some((ticket) => ticket.url === genericTicketUrl)) {
                tickets.push({
                    provider: "GENERIC",
                    url: genericTicketUrl,
                    id: genericTicketRef,
                });
            }
        }
        const ticketsSorted = sortArray(tickets, { by: ["id"], order: ["asc"] });
        return ticketsSorted;
    }
    getLabel() {
        return "sfdx-hardis Generic ticketing system connector";
    }
    async collectTicketsInfo(tickets) {
        // No remote server here so do nothing
        return tickets;
    }
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    async postDeploymentComments(tickets, _org, _pullRequestInfo) {
        // No remote server here so do nothing
        return tickets;
    }
}
//# sourceMappingURL=genericProvider.js.map