'use strict';
import { buildGitHttpsUrl, remoteBranchExists } from '../tools/git.js';
import { environmentVariables, ciLogMessages } from '../constants/ci-constants.js';
import { branches } from '../../../base-repo/app-constants/internal-system-constants.js';
import { cliLogInfo, cliLogWarning, cliLogSuccess } from '../../../fd-toolbox/logging/cli-logger.js';
import { gitlabProjectIds } from '../../git/gitlab/gitlab-constants.js';
import { getMergeRequestFromProject } from '../../git/gitlab/gitlab.js';
import { gitlabGroup } from '../../../base-repo/constants/gitlab.js';
import { folders } from '../../../base-repo/app-constants/project-paths.js';
import { getStringEnvSetting, getNumberEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';

var __defProp=Object['defineProperty'],__name=(_0x32371b,_0x581d7a)=>__defProp(_0x32371b,'name',{'value':_0x581d7a,'configurable':!![]});async function resolveMainRepoBranch(){const _0x2c6a64=getStringEnvSetting(environmentVariables['ciBot']),_0x38c2c2=getStringEnvSetting(environmentVariables['ciBotToken']),_0x4843ac=environmentVariables['ciMainRepoBranchName'],_0x38854b=getStringEnvSetting(environmentVariables['ciMergeRequestSourceBranchName'])??getStringEnvSetting(environmentVariables['ciCommitRefName'])??'',_0x5dbc1b=branches['main'];let _0x3ad8bb;_0x2c6a64&&_0x38c2c2&&(_0x3ad8bb=buildGitHttpsUrl({'owner':gitlabGroup,'repo':folders['monorepo'],'gitUser':_0x2c6a64,'gitToken':_0x38c2c2}));cliLogInfo('Current\x20branch:\x20'+_0x38854b);let _0x2169d4=getStringEnvSetting(_0x4843ac);if(_0x2169d4)cliLogInfo('Branch\x20name\x20is\x20set\x20to\x20$'+_0x4843ac+'='+_0x2169d4);else {const _0x335a9b=getNumberEnvSetting(environmentVariables['ciProjectId']);let _0x3d9a0d=![];if(_0x335a9b!==void(0x6f8+0x1901+-0x665*0x5)&&_0x38854b){const _0x2e2d3b=await getMergeRequestFromProject(_0x38854b,_0x335a9b);_0x3d9a0d=!!_0x2e2d3b;}!_0x3d9a0d?(cliLogInfo(ciLogMessages['currentProjectNoMergeTogetherLabelPrefix']+'Defaulting\x20monorepo\x20branch\x20to\x20main.'),_0x2169d4=_0x5dbc1b):_0x2169d4=await getSelectedBranch(_0x38854b,_0x5dbc1b,_0x3ad8bb);}return _0x2169d4;}__name(resolveMainRepoBranch,'resolveMainRepoBranch');function buildRelatedBranchWarning(_0x1e946f,_0x53b33e){return 'Found\x20related\x20branch\x20\x22'+buildBranchRepoMessage(_0x1e946f,_0x53b33e)+ciLogMessages['didNotUseBranchMiddle']+ciLogMessages['noMergeTogetherLabel'];}__name(buildRelatedBranchWarning,'buildRelatedBranchWarning');function buildBranchRepoMessage(_0x107fea,_0x59c227){return ''+_0x107fea+ciLogMessages['quoteInRepoWithDoubleQuote']+_0x59c227;}__name(buildBranchRepoMessage,'buildBranchRepoMessage');async function getSelectedBranch(_0x4b103e,_0x44ca33,_0x4395e2){let _0x3793b3=_0x44ca33;if(_0x4b103e&&_0x4395e2){if(remoteBranchExists(_0x4b103e,{'repoUrl':_0x4395e2})){const _0x7cf2f8=await getMergeRequestFromProject(_0x4b103e,gitlabProjectIds['monorepo']);!_0x7cf2f8?cliLogWarning(buildRelatedBranchWarning(_0x4b103e,folders['monorepo'])):(cliLogSuccess('Found\x20matching\x20branch\x20\x27'+_0x4b103e+ciLogMessages['quoteInRepo']+folders['monorepo']+'\x27\x20repository.'),_0x3793b3=_0x4b103e);}else cliLogInfo('Branch\x20\x27'+_0x4b103e+'\x27\x20not\x20found\x20in\x20\x27'+folders['monorepo']+'\x27.\x20Using\x20default:\x20'+_0x44ca33+'.');}return _0x3793b3;}__name(getSelectedBranch,'getSelectedBranch');

export { buildBranchRepoMessage, buildRelatedBranchWarning, resolveMainRepoBranch };
