'use strict';
import _0x589480 from 'process';
import { existsSync } from 'node:fs';
import { rm } from 'node:fs/promises';
import { folders, files, packagesPath } from '../../../base-repo/app-constants/project-paths.js';
import { ciArtifacts, gitlabGroup } from '../../../base-repo/constants/gitlab.js';
import { ciLogMessages, environmentVariables } from '../constants/ci-constants.js';
import { submoduleRepoNames } from '../constants/paths.js';
import { gitLogs, gitCommands } from '../constants/git-commands.js';
import { branches } from '../../../base-repo/app-constants/internal-system-constants.js';
import { npmCommands } from '../constants/npm-commands.js';
import { verifyPublishedVersion } from '../setup/verify-published-version.js';
import { updateWorkspaceDependencies } from '../setup/update-workspace-dependencies.js';
import { execSyncCommand } from '../tools/exec.js';
import { buildGitHttpsUrl, configureUser, setRemoteUrl, configureCiToken, fetchOrigin, forceCheckoutAndTrack, configSubmoduleUrl, submoduleUpdate, hasStagedChanges, commit, push, add, clean } from '../tools/git.js';
import { syncSaasKitVersionToMainCommitMessage } from '../tools/git-command-funcs.js';
import { cliLogSubtitle, cliLogSeparator, cliLogInfo, cliLogWarning } from '../../../fd-toolbox/logging/cli-logger.js';
import { submodulesConfig } from '../submodules-config.js';
import { getStringEnvSetting, getBooleanEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';
import { gitRemotes } from '../../../base-repo/constants/tools-constants.js';

var __defProp=Object['defineProperty'],__name=(_0xb825e4,_0x37bc03)=>__defProp(_0xb825e4,'name',{'value':_0x37bc03,'configurable':!![]});async function syncSaasKitVersionToMain(){cliLogSubtitle('Step\x201:\x20Activating\x20corepack\x20to\x20enable\x20pnpm...'),preparePrerequisites(),cliLogSeparator(),cliLogSubtitle('Step\x202:\x20Verify\x20published\x20version\x20against\x20artifact...');const _0x384c09=await verifyPublishedVersion(ciArtifacts['ciSaasKitPublishVersionFile']);cliLogSeparator(),cliLogSubtitle('Step\x203:\x20Configuring\x20Git\x20Settings...');const _0x2b3f7b=configureGitSettings();cliLogSeparator(),cliLogSubtitle('Step\x204:\x20Updating\x20workspace\x20dependencies\x20to\x20version\x20'+_0x384c09['expectedVersion']+'...'),await updateWorkspaceDependencies(_0x384c09),cliLogSeparator(),cliLogSubtitle('Step\x205:\x20Cleaning\x20up\x20temporary\x20artifacts...'),await cleanupArtifacts(),cliLogSeparator(),cliLogSubtitle('Step\x206:\x20Commit\x20and\x20push\x20changes\x20to\x20the\x20main\x20branch...'),cliLogSeparator(),commitAndPushChanges(_0x384c09['expectedVersion'],_0x2b3f7b);}__name(syncSaasKitVersionToMain,'syncSaasKitVersionToMain');function logDryRun(_0x5d7ec2,_0x53200e){cliLogInfo(''+ciLogMessages['dryRun']+environmentVariables['ciPublishAndTestPackageDryRun']+ciLogMessages['isActive']),_0x53200e&&cliLogInfo(ciLogMessages['dryRunGeneratedCommitMessage']+'\x20\x22'+_0x53200e+'\x22'),cliLogInfo(_0x5d7ec2);}__name(logDryRun,'logDryRun');function preparePrerequisites(){execSyncCommand(npmCommands['corepackEnable']),execSyncCommand(npmCommands['corepackPreparePnpm']);}__name(preparePrerequisites,'preparePrerequisites');function getAuthenticatedUrl(_0x8ed51c){const _0x131130=getStringEnvSetting(environmentVariables['ciAccess']),_0x58f065=getStringEnvSetting(environmentVariables['ciAccessToken']);if(!_0x131130||!_0x58f065)return void(0x1c93+0x16*-0x9d+0x1b*-0x8f);return buildGitHttpsUrl({'owner':gitlabGroup,'repo':_0x8ed51c,'gitUser':_0x131130,'gitToken':_0x58f065});}__name(getAuthenticatedUrl,'getAuthenticatedUrl');function configureGitSettings(){configureUser();const _0x1c0eea=getAuthenticatedUrl(folders['monorepo']);return _0x1c0eea?(cliLogInfo('Setting\x20authenticated\x20remote\x20URL\x20for\x20\x27'+folders['monorepo']+'\x27...'),setRemoteUrl(_0x1c0eea)):(cliLogWarning('Credentials\x20for\x20Git\x20access\x20are\x20missing.\x20Using\x20fallback\x20token\x20configuration.'),configureCiToken()),cliLogSeparator(),cliLogSubtitle(gitLogs['fetchingLatestChanges']),fetchOrigin(),cliLogSeparator(),cliLogSubtitle(''+gitLogs['checkingOutThe']+branches['main']+gitLogs['branchDots']),forceCheckoutAndTrack(branches['main']),cliLogSeparator(),cliLogSubtitle('Configuring\x20and\x20updating\x20submodules...'),configureSubmodules(),_0x1c0eea??gitRemotes['origin'];}__name(configureGitSettings,'configureGitSettings');function configureSubmodules(){const _0x32c2e5=_0x589480['cwd']();cliLogSeparator(),cliLogSubtitle('Initializing\x20and\x20cloning\x20submodules...');for(const _0x5f0e61 of submodulesConfig){const _0x179e81=getAuthenticatedUrl(_0x5f0e61['repoName']);_0x179e81&&configSubmoduleUrl(_0x5f0e61['path'],_0x179e81);}submoduleUpdate(),cliLogSeparator();for(const _0x26a5e0 of submodulesConfig){cliLogSubtitle('Setting\x20up\x20submodule:\x20'+_0x26a5e0['repoName']);if(existsSync(_0x26a5e0['path'])){_0x589480['chdir'](_0x26a5e0['path']);const _0x5e1e71=getAuthenticatedUrl(_0x26a5e0['repoName']);_0x5e1e71&&setRemoteUrl(_0x5e1e71),cliLogSeparator(),cliLogSubtitle(gitLogs['fetchingLatestChanges']),fetchOrigin(),cliLogSeparator(),cliLogSubtitle(''+gitLogs['checkingOutThe']+branches['main']+'\x27\x20branch\x20in\x20submodule...'),forceCheckoutAndTrack(branches['main']),_0x589480['chdir'](_0x32c2e5);}else cliLogWarning('Submodule\x20path\x20\x27'+_0x26a5e0['path']+'\x27\x20not\x20found.\x20Skipping\x20setup.');}}__name(configureSubmodules,'configureSubmodules');async function cleanupArtifacts(){const _0x3ac102=ciArtifacts['ciSaasKitPublishVersionFile'];cliLogInfo('Cleaning\x20up\x20artifact\x20file:\x20\x27'+_0x3ac102+'\x27.'),await rm(_0x3ac102,{'force':!![]}),cliLogInfo('Artifact\x20cleanup\x20complete.');}__name(cleanupArtifacts,'cleanupArtifacts');function commitAndPushChanges(_0x40181a,_0x1e12f4){const _0x3c23d5=[files['packageJson'],packagesPath['testPackageJsonPath'],files['pnpmLock']];cliLogSubtitle('Formatting\x20modified\x20files\x20with\x20Prettier...');const _0x3a25a1=[packagesPath['testPackageJsonPath'],files['pnpmLock']]['join']('\x20');execSyncCommand(npmCommands['prettierWrite']+'\x20'+_0x3a25a1),cliLogSeparator(),cliLogInfo('Staging\x20files\x20for\x20commit:'),_0x3c23d5['forEach'](cliLogInfo),prepareAndValidateChanges(_0x3c23d5),cliLogSubtitle('Commit\x20and\x20push\x20changes\x20to\x20'+folders['monorepo']+'.');if(hasStagedChanges()){const _0xe080b3=syncSaasKitVersionToMainCommitMessage(_0x40181a);commit(_0xe080b3,{'noVerify':!![]});const _0xea3f32=getBooleanEnvSetting(environmentVariables['ciPublishAndTestPackageDryRun']);if(_0xea3f32){const _0x4b793='[DRY\x20RUN]\x20Skipping\x20\x27git\x20push\x27\x20to\x20the\x20main\x20repository.';logDryRun(_0x4b793,_0xe080b3);}else push(branches['main'],{'remoteTarget':_0x1e12f4});}else cliLogSeparator(),cliLogSubtitle('No\x20changes\x20detected\x20to\x20commit.');commitChangesInSubmodule(packagesPath['packagesRanksters'],submoduleRepoNames['ranksters'],_0x40181a);}__name(commitAndPushChanges,'commitAndPushChanges');function commitChangesInSubmodule(_0x6e3dc0,_0x253bc2,_0x480f8e){if(!existsSync(_0x6e3dc0)){cliLogWarning('Submodule\x20directory\x20\x27'+_0x6e3dc0+'\x27\x20not\x20found.\x20Skipping\x20commit.');return;}const _0x40a90c=_0x589480['cwd']();_0x589480['chdir'](_0x6e3dc0),cliLogSubtitle('Changed\x20to\x20submodule\x20directory:\x20'+_0x6e3dc0),prepareAndValidateChanges([files['packageJson'],files['pnpmLock']]);if(hasStagedChanges()){cliLogSubtitle('Committing\x20changes\x20in\x20'+_0x253bc2+'\x20submodule...');const _0x3dcb20=syncSaasKitVersionToMainCommitMessage(_0x480f8e);commit(_0x3dcb20,{'noVerify':!![]});const _0x7d83b2=getBooleanEnvSetting(environmentVariables['ciPublishAndTestPackageDryRun']);if(_0x7d83b2){const _0x59f2bb='[DRY\x20RUN]\x20Skipping\x20\x27git\x20push\x27\x20for\x20the\x20\x27'+_0x253bc2+'\x27\x20submodule.';logDryRun(_0x59f2bb,_0x3dcb20);}else {const _0x548de4=getAuthenticatedUrl(_0x253bc2);push(branches['main'],{'remoteTarget':_0x548de4??gitRemotes['origin']});}}else cliLogSubtitle('No\x20changes\x20detected\x20in\x20'+_0x253bc2);_0x589480['chdir'](_0x40a90c),cliLogSeparator();}__name(commitChangesInSubmodule,'commitChangesInSubmodule');function prepareAndValidateChanges(_0x1a06b9){add(_0x1a06b9),cliLogSeparator(),cliLogSubtitle('Cleaning\x20the\x20working\x20directory\x20of\x20all\x20untracked\x20files...'),clean(),cliLogSeparator(),cliLogSubtitle('Checking\x20git\x20status\x20for\x20modified\x20files...'),execSyncCommand(gitCommands['statusPorcelain']),cliLogSeparator();}__name(prepareAndValidateChanges,'prepareAndValidateChanges');

export { logDryRun, syncSaasKitVersionToMain };
