'use strict';
import _0x589480 from 'process';
import { fetchQuiet, mergeOriginMain } from '../tools/git.js';
import { branches } from '../../../base-repo/app-constants/internal-system-constants.js';
import { submodulesConfig } from '../submodules-config.js';
import { cliLogSeparator, cliLogSubtitle } from '../../../fd-toolbox/logging/cli-logger.js';
import { checkoutSubmoduleBranch, resolveAndCheckoutSubmoduleBranch } from './resolve-and-checkout-submodule-branch.js';
import { environmentVariables, ciLogMessages } from '../constants/ci-constants.js';
import { gitlabProjectIds } from '../../git/gitlab/gitlab-constants.js';
import { getMergeRequestFromProject } from '../../git/gitlab/gitlab.js';
import { getNumberEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';

var __defProp=Object['defineProperty'],__name=(_0xf6b699,_0x3573f3)=>__defProp(_0xf6b699,'name',{'value':_0x3573f3,'configurable':!![]});const constants={'stepPrefix':'[2.3.','processingSubmoduleBranchFor':']\x20Processing\x20submodule\x20branch\x20for:\x20','syncingRepositoryAt':'Syncing\x20repository\x20at\x20\x27','quoteWith':'\x27\x20with\x20','asPerConfiguration':'\x20as\x20per\x20configuration.'};async function checkoutAndSyncSubmodules(_0x1c25ba){const _0x548301=getNumberEnvSetting(environmentVariables['ciProjectId']),_0x10996c=_0x548301===gitlabProjectIds['monorepo'];let _0x458953=![];if(_0x548301!==void(-0x4*-0x691+-0x18f2+-0x152)){const _0x563bbe=await getMergeRequestFromProject(_0x1c25ba,_0x548301);_0x458953=!!_0x563bbe;}if(_0x10996c)await syncSubmodulesFromMainRepo(_0x1c25ba,submodulesConfig,_0x458953);else {const _0x4f6148=submodulesConfig['findIndex'](_0x5dfada=>_0x5dfada['projectId']===_0x548301);if(_0x4f6148!==-(-0x996*-0x1+-0x5d*0x5f+0x18ee)){const _0x224e40=submodulesConfig[_0x4f6148];cliLogSeparator(),cliLogSubtitle(''+constants['stepPrefix']+(_0x4f6148+(-0x1cd7+0x17c+0x1b5c))+constants['processingSubmoduleBranchFor']+_0x224e40['path']+'...'),cliLogSubtitle('Current\x20project\x20is\x20'+_0x224e40['repoName']+'.\x20Attempting\x20to\x20checkout\x20branch:\x20'+_0x1c25ba+'\x20in\x20submodule:\x20'+_0x224e40['path']),checkoutSubmoduleBranch(_0x1c25ba,_0x224e40['path']);_0x224e40['syncWithMain']&&(cliLogSubtitle(''+constants['syncingRepositoryAt']+_0x224e40['path']+constants['quoteWith']+branches['main']+'...'),fetchQuiet(_0x224e40['path']),mergeOriginMain(_0x224e40['path']));const _0x531225=submodulesConfig['filter']((_0x40f2e5,_0x1ebef3)=>_0x1ebef3!==_0x4f6148);await syncSubmodulesFromMainRepo(_0x1c25ba,_0x531225,_0x458953);}}}__name(checkoutAndSyncSubmodules,'checkoutAndSyncSubmodules');async function syncSubmodulesFromMainRepo(_0x289b77,_0x4c475d,_0x53e9b2){for(const [_0x14adfe,_0x342a76]of _0x4c475d['entries']()){const _0x22ef81=_0x14adfe+(-0x7c3*-0x1+0xc32+-0x9fa*0x2);cliLogSeparator(),cliLogSubtitle(''+constants['stepPrefix']+_0x22ef81+constants['processingSubmoduleBranchFor']+_0x342a76['path']+'...');if(!_0x53e9b2)cliLogSubtitle(ciLogMessages['currentProjectNoMergeTogetherLabelPrefix']+('Branch\x20checkout\x20skipped\x20for\x20'+_0x342a76['repoName']+'.'));else {const _0x8d60f4=await getMergeRequestFromProject(_0x289b77,_0x342a76['projectId']);if(!_0x8d60f4)cliLogSubtitle('No\x20Merge\x20Request\x20found\x20for\x20\x27'+_0x289b77+'\x27\x20in\x20'+_0x342a76['repoName']);else {if(_0x342a76['checkoutSourceBranch']){const _0x12eb9b=await resolveAndCheckoutSubmoduleBranch({'submodulePath':_0x342a76['path'],'variableName':_0x342a76['branchEnvVar'],'currentRefName':_0x289b77,'projectId':_0x342a76['projectId']});_0x589480['env'][_0x12eb9b['variableName']]=_0x12eb9b['branchToCheckout'],cliLogSubtitle('Loaded\x20env\x20var:\x20'+_0x12eb9b['variableName']+'='+_0x12eb9b['branchToCheckout']);}else cliLogSubtitle('Branch\x20checkout\x20skipped\x20for\x20'+_0x342a76['path']+constants['asPerConfiguration']);}}_0x342a76['syncWithMain']?(cliLogSubtitle(''+constants['syncingRepositoryAt']+_0x342a76['path']+constants['quoteWith']+branches['main']+'...'),fetchQuiet(_0x342a76['path']),mergeOriginMain(_0x342a76['path'])):cliLogSubtitle('Sync\x20with\x20main\x20skipped\x20for\x20'+_0x342a76['path']+constants['asPerConfiguration']);}}__name(syncSubmodulesFromMainRepo,'syncSubmodulesFromMainRepo');

export { checkoutAndSyncSubmodules };
