'use strict';
import _0x589480 from 'process';
import { ciArtifacts } from '../../../base-repo/constants/gitlab.js';
import { folders } from '../../../base-repo/app-constants/project-paths.js';
import { branches } from '../../../base-repo/app-constants/internal-system-constants.js';
import { npmCommands } from '../constants/npm-commands.js';
import { ciLogMessages, huskyDisabledValue, environmentVariables, ciContexts, ciJobs } from '../constants/ci-constants.js';
import { booleanStringValues } from '../../../base-repo/constants/type-string-values.js';
import { gitRestoreDependencyFiles } from '../constants/job-constants.js';
import { resolveMainRepoBranch } from './resolve-main-repo-branch.js';
import { setupSubmodules } from './setup-submodules.js';
import { verifyPublishedVersion } from './verify-published-version.js';
import { updateWorkspaceDependencies } from './update-workspace-dependencies.js';
import { configureUser, configureCiToken, fetchQuiet, mergeOriginMain, fetchOrigin, remoteBranchExists, forceCheckoutAndTrack } from '../tools/git.js';
import { execSyncCommand } from '../tools/exec.js';
import { cliLogSubtitle, cliLogError, cliLogSeparator, cliLogInfo } from '../../../fd-toolbox/logging/cli-logger.js';
import { checkoutAndSyncSubmodules } from './checkout-and-sync-submodules.js';
import { getStringEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';

var __defProp=Object['defineProperty'],__name=(_0x2c75b2,_0x370c73)=>__defProp(_0x2c75b2,'name',{'value':_0x370c73,'configurable':!![]});const constants={'commonSetupStep':'[2]\x20Run\x20common\x20setup'};async function prepareEnvironment(_0x35f105){try{cliLogSubtitle(ciLogMessages['configuringGitSettings']),_0x589480['env'][environmentVariables['fdHusky']]=huskyDisabledValue,configureUser(),configureCiToken();if(!_0x35f105){const _0x502812='Critical\x20error:\x20CI\x20context\x20(\x27'+ciContexts['mainRepo']+ciLogMessages['quotedOrQuoted']+ciContexts['submodule']+'\x27)\x20is\x20required.';cliLogError(_0x502812),_0x589480['exit'](0x262+0x5*0x168+-0x969);}if(_0x35f105===ciContexts['mainRepo'])await prepareForMainRepoContext();else _0x35f105===ciContexts['submodule']?await prepareForSubmoduleContext():(cliLogError('Unknown\x20context\x20provided:\x20'+_0x35f105),_0x589480['exit'](-0x1*-0x1ae5+-0x1*-0x1c35+0x7*-0x7df));}catch(_0x5ac76f){cliLogError('Critical\x20error\x20during\x20environment\x20setup:\x0a',_0x5ac76f),_0x589480['exit'](-0x1*0x7c1+0x708+0x6*0x1f);}}__name(prepareEnvironment,'prepareEnvironment');function buildMergeMessage(_0x2dd90c,_0x1d62c9,_0x2c51c8,_0x522e39){return '['+_0x2dd90c+']\x20Merging\x20\x27'+_0x1d62c9+'\x27\x20into\x20branch\x20\x27'+_0x522e39+ciLogMessages['quoteInRepo']+_0x2c51c8+'\x27...';}__name(buildMergeMessage,'buildMergeMessage');async function prepareForMainRepoContext(){const _0x501eb9=getStringEnvSetting(environmentVariables['ciCommitRefName']);cliLogSeparator();const _0x57ea86=buildMergeMessage('1.2',branches['main'],folders['monorepo'],_0x501eb9);cliLogSubtitle(_0x57ea86),checkoutBranchForMerge(folders['monorepo'],_0x501eb9),execSyncCommand(gitRestoreDependencyFiles),fetchQuiet(_0x589480['cwd']()),mergeOriginMain(_0x589480['cwd']()),cliLogSeparator(),cliLogSubtitle(constants['commonSetupStep']),cliLogSeparator(),await completeEnvironmentSetup();}__name(prepareForMainRepoContext,'prepareForMainRepoContext');async function prepareForSubmoduleContext(){const _0x44dde2=await resolveMainRepoBranch();cliLogSeparator();const _0x2bf7fa=buildMergeMessage('1.3',branches['main'],folders['monorepo'],_0x44dde2);cliLogSubtitle(_0x2bf7fa),checkoutBranchForMerge(folders['monorepo'],_0x44dde2),execSyncCommand(gitRestoreDependencyFiles),fetchOrigin(_0x589480['cwd']()),mergeOriginMain(_0x589480['cwd']()),cliLogSeparator(),cliLogSubtitle(constants['commonSetupStep']),cliLogSeparator(),await completeEnvironmentSetup();}__name(prepareForSubmoduleContext,'prepareForSubmoduleContext');async function completeEnvironmentSetup(){setupSubmodules();const _0xd5288d=getStringEnvSetting(environmentVariables['ciCommitRefName'])??'';await checkoutAndSyncSubmodules(_0xd5288d),cliLogSeparator(),cliLogSubtitle('[2.4]\x20Installing\x20dependencies...'),execSyncCommand(npmCommands['pnpmInstallFrozen']),cliLogSeparator(),cliLogSubtitle('[2.5]\x20Checking\x20for\x20post-publish\x20sync');const _0x4880a9=_0x589480['argv'][0x9*-0xf2+0x15*-0x14e+0x5*0x72f]??'',_0x33e1e5=[ciJobs['buildPackage'],ciJobs['buildAndTest'],ciJobs['saasKitTestBuildAndTest']];if(getStringEnvSetting(ciJobs['publishAndTestPackage'])===booleanStringValues['true']&&_0x33e1e5['includes'](_0x4880a9)){cliLogInfo('\x22'+ciJobs['publishAndTestPackage']+'\x22\x20is\x20active\x20and\x20current\x20job\x20is\x20\x22'+_0x4880a9+'\x22.\x20Attempting\x20to\x20sync...'),cliLogSeparator(),cliLogSubtitle('[2.5.1]\x20Reading\x20expected\x20version\x20from\x20artifact:\x20\x27'+ciArtifacts['ciSaasKitPublishVersionFile']+'\x27...');const _0x5a1da0=await verifyPublishedVersion(ciArtifacts['ciSaasKitPublishVersionFile']);cliLogSeparator(),cliLogSubtitle('[2.5.2]\x20Syncing\x20workspace\x20dependencies\x20to\x20published\x20version'),await updateWorkspaceDependencies(_0x5a1da0);}else cliLogInfo('Skipping\x20version\x20sync\x20for\x20job\x20\x27'+_0x4880a9+'\x27.');cliLogSeparator(),cliLogSubtitle('[2.6]\x20Generating\x20meta\x20and\x20schema...'),execSyncCommand(npmCommands['genMeta']),execSyncCommand(npmCommands['genSchema']);}__name(completeEnvironmentSetup,'completeEnvironmentSetup');function checkoutBranchForMerge(_0xf2dbba,_0x59d223){!_0x59d223&&(cliLogError('Target\x20branch\x20is\x20not\x20defined\x20for\x20repository\x20\x27'+_0xf2dbba+'\x27.'),_0x589480['exit'](-0x1*-0x1963+0x198f+-0x32f1));cliLogSubtitle('Checking\x20out\x20branch\x20\x27'+_0x59d223+'\x27\x20in\x20\x27'+_0xf2dbba+'\x27\x20before\x20merging\x20\x27'+branches['main']+'\x27.'),fetchOrigin(_0x589480['cwd']());const _0x347a68=remoteBranchExists(_0x59d223,{'cwd':_0x589480['cwd']()});!_0x347a68&&(cliLogError('Cannot\x20merge\x20\x27'+branches['main']+'\x27\x20into\x20\x27'+_0x59d223+'\x27\x20because\x20the\x20branch\x20was\x20not\x20found\x20on\x20origin\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20in\x20\x27'+_0xf2dbba+'\x27.'),_0x589480['exit'](0x7*-0x377+-0xba0+0x23e2)),forceCheckoutAndTrack(_0x59d223),cliLogSubtitle('Checked\x20out\x20\x27'+_0x59d223+'\x27\x20in\x20\x27'+_0xf2dbba+'\x27.');}__name(checkoutBranchForMerge,'checkoutBranchForMerge');

export { prepareEnvironment };
