import { SfError } from "@salesforce/core";
import c from "chalk";
import { uxLog } from "../utils/index.js";
import { extractImagesFromMarkdown, replaceImagesInMarkdown } from "./utilsMarkdown.js";
import { getEnvVar } from "../../config/index.js";
export class GitProviderRoot {
    serverUrl;
    token;
    getLabel() {
        throw new SfError("getLabel should be implemented on this call");
    }
    async getBranchDeploymentCheckId(gitBranch) {
        uxLog("other", this, `Method getBranchDeploymentCheckId(${gitBranch}) is not implemented yet on ${this.getLabel()}`);
        return null;
    }
    async getPullRequestDeploymentCheckId() {
        uxLog("other", this, `Method getPullRequestDeploymentCheckId() is not implemented yet on ${this.getLabel()}`);
        return null;
    }
    async getCurrentJobUrl() {
        uxLog("other", this, `Method getCurrentJobUrl is not implemented yet on ${this.getLabel()}`);
        return null;
    }
    async getCurrentBranchUrl() {
        uxLog("other", this, `Method getCurrentBranchUrl is not implemented yet on ${this.getLabel()}`);
        return null;
    }
    async supportsMermaidInPrMarkdown() {
        uxLog("other", this, `Method supportsMermaidInPrMarkdown is not implemented yet on ${this.getLabel()}`);
        return false;
    }
    async supportsSvgAttachments() {
        // False by default, might be used later
        return false;
    }
    async getPullRequestInfo() {
        uxLog("other", this, `Method getPullRequestInfo is not implemented yet on ${this.getLabel()}`);
        return null;
    }
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    async uploadImage(image) {
        uxLog("other", this, `Method uploadImage is not implemented yet on ${this.getLabel()}`);
        return null;
    }
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    async listPullRequests(filters = {}, options = { formatted: false }) {
        uxLog("other", this, `Method listPullRequests is not implemented yet on ${this.getLabel()}`);
        return null;
    }
    async postPullRequestMessage(prMessage) {
        uxLog("warning", this, c.yellow("Method postPullRequestMessage is not yet implemented on " + this.getLabel() + " to post " + JSON.stringify(prMessage)));
        return { posted: false, providerResult: { error: "Not implemented in sfdx-hardis" } };
    }
    /* jscpd:ignore-start */
    // Do not make crash the whole process in case there is an issue with integration
    async tryPostPullRequestMessage(prMessage) {
        let prResult = null;
        try {
            prResult = await this.postPullRequestMessage(prMessage);
        }
        catch (e) {
            uxLog("warning", this, c.yellow(`[GitProvider] Error while trying to post pull request message.\n${e.message}\n${e.stack}`));
            prResult = { posted: false, providerResult: { error: e } };
        }
        return prResult;
    }
    /* jscpd:ignore-end */
    async uploadAndReplaceImageReferences(markdownBody, sourceFile = null) {
        const replacements = {};
        const markdownImages = extractImagesFromMarkdown(markdownBody, sourceFile);
        for (const image of markdownImages) {
            const imageUrl = await this.uploadImage(image.path);
            if (imageUrl) {
                replacements[image.name] = imageUrl;
            }
        }
        markdownBody = replaceImagesInMarkdown(markdownBody, replacements);
        return markdownBody;
    }
    completeWithCustomBehaviors(pullRequestInfo) {
        const desc = pullRequestInfo.description || "";
        if (desc.includes("NO_DELTA")
            || getEnvVar("NO_DELTA") === "true"
            || getEnvVar("NO_DELTA_" + pullRequestInfo.targetBranch) === "true") {
            pullRequestInfo.customBehaviors.noDeltaDeployment = true;
        }
        if (desc.includes("PURGE_FLOW_VERSIONS")
            || getEnvVar("PURGE_FLOW_VERSIONS") === "true"
            || getEnvVar("PURGE_FLOW_VERSIONS_" + pullRequestInfo.targetBranch) === "true") {
            pullRequestInfo.customBehaviors.purgeFlowVersions = true;
        }
        if (desc.includes("DESTRUCTIVE_CHANGES_AFTER_DEPLOYMENT")
            || getEnvVar("DESTRUCTIVE_CHANGES_AFTER_DEPLOYMENT") === "true"
            || getEnvVar("DESTRUCTIVE_CHANGES_AFTER_DEPLOYMENT_" + pullRequestInfo.targetBranch) === "true") {
            pullRequestInfo.customBehaviors.destructiveChangesAfterDeployment = true;
        }
        return pullRequestInfo;
    }
}
//# sourceMappingURL=gitProviderRoot.js.map