"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkBranchNameAndAutoBranchMaybe = checkBranchNameAndAutoBranchMaybe;
exports.printBranchRunningInfo = printBranchRunningInfo;
exports.crossCheckBranchWithTest = crossCheckBranchWithTest;
exports.isValidMablBranchName = isValidMablBranchName;
const constants_1 = require("../../core/constants");
const loggingProvider_1 = require("../../providers/logging/loggingProvider");
const chalk = require('chalk');
async function checkBranchNameAndAutoBranchMaybe(workspaceId, apiClient, branchName, autoBranch) {
    if (branchName) {
        try {
            await apiClient.getBranchByName(workspaceId, branchName);
        }
        catch (error) {
            if (autoBranch) {
                await apiClient.createBranch(workspaceId, branchName);
            }
            else {
                loggingProvider_1.logger.error(chalk.red.bold(`mabl branch [${branchName}] does not exist.\n` +
                    'Please specify an existing branch or provide the [--auto-branch] argument to create a new branch'));
                process.exit(1);
            }
        }
    }
}
function printBranchRunningInfo(branchName) {
    loggingProvider_1.logger.info(chalk.yellow('Running test(s) on the', chalk.magenta(branchName), chalk.yellow('branch')));
}
function crossCheckBranchWithTest(branchName, test) {
    if (test.source_control_tags?.[branchName] === undefined &&
        test.source_control_tags?.[constants_1.DefaultBranchName] === undefined) {
        loggingProvider_1.logger.info(chalk.red('ERROR: Test does not exist on the branch:', chalk.magenta(branchName), 'Use the --mabl-branch flag to specify a branch'));
        process.exit(1);
    }
}
function isValidMablBranchName(branchName) {
    if (branchName) {
        return /^[A-Za-z0-9._/-]+$/.test(branchName.trim());
    }
    return false;
}
