"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScmContextProviderV2 = void 0;
const scmContextInterfaces_1 = require("./scmContextInterfaces");
const loggingProvider_1 = require("./logging/loggingProvider");
function processBuildEnv(envVariables, mapper, confidenceCheck) {
    const results = {};
    envVariables.forEach((value, key) => {
        mapper(key, value, results);
    });
    results.isCorrectType = confidenceCheck(results);
    if (results.isCorrectType) {
        return results;
    }
    return;
}
class ScmContextProviderV2 {
    constructor(processRef) {
        this.envVariables = this.extractEnvVariables(processRef);
    }
    extractScmAndBuildInfo() {
        try {
            return this.innerExtractScmAndBuildInfo();
        }
        catch (error) {
            loggingProvider_1.logger.error('Unable to collect build information. Using defaults.');
            return;
        }
    }
    innerExtractScmAndBuildInfo() {
        const maybeBuildInfo = this.attemptExtractBuildInfo();
        return {
            buildInfo: maybeBuildInfo,
        };
    }
    attemptExtractBuildInfo() {
        const processors = [
            () => this.getForCloudBuild(),
        ];
        return processors
            .map((processor) => processor())
            .find((result) => result !== undefined);
    }
    extractEnvVariables(processRef) {
        const result = new Map();
        Object.entries(processRef).forEach(([key, value]) => {
            if (value && value !== '') {
                result.set(key, value);
            }
        });
        return result;
    }
    getForCloudBuild() {
        function mapper(key, value, result) {
            switch (key) {
                case '_HEAD_BRANCH':
                    result.pullRequestHeadBranch = value;
                    break;
                case '_BASE_BRANCH':
                    result.pullRequestBaseBranch = value;
                    break;
                case '_HEAD_REPO_URL':
                    result.pullRequestHeadRepo = value;
                    break;
                case '_PR_NUMBER':
                    result.pullRequestId = value;
                    break;
                case 'PROJECT_ID':
                    result.projectId = value;
                    break;
                case 'PROJECT_NUMBER':
                    result.projectNumber = value;
                    break;
                case 'SERVICE_ACCOUNT':
                case 'SERVICE_ACCOUNT_EMAIL':
                    result.hasServiceAccount = true;
                    break;
                case 'LOCATION':
                    result.cloudRegion = value;
                    break;
                case 'BUILD_ID':
                    result.buildId = value;
                    break;
                case 'COMMIT_SHA':
                    result.commitSha = value;
                    break;
                case 'SHORT_SHA':
                    result.shortSha = value;
                    break;
                case 'REPO_NAME':
                    result.repoName = value;
                    break;
                case 'BRANCH_NAME':
                    result.branchName = value;
                    break;
                case 'TAG_NAME':
                    result.tagName = value;
                    break;
                case 'REVISION_ID':
                    result.revisionId = value;
                    break;
                case 'MABL_CLI_NAME':
                    result.isCliDockerContainer = true;
                    break;
                default:
            }
        }
        function confidenceCheck(result) {
            const checksOut = result?.projectId !== undefined &&
                result.projectNumber !== undefined &&
                result.cloudRegion !== undefined &&
                result.buildId !== undefined;
            if (checksOut) {
                result.buildType = scmContextInterfaces_1.BuildTypes.CloudBuild;
                result.buildInfoUrl = `https://console.cloud.google.com/cloud-build/builds;region=${result.cloudRegion}/${result.buildId}?project=${result.projectId}`;
            }
            return checksOut;
        }
        return processBuildEnv(this.envVariables, mapper, confidenceCheck);
    }
}
exports.ScmContextProviderV2 = ScmContextProviderV2;
