'use strict';
import { readFile, writeFile } from 'node:fs/promises';
import { stringEncodingNames } from '../../../base-repo/app-constants/internal-system-constants.js';
import { nonRootProjectPaths, buildPaths } from '../../../base-repo/app-constants/project-paths.js';
import { ciArtifacts } from '../../../base-repo/constants/gitlab.js';
import { npmCommands, npmFlags, systemCommands } from '../constants/npm-commands.js';
import { environmentVariables, ciLogMessages, ciTimeouts, timeConstants } from '../constants/ci-constants.js';
import { setupNpmAuth, cleanupNpmAuth } from '../setup/npm-auth.js';
import { execSyncCommand, execOutput } from '../tools/exec.js';
import { createEarlyInfraError } from '../tools/create-early-infra-error.js';
import { cliLogInfo, cliLogSeparator, cliLogSubtitle, cliLogError, cliLogSuccess } from '../../../fd-toolbox/logging/cli-logger.js';
import { getBooleanEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';
import { jsBasicTypes } from '../../../base-repo/constants/basic-types.js';
import { packageJsonPath } from '../constants/job-constants.js';

var __defProp=Object['defineProperty'],__name=(_0x3c9950,_0x20f8cf)=>__defProp(_0x3c9950,'name',{'value':_0x20f8cf,'configurable':!![]});async function publishPackage(){const _0x4d4733=getBooleanEnvSetting(environmentVariables['ciPublishAndTestPackageDryRun']);_0x4d4733&&(cliLogInfo('[DRY\x20RUN]\x20MODE\x20ACTIVE:\x20\x27'+environmentVariables['ciPublishAndTestPackageDryRun']+'\x27\x20flag\x20is\x20true.'),cliLogInfo('[DRY\x20RUN]\x20This\x20pipeline\x20will\x20execute\x20in\x20a\x20safely\x20isolated\x20dry-run\x20environment.'),cliLogInfo('[DRY\x20RUN]\x20Packages\x20WILL\x20NOT\x20be\x20published\x20to\x20NPM,\x20and\x20commits\x20WILL\x20NOT\x20be\x20pushed\x20to\x20remote\x20repositories.'),cliLogSeparator());cliLogSubtitle('Step\x201.\x20Building\x20and\x20verifying\x20package');const _0x16fa40=await buildAndVerify();cliLogSeparator(),cliLogSubtitle('Step\x202.\x20Setting\x20up\x20NPM\x20authentication');const {npmrcPath:_0x4ed5af,originalContent:_0x275cd1}=await setupNpmAuth(nonRootProjectPaths['currentDir']+buildPaths['buildPackage']);cliLogSeparator();try{cliLogSubtitle('Step\x203.\x20Publishing\x20package'),await publishPackageToNpm(_0x16fa40);}finally{cliLogSeparator(),cliLogSubtitle('Step\x204.\x20Cleaning\x20up\x20npm\x20authentication\x20files'),await cleanupNpmAuth(_0x4ed5af,_0x275cd1);}cliLogSeparator(),cliLogSubtitle('Step\x205.\x20Creating\x20version\x20artifact'),await createVersionArtifact(_0x16fa40);}__name(publishPackage,'publishPackage');async function buildAndVerify(){execSyncCommand(npmCommands['npmBuild']);const _0xc3a9c3=await readFile(packageJsonPath,stringEncodingNames['utfDash8']),_0x4b3cce=JSON['parse'](_0xc3a9c3),_0x202087=_0x4b3cce?.['version'];if(typeof _0x202087!=='string'||!_0x202087)throw createEarlyInfraError('\x27version\x27\x20field\x20not\x20found\x20or\x20is\x20not\x20a\x20string\x20in\x20\x27'+packageJsonPath+'\x27.');return _0x202087;}__name(buildAndVerify,'buildAndVerify');async function publishPackageToNpm(_0xfb2eb7){const _0x45811b=getBooleanEnvSetting(environmentVariables['ciPublishAndTestPackageDryRun']);let _0x51037d=npmCommands['npmPublish']+'\x20'+npmFlags['json'];_0x45811b&&(cliLogInfo(''+ciLogMessages['dryRun']+environmentVariables['ciPublishAndTestPackageDryRun']+'\x27\x20flag\x20is\x20active.\x20Executing\x20NPM\x20publish\x20in\x20dry-run\x20mode.'),_0x51037d+='\x20'+npmFlags['dryRun']);let _0x4cfbe0='';try{const _0x514888=execOutput(_0x51037d,{'cwd':nonRootProjectPaths['currentDir']+buildPaths['buildPackage']}),_0x1cda0a=JSON['parse'](_0x514888),_0x384f7e={'name':_0x1cda0a?.['name'],'version':_0x1cda0a?.['version']},_0x3a7f0b=_0x1cda0a?.['version'];if(typeof _0x3a7f0b!==jsBasicTypes['string']||!_0x3a7f0b){cliLogError('Version\x20not\x20found\x20in\x20publish\x20command\x20output.\x20Full\x20object\x20received:'),cliLogInfo(_0x1cda0a);throw createEarlyInfraError('Version\x20not\x20found\x20in\x20publish\x20command\x20output.');}_0x4cfbe0=_0x3a7f0b;const _0x47fc5e=ciTimeouts['npmRegistryUpdate']/timeConstants['millisecondsInSecond'],_0x54e56e='Waiting\x20'+_0x47fc5e+'\x20seconds\x20for\x20npm\x20registry\x20to\x20update\x20after\x20publish...';cliLogInfo(_0x54e56e),await new Promise(_0x4f7d1c=>setTimeout(_0x4f7d1c,ciTimeouts['npmRegistryUpdate'])),cliLogInfo(JSON['stringify'](_0x384f7e,void(-0x15d1+0x905*0x3+-0x53e),-0x1bba+0xfde+-0x31*-0x3e)),cliLogSuccess('Successfully\x20published\x20version:\x20'+_0x3a7f0b),_0x45811b&&(cliLogInfo('[DRY\x20RUN]\x20This\x20version\x20was\x20not\x20actually\x20published\x20to\x20NPM.'),cliLogInfo('[DRY\x20RUN]\x20This\x20is\x20the\x20version\x20that\x20would\x20have\x20been\x20published\x20during\x20a\x20normal\x20run.'));}catch(_0x28163d){cliLogError('Publish\x20command\x20failed.\x0a',_0x28163d&&typeof _0x28163d==='object'&&systemCommands['stderr']in _0x28163d?String(_0x28163d['stderr']):_0x28163d);throw _0x28163d;}if(_0x4cfbe0!==_0xfb2eb7){const _0x1305f8='CRITICAL:\x20Version\x20mismatch\x20after\x20publish!\x20Build\x20generated\x20\x27'+_0xfb2eb7+'\x27,\x20but\x20\x27'+npmCommands['npmPublish']+'\x27\x20reported\x20\x27'+_0x4cfbe0+'\x27.';cliLogError(_0x1305f8);throw createEarlyInfraError(_0x1305f8);}}__name(publishPackageToNpm,'publishPackageToNpm');async function createVersionArtifact(_0x4a0079){const _0x53b281=ciArtifacts['ciSaasKitPublishVersionFile'];await writeFile(_0x53b281,_0x4a0079),cliLogInfo(''+ciLogMessages['artifactPrefix']+_0x53b281+'\x27\x20created.');}__name(createVersionArtifact,'createVersionArtifact');

export { publishPackage };
