'use strict';
import { existsSync } from 'fs';
import { createEarlyInfraError } from '../tools/create-early-infra-error.js';
import { environmentVariables } from '../constants/ci-constants.js';
import { gitLogs, gitCommitMessages } from '../constants/git-commands.js';
import { branches } from '../../../base-repo/app-constants/internal-system-constants.js';
import { submodulesConfig } from '../submodules-config.js';
import { submoduleUpdate, configureUser, buildGitHttpsUrl, setRemoteUrl, fetchOrigin, forceCheckoutAndTrack, configSubmoduleUrl, checkoutBranch, pullMain, add, hasStagedChanges, commit, push } from '../tools/git.js';
import { cliLogSeparator, cliLogSubtitle } from '../../../fd-toolbox/logging/cli-logger.js';
import { gitlabGroup } from '../../../base-repo/constants/gitlab.js';
import { folders } from '../../../base-repo/app-constants/project-paths.js';
import { getStringEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';

var __defProp=Object['defineProperty'],__name=(_0x33c173,_0x4b7796)=>__defProp(_0x33c173,'name',{'value':_0x4b7796,'configurable':!![]});async function updateSubmodules(){const _0x3f2d4c=getStringEnvSetting(environmentVariables['ciCommitRefName']),_0x100384=getStringEnvSetting(environmentVariables['ciAccess']),_0x3b6000=getStringEnvSetting(environmentVariables['ciAccessToken']);if(!_0x3f2d4c||!_0x100384||!_0x3b6000){const _0x64f4b0=[!_0x3f2d4c&&environmentVariables['ciCommitRefName'],!_0x100384&&environmentVariables['ciAccess'],!_0x3b6000&&environmentVariables['ciAccessToken']]['filter'](Boolean)['join'](',\x20');throw createEarlyInfraError('Critical\x20error:\x20Missing\x20required\x20environment\x20variables:\x20'+_0x64f4b0);}const _0x44543a=configureGit(_0x100384,_0x3b6000,_0x3f2d4c);cliLogSeparator(),configureSubmoduleUrls(_0x100384,_0x3b6000),cliLogSeparator(),cliLogSubtitle(gitLogs['initializingAndUpdatingSubmodules']),submoduleUpdate(),cliLogSeparator(),resetSubmodulesAndCommitChanges(_0x3f2d4c,_0x44543a);}__name(updateSubmodules,'updateSubmodules');function configureGit(_0xeac66c,_0x3b6f54,_0x2e1a8e){cliLogSubtitle('Configuring\x20git\x20user...'),configureUser(),cliLogSeparator();const _0x20f020=buildGitHttpsUrl({'owner':gitlabGroup,'repo':folders['monorepo'],'gitUser':_0xeac66c,'gitToken':_0x3b6f54});return cliLogSubtitle('Setting\x20remote\x20URL\x20for\x20the\x20main\x20repository...'),setRemoteUrl(_0x20f020),cliLogSeparator(),cliLogSubtitle(gitLogs['fetchingLatestChanges']),fetchOrigin(),cliLogSeparator(),cliLogSubtitle(''+gitLogs['checkingOutThe']+_0x2e1a8e+gitLogs['branchDots']),forceCheckoutAndTrack(_0x2e1a8e),_0x20f020;}__name(configureGit,'configureGit');function configureSubmoduleUrls(_0x3cc609,_0x577e4e){for(const _0xf5e7cc of submodulesConfig){const _0x36d3f3=buildGitHttpsUrl({'owner':gitlabGroup,'repo':_0xf5e7cc['repoName'],'gitUser':_0x3cc609,'gitToken':_0x577e4e});cliLogSubtitle('Configuring\x20URLs\x20for\x20submodule:\x20'+_0xf5e7cc['repoName']),configSubmoduleUrl(_0xf5e7cc['path'],_0x36d3f3),setRemoteUrl(_0x36d3f3,_0xf5e7cc['path']);}}__name(configureSubmoduleUrls,'configureSubmoduleUrls');function resetSubmoduleToMain(_0x1e6ffd){if(!existsSync(_0x1e6ffd)){cliLogSubtitle('Submodule\x20at\x20'+_0x1e6ffd+'\x20not\x20found,\x20skipping');return;}cliLogSubtitle(''+gitLogs['checkingOutBranchPrefix']+branches['main']+'\x22\x20in\x20submodule\x20\x27'+_0x1e6ffd+'\x27...'),checkoutBranch(branches['main'],_0x1e6ffd),cliLogSeparator(),cliLogSubtitle('Pulling\x20latest\x20changes\x20from\x20'+branches['originMain']+'\x20for\x20\x27'+_0x1e6ffd+'\x27...'),pullMain(_0x1e6ffd);}__name(resetSubmoduleToMain,'resetSubmoduleToMain');function resetSubmodulesAndCommitChanges(_0x1d8721,_0x188f7c){const _0x72c4e3=submodulesConfig['filter'](_0x4c2cef=>_0x4c2cef['autoUpdate']);_0x72c4e3['forEach'](_0x5a4438=>resetSubmoduleToMain(_0x5a4438['path'])),cliLogSeparator(),cliLogSubtitle('Staging\x20updated\x20submodules...');const _0x5acbd3=_0x72c4e3['map'](_0x3be11d=>_0x3be11d['path']);add(_0x5acbd3),hasStagedChanges()?(cliLogSeparator(),cliLogSubtitle(gitLogs['changesDetected']),commit(gitCommitMessages['updateSubmodules'],{'noVerify':!![]}),push(_0x1d8721,{'remoteTarget':_0x188f7c})):(cliLogSeparator(),cliLogSubtitle(gitLogs['noChanges']));}__name(resetSubmodulesAndCommitChanges,'resetSubmodulesAndCommitChanges');

export { updateSubmodules };
