'use strict';
import _0x1869d0__default from 'path';
import _0x589480 from 'process';
import { execSyncCommand, execOutput } from './exec.js';
import { gitCommands } from '../constants/git-commands.js';
import { environmentVariables } from '../constants/ci-constants.js';
import { cliLogInfo, cliLogError } from '../../../fd-toolbox/logging/cli-logger.js';
import { gitlabConstants } from '../constants/gitlab-constants.js';
import { folders } from '../../../base-repo/app-constants/project-paths.js';
import { gitRemotes, gitFlags, stdioStatuses } from '../../../base-repo/constants/tools-constants.js';
import { systemCommands } from '../constants/npm-commands.js';
import { jsBasicTypes } from '../../../base-repo/constants/basic-types.js';
import { gitLabHost } from '../../../base-repo/app-constants/internal-system-constants.js';
import { getStringEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';

var __defProp=Object['defineProperty'],__name=(_0x325574,_0x3c7ef4)=>__defProp(_0x325574,'name',{'value':_0x3c7ef4,'configurable':!![]});const gitClone='git\x20clone\x20-b',gitConfigGlobal='git\x20config\x20--global';function fetchQuiet(_0x41ecba){execSyncCommand(gitCommands['fetchOriginQuiet'],{'cwd':_0x41ecba});}__name(fetchQuiet,'fetchQuiet');function fetchOrigin(_0x438648){execSyncCommand(gitCommands['fetchOrigin'],{'cwd':_0x438648});}__name(fetchOrigin,'fetchOrigin');function remoteBranchExists(_0x3c43a6,_0x4d9643){if(!_0x3c43a6)return ![];const _0x4f3cdb=_0x4d9643['repoUrl']??gitRemotes['origin'],_0x57e8c7='git\x20ls-remote\x20--exit-code\x20'+_0x4f3cdb+'\x20\x22'+_0x3c43a6+'\x22';try{return cliLogInfo('Executing\x20check:\x20'+_0x57e8c7),execSyncCommand(_0x57e8c7,{'cwd':_0x4d9643['cwd']}),!![];}catch(_0x5acecf){cliLogInfo('Failed\x20to\x20find\x20remote\x20branch.\x20Underlying\x20git\x20error:');let _0x17cc4d;if(_0x5acecf instanceof Error){const _0x29ba20=systemCommands['stderr']in _0x5acecf?_0x5acecf['stderr']:void(-0x1ac4+0x3b*-0x98+0xe2*0x46);_0x17cc4d=_0x29ba20&&typeof _0x29ba20===jsBasicTypes['object']?_0x29ba20['toString']():_0x5acecf['message'];}else _0x17cc4d=String(_0x5acecf);return cliLogError(_0x17cc4d),![];}}__name(remoteBranchExists,'remoteBranchExists');function checkoutRemoteBranch(_0x70e67c,_0xb20d72){const _0xf39518=_0x1869d0__default['join'](_0xb20d72,folders['dotGit']);execSyncCommand('git\x20--git-dir='+_0xf39518+'\x20--work-tree='+_0xb20d72+'\x20checkout\x20\x22'+gitRemotes['origin']+'/'+_0x70e67c+'\x22');}__name(checkoutRemoteBranch,'checkoutRemoteBranch');function checkoutAll(){execSyncCommand(gitCommands['checkoutAll']);}__name(checkoutAll,'checkoutAll');function resetHead(_0x4dbadf,_0x518bf2){execSyncCommand(''+gitCommands['resetHead']+(_0x518bf2??''),{'cwd':_0x4dbadf});}__name(resetHead,'resetHead');function checkoutBranch(_0x35e006,_0x502fc0){execSyncCommand(gitCommands['checkoutBranch']+'\x20'+_0x35e006,{'cwd':_0x502fc0});}__name(checkoutBranch,'checkoutBranch');function forceCheckoutAndTrack(_0x51ee21,_0x706de5){execSyncCommand(gitCommands['checkoutBranch']+'\x20'+gitFlags['force']+'\x20-B\x20'+_0x51ee21+'\x20'+gitRemotes['origin']+'/'+_0x51ee21,{'cwd':_0x706de5});}__name(forceCheckoutAndTrack,'forceCheckoutAndTrack');function mergeOriginMain(_0x252b83){execSyncCommand(gitCommands['mergeOriginMain'],{'cwd':_0x252b83});}__name(mergeOriginMain,'mergeOriginMain');function pullMain(_0x15e1c1){execSyncCommand(gitCommands['pullMain'],{'cwd':_0x15e1c1});}__name(pullMain,'pullMain');function cloneRepo(_0x564c9e,_0x3bdd69,_0x151ae7){execSyncCommand(gitClone+'\x20'+_0x3bdd69+'\x20'+_0x564c9e+'\x20'+_0x151ae7);}__name(cloneRepo,'cloneRepo');function configGlobal(_0x4853d6,_0x1e69fb){execSyncCommand(gitConfigGlobal+'\x20'+_0x4853d6+'\x20\x22'+_0x1e69fb+'\x22');}__name(configGlobal,'configGlobal');function submoduleUpdate(){execSyncCommand(gitCommands['updateSubmodules']);}__name(submoduleUpdate,'submoduleUpdate');function cloneRepository(_0x480044,_0x242954,_0x3db6df){const _0x5d24cd=getStringEnvSetting(environmentVariables['ciBot']),_0x1feb08=getStringEnvSetting(environmentVariables['ciBotToken']);if(_0x5d24cd&&_0x1feb08){const _0x24ebc4=''+_0x480044+folders['dotGit'],_0xf357fb=new URL(_0x24ebc4,gitLabHost);_0xf357fb['username']=_0x5d24cd,_0xf357fb['password']=_0x1feb08,execSyncCommand(gitClone+'\x20'+_0x3db6df+'\x20'+_0xf357fb['toString']()+'\x20'+_0x242954);}}__name(cloneRepository,'cloneRepository');function configureCiToken(){const _0x47738f=getStringEnvSetting(environmentVariables['ciJobToken']);!_0x47738f&&(cliLogError('Missing\x20required\x20environment\x20variable:\x20'+environmentVariables['ciJobToken']),_0x589480['exit'](0x1*-0x178+0x18b8+-0xb*0x21d));const _0x5a3187=new URL(gitLabHost);_0x5a3187['username']=gitlabConstants['gitlabCiToken'],_0x5a3187['password']=_0x47738f;const _0x509315='url.'+_0x5a3187['toString']()+'.insteadOf\x20\x22git@gitlab.com:\x22';execSyncCommand(gitConfigGlobal+'\x20'+_0x509315);}__name(configureCiToken,'configureCiToken');function configureUser(){execSyncCommand(gitCommands['configUserName']),execSyncCommand(gitCommands['configUserEmail']);}__name(configureUser,'configureUser');function configSubmoduleUrl(_0xb1a419,_0x403362){execSyncCommand(''+gitCommands['configSubmoduleCommand']+_0xb1a419+'.url\x20\x22'+_0x403362+'\x22');}__name(configSubmoduleUrl,'configSubmoduleUrl');function setRemoteUrl(_0x18a9fe,_0x3c5559='.'){execSyncCommand(gitCommands['setRemoteCommand']+'\x20\x22'+_0x18a9fe+'\x22',{'cwd':_0x3c5559});}__name(setRemoteUrl,'setRemoteUrl');function add(_0x1060d6){if(_0x1060d6['length']===-0xf2a+0x767+0x1*0x7c3)return;execSyncCommand(gitCommands['add']+'\x20'+_0x1060d6['join']('\x20'));}__name(add,'add');function hasStagedChanges(){try{return execSyncCommand(gitCommands['diff'],{'stdio':stdioStatuses['ignore']}),![];}catch{return !![];}}__name(hasStagedChanges,'hasStagedChanges');function getGitDiffFiles(_0x232b13){let _0x18cc60=[];const _0x6b8996=execOutput(_0x232b13)['trim']();return _0x6b8996&&(_0x18cc60=_0x6b8996['split']('\x0a')['map'](_0xff2a06=>_0xff2a06['trim']())['filter'](_0x4df976=>_0x4df976)),_0x18cc60;}__name(getGitDiffFiles,'getGitDiffFiles');function clean(){execSyncCommand(gitCommands['clean']+'\x20'+gitFlags['force']+'\x20'+gitFlags['directories']);}__name(clean,'clean');function commit(_0x3fb3d1,_0x3b172e){const _0x2cb523=_0x3b172e?.['noVerify']?'\x20'+gitFlags['noVerify']:'';execSyncCommand(gitCommands['commit']+'\x20\x22'+_0x3fb3d1+'\x22'+_0x2cb523);}__name(commit,'commit');function push(_0x4501c0,_0x573c61){const _0x5b82ef=_0x573c61?.['remoteTarget']??gitRemotes['origin'];execSyncCommand(gitCommands['push']+'\x20'+_0x5b82ef+'\x20\x22'+_0x4501c0+'\x22');}__name(push,'push');function buildGitHttpsUrl(_0x768753){const _0x3e35aa=new URL(_0x768753['owner']+'/'+_0x768753['repo']+folders['dotGit'],gitLabHost);return _0x3e35aa['username']=_0x768753['gitUser'],_0x3e35aa['password']=_0x768753['gitToken'],_0x3e35aa['href'];}__name(buildGitHttpsUrl,'buildGitHttpsUrl');

export { add, buildGitHttpsUrl, checkoutAll, checkoutBranch, checkoutRemoteBranch, clean, cloneRepo, cloneRepository, commit, configGlobal, configSubmoduleUrl, configureCiToken, configureUser, fetchOrigin, fetchQuiet, forceCheckoutAndTrack, getGitDiffFiles, hasStagedChanges, mergeOriginMain, pullMain, push, remoteBranchExists, resetHead, setRemoteUrl, submoduleUpdate };
