'use strict';
import _0x589480 from 'process';
import { fetchQuiet, remoteBranchExists, checkoutRemoteBranch } from '../tools/git.js';
import { environmentVariables } from '../constants/ci-constants.js';
import { gitLogs } from '../constants/git-commands.js';
import { branches } from '../../../base-repo/app-constants/internal-system-constants.js';
import { cliLogError, cliLogInfo, cliLogWarning, cliLogSuccess, cliLogSubtitle } from '../../../fd-toolbox/logging/cli-logger.js';
import { getMergeRequestFromProject } from '../../git/gitlab/gitlab.js';
import { buildRelatedBranchWarning, buildBranchRepoMessage } from './resolve-main-repo-branch.js';
import { getStringEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';

var __defProp=Object['defineProperty'],__name=(_0x1edbf2,_0x4fe1c6)=>__defProp(_0x1edbf2,'name',{'value':_0x4fe1c6,'configurable':!![]});async function resolveAndCheckoutSubmoduleBranch(_0x25e098){(!_0x25e098['submodulePath']||!_0x25e098['variableName']||!_0x25e098['currentRefName'])&&(cliLogError('Error:\x20Missing\x20required\x20options\x20to\x20resolve\x20submodule\x20branch.\x20Provided\x20options:\x0a',_0x25e098),_0x589480['exit'](-0x139*-0x11+0x11*-0x4a+0x7ef*-0x2));const _0x468464=getStringEnvSetting(_0x25e098['variableName']),_0xcb960b=getStringEnvSetting(environmentVariables['ciMergeRequestSourceBranchName']);let _0x4ec88d='';if(_0x468464)_0x4ec88d=_0x468464;else _0xcb960b?_0x4ec88d=_0xcb960b:_0x4ec88d=_0x25e098['currentRefName'];let _0x304ed4='';if(_0x468464)cliLogInfo('Branch\x20is\x20already\x20defined\x20by\x20variable\x20'+_0x25e098['variableName']+':\x20'+_0x468464),_0x304ed4=_0x468464;else {cliLogInfo('Variable\x20not\x20set.\x20Checking\x20for\x20remote\x20branch:\x20'+_0x4ec88d),fetchQuiet(_0x25e098['submodulePath']);if(remoteBranchExists(_0x4ec88d,{'cwd':_0x25e098['submodulePath']})&&_0x4ec88d!==branches['main']){const _0x202385=await getMergeRequestFromProject(_0x4ec88d,_0x25e098['projectId']);!_0x202385?(cliLogWarning(buildRelatedBranchWarning(_0x4ec88d,_0x25e098['submodulePath'])),_0x304ed4=branches['main']):(cliLogSuccess('Found\x20matching\x20remote\x20branch:\x20'+_0x4ec88d),_0x304ed4=_0x4ec88d);}else cliLogInfo('No\x20matching\x20remote\x20branch\x20found\x20for\x20'+_0x4ec88d+'\x20or\x20current\x20branch\x20is\x20main.\x20Checking\x20out\x20'+branches['main']+'.'),_0x304ed4=branches['main'];}return checkoutSubmoduleBranch(_0x304ed4,_0x25e098['submodulePath']),{'variableName':_0x25e098['variableName'],'branchToCheckout':_0x304ed4};}__name(resolveAndCheckoutSubmoduleBranch,'resolveAndCheckoutSubmoduleBranch');function checkoutSubmoduleBranch(_0xedcf94,_0x4029f4){_0xedcf94?(cliLogSubtitle(''+gitLogs['checkingOutBranchPrefix']+buildBranchRepoMessage(_0xedcf94,_0x4029f4)+'...'),checkoutRemoteBranch(_0xedcf94,_0x4029f4)):cliLogInfo('No\x20branch\x20to\x20checkout\x20for\x20'+_0x4029f4+'.\x20Submodule\x20remains\x20on\x20default\x20commit.');}__name(checkoutSubmoduleBranch,'checkoutSubmoduleBranch');

export { checkoutSubmoduleBranch, resolveAndCheckoutSubmoduleBranch };
