'use strict';
import _0x3881d1__default from 'fs';
import { parseEnv } from 'node:util';
import { environmentVariables } from '../constants/ci-constants.js';
import { booleanStringValues } from '../../../base-repo/constants/type-string-values.js';
import { cliLogSubtitle, cliLogError, cliLogSuccess, cliLogWarning } from '../../../fd-toolbox/logging/cli-logger.js';
import { execSyncCommand, execOutput } from '../tools/exec.js';
import { npmCommands } from '../constants/npm-commands.js';
import { packagesPath } from '../../../base-repo/app-constants/project-paths.js';
import { installLocalSaasKit } from '../setup/install-local-saaskit.js';
import { getStringEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';
import { toolboxEnvSettingKeys } from '../../../fd-toolbox/infra/toolbox-env-setting-keys.js';

var __defProp=Object['defineProperty'],__name=(_0xf8c7e0,_0xb07f60)=>__defProp(_0xf8c7e0,'name',{'value':_0xb07f60,'configurable':!![]});const deployVercelConfig={'vercelToken':getStringEnvSetting(environmentVariables['vercel']),'commitSha':getStringEnvSetting(environmentVariables['ciCommitSha'])},commitHash='commitHash';async function deployToVercel(){cliLogSubtitle('==>\x20Starting\x20deployToVercel\x20job'),cliLogSubtitle('Building\x20local\x20package...'),await initializeTestProject(),cliLogSubtitle('==>\x20Installing\x20Vercel\x20CLI...'),execSyncCommand('npm\x20install\x20-g\x20vercel');if(!deployVercelConfig['vercelToken'])cliLogError('VERCEL_TOKEN\x20is\x20not\x20set!');const _0x774f83=deployVercelConfig['commitSha']??'';if(!await checkForChanges(_0x774f83)){cliLogSubtitle('No\x20changes\x20for\x20deployment.\x20Skipping\x20tests.');return;}cliLogSubtitle('New\x20deploy:\x20'+_0x774f83),execSyncCommand('vercel\x20link\x20--token='+deployVercelConfig['vercelToken']+'\x20--project\x20saas-kit\x20--yes');const _0x567b8d=await pullVercelConfig(),_0x1f4a3f=_0x567b8d[toolboxEnvSettingKeys['databaseConnectionString']];!_0x1f4a3f&&(cliLogError('Failed\x20to\x20pull\x20'+toolboxEnvSettingKeys['databaseConnectionString']+'\x20from\x20Vercel\x20environment.'),process['exit'](0xa55*-0x1+0x9*0x255+-0x9*0x12f));Object['assign'](process['env'],_0x567b8d),cliLogSubtitle('==>\x20Resetting\x20and\x20seeding\x20Vercel\x20database...'),execSyncCommand(npmCommands['recreateDb']),cliLogSubtitle('==>\x20Starting\x20Vercel\x20deployment...');const _0x5e9910=execOutput('vercel\x20deploy\x20--prod\x20--yes\x20--token='+deployVercelConfig['vercelToken']+'\x20--env\x20FD_APP_ENV=test\x20--env\x20'+environmentVariables['ci']+'='+booleanStringValues['true']+'\x20--archive=tgz\x20\x20--meta\x20commitHash='+_0x774f83)['trim']();if(_0x5e9910){cliLogSuccess('Deployment\x20successful.\x20URL:\x20'+_0x5e9910);const _0x253f3c=_0x5e9910['endsWith']('/')?_0x5e9910:_0x5e9910+'/';cliLogSubtitle('Final\x20Test\x20URL:\x20'+_0x253f3c),cliLogSubtitle('Skipping\x20automated\x20tests\x20for\x20Vercel\x20deploy\x20job.');}else cliLogError('Deployment\x20failed:\x20No\x20deployment\x20URL\x20returned.'),process['exit'](-0xb2a+-0x2*0x111b+0x2d61);}__name(deployToVercel,'deployToVercel');async function checkForChanges(_0x2566c4){cliLogSubtitle('==>\x20Checking\x20last\x20deployment...');const _0x24d509=execOutput('vercel\x20deployments\x20list\x20--token='+deployVercelConfig['vercelToken']+'\x20--limit=1\x20--meta\x20'+commitHash+'\x202>/dev/null\x20||\x20echo\x20\x22\x22');cliLogSubtitle('Retrieved\x20deployment\x20list');const _0x50cb51=_0x24d509['split']('\x0a')['find'](_0x35761c=>_0x35761c['includes'](commitHash+':')),_0xd5b2c3=_0x50cb51?.['split'](':')[0x17*-0x3a+0x4d9*-0x3+-0x34b*-0x6]?.['trim']()??'';return cliLogSubtitle('Last\x20deployed\x20commit\x20hash:\x20'+_0xd5b2c3),cliLogSubtitle('Current\x20commit\x20hash:\x20'+_0x2566c4),_0xd5b2c3!==_0x2566c4;}__name(checkForChanges,'checkForChanges');async function pullVercelConfig(){cliLogSubtitle('Pulling\x20configuration\x20from\x20Vercel\x20environment...');const _0x1f5253='.env.vercel.tmp';let _0x245ba8={};try{execSyncCommand('vercel\x20env\x20pull\x20'+_0x1f5253+'\x20--token='+deployVercelConfig['vercelToken']+'\x20--yes\x20--environment=production'),_0x3881d1__default['existsSync'](_0x1f5253)&&(_0x245ba8=parseEnv(_0x3881d1__default['readFileSync'](_0x1f5253,'utf8')));}catch(_0x505f57){cliLogWarning('Failed\x20to\x20pull\x20env\x20from\x20Vercel:\x20'+_0x505f57);}finally{if(_0x3881d1__default['existsSync'](_0x1f5253))try{_0x3881d1__default['unlinkSync'](_0x1f5253);}catch(_0xb766a5){cliLogWarning('Failed\x20to\x20cleanup\x20temp\x20file:\x20'+_0xb766a5);}}return _0x245ba8;}__name(pullVercelConfig,'pullVercelConfig');async function initializeTestProject(){execSyncCommand(npmCommands['npmBuild']),execSyncCommand(npmCommands['npmPack']),await installLocalSaasKit(),execSyncCommand(npmCommands['genMeta'],{'cwd':packagesPath['packagesTest']}),execSyncCommand(npmCommands['genSchema'],{'cwd':packagesPath['packagesTest']});}__name(initializeTestProject,'initializeTestProject');

export { deployToVercel };
