'use strict';
import _0x1c3e3a from 'node:path';
import { folders, buildPaths, files } from '../../../base-repo/app-constants/project-paths.js';
import { execOutput, execSyncCommand } from '../tools/exec.js';
import { npmCommands, npxCommands } from '../constants/npm-commands.js';
import { createSaasKitApp, getRelativeTgzPath } from '../tools/npm-command-funcs.js';
import { cliLogSeparator, cliLogError, cliLogSubtitle, cliLogSuccess } from '../../../fd-toolbox/logging/cli-logger.js';
import { singleOrUndefinedWithWarning } from '../../../fd-toolbox/server/collections/single-funcs.js';
import { readFile, writeFile } from 'node:fs/promises';
import { existsSync } from 'fs';
import { join } from 'path';
import { importBuildFromSubmodule } from '../setup/sync-build-artifacts.js';
import { createEarlyInfraError } from '../tools/create-early-infra-error.js';
import { toolboxEnvSettingKeys } from '../../../fd-toolbox/infra/toolbox-env-setting-keys.js';
import { copyPlaywrightReport } from '../setup/copy-playwright-report.js';
import { cleanup } from '../setup/cleanup.js';
import { installLocalSaasKit } from '../setup/install-local-saaskit.js';
import { waitForServerReady, terminateServerProcess } from '../setup/spawn-background-server.js';
import { ciLogMessages, ciJobs, environmentVariables } from '../constants/ci-constants.js';
import { buildDbConnectionString } from './build-db-connection-string.js';
import { getStringEnvSetting } from '../../../fd-toolbox/infra/env-functions.js';
import { shells } from '../../../base-repo/constants/tools-constants.js';

var __defProp=Object['defineProperty'],__name=(_0x4c340a,_0x2c2eb1)=>__defProp(_0x4c340a,'name',{'value':_0x2c2eb1,'configurable':!![]});async function cliCreateTest(){const _0x415588='create-saas-kit-app',_0x5af64a=folders['test'],_0x13a7ad=_0x1c3e3a['join'](_0x415588,_0x5af64a);try{const _0x461de0=await buildAndPackPackage();cliLogSeparator(),await createTestProject(_0x415588,_0x5af64a),cliLogSeparator(),await installLocalPackage(_0x13a7ad,_0x461de0),cliLogSeparator(),await setupEnvVariables(),cliLogSeparator(),await buildAndTestCliCreatedApp(_0x13a7ad);}catch(_0x50d2ef){cliLogError('[\x20create-saas-kit-app-test\x20JOB\x20]\x20Failed\x20with\x20an\x20unexpected\x20error.');throw _0x50d2ef;}finally{try{await copyPlaywrightReport(),cliLogSubtitle(ciLogMessages['runningCleanupScript']),cleanup();}catch(_0x46d61a){cliLogError(ciLogMessages['cleanupScriptFailed'],_0x46d61a);}}}__name(cliCreateTest,'cliCreateTest');async function buildAndPackPackage(){await importBuildFromSubmodule(),cliLogSubtitle('Step\x201:\x20Building\x20and\x20packing\x20the\x20saas-kit\x20package...');const _0x3951ee=singleOrUndefinedWithWarning(execOutput('find\x20'+buildPaths['buildPackage']+'\x20-name\x20\x22fernir2-saas-kit-*.tgz\x22\x20-printf\x20\x22%f\x22')['split']('\x0a'));if(!_0x3951ee)throw createEarlyInfraError('Packed\x20.tgz\x20file\x20for\x20saas-kit\x20not\x20found\x20after\x20\x27npm\x20pack\x27');const _0x33e242=await installLocalSaasKit();return cliLogSuccess('Package\x20built\x20and\x20packed\x20successfully:\x20'+_0x33e242),_0x33e242;}__name(buildAndPackPackage,'buildAndPackPackage');async function createTestProject(_0x199777,_0x283efa){cliLogSubtitle('Step\x202:\x20Creating\x20a\x20new\x20Next.js\x20project\x20via\x20create-saas-kit-app\x20CLI\x20in\x20\x27'+_0x199777+'\x27...'),execSyncCommand('mkdir\x20-p\x20'+_0x199777),await copyEnvFile(_0x199777);const _0x54e6bc=createSaasKitApp(_0x283efa);execSyncCommand(_0x54e6bc,{'cwd':_0x199777}),cliLogSuccess('CLI\x20executed\x20and\x20project\x20\x27'+_0x283efa+'\x27\x20created\x20inside\x20\x27'+_0x199777+'\x27.');}__name(createTestProject,'createTestProject');async function installLocalPackage(_0x5e3da1,_0xffab33){cliLogSubtitle('Step\x203:\x20Installing\x20local\x20saas-kit\x20package\x20into\x20the\x20new\x20project...');const _0x5a9c92=getRelativeTgzPath(_0xffab33);cliLogSubtitle('Installing\x20from\x20relative\x20path:\x20'+_0x5a9c92),execSyncCommand(npmCommands['removeLocalSaasKit'],{'cwd':_0x5e3da1});const _0x1ee623='\x0a\x20\x20\x20\x20\x20\x20\x20\x20if\x20[\x20!\x20-f\x20\x22'+_0x5a9c92+'\x22\x20];\x20then\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20echo\x20\x22ERROR:\x20Expected\x20TGZ\x20file\x20not\x20found\x20at\x20'+_0x5a9c92+'\x20(from\x20inside\x20'+_0x5e3da1+')\x22\x0a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20exit\x201\x0a\x20\x20\x20\x20\x20\x20\x20\x20fi\x0a\x20\x20\x20\x20\x20\x20\x20\x20pnpm\x20add\x20\x22'+_0x5a9c92+'\x22\x20--save-dev\x0a\x20\x20\x20\x20';execSyncCommand(_0x1ee623,{'cwd':_0x5e3da1,'shell':shells['binBash']}),cliLogSuccess('Local\x20package\x20installed\x20successfully.');}__name(installLocalPackage,'installLocalPackage');async function setupEnvVariables(){cliLogSubtitle('Step\x204:\x20Setting\x20up\x20environment\x20variables\x20for\x20'+ciJobs['cliCreateTest']+'...');const _0x54f410='create-saas-kit-app-'+getStringEnvSetting(environmentVariables['ciCommitSha']),_0x27b5ac=buildDbConnectionString(_0x54f410);process['env'][environmentVariables['databaseName']]=_0x54f410,process['env'][toolboxEnvSettingKeys['databaseConnectionString']]=_0x27b5ac,cliLogSuccess(ciLogMessages['environmentVariablesSet']);}__name(setupEnvVariables,'setupEnvVariables');async function buildAndTestCliCreatedApp(_0x5c6800){cliLogSubtitle('Step\x205:\x20Building\x20the\x20newly\x20created\x20test\x20application...');let _0x309550;try{_0x309550=await waitForServerReady(_0x5c6800,npmCommands['start']),cliLogSuccess(ciLogMessages['serverReadyStartingTests']),execSyncCommand(npxCommands['testSingular']);}catch(_0x37d6e5){_0x37d6e5 instanceof Error&&cliLogError(ciLogMessages['errorDetails'],_0x37d6e5['message']);throw _0x37d6e5;}finally{await terminateServerProcess(_0x309550);}}__name(buildAndTestCliCreatedApp,'buildAndTestCliCreatedApp');async function copyEnvFile(_0x3221b1){const _0x2f9992=getPathWithCwd(files['dotEnv']);if(existsSync(_0x2f9992)){const _0x40831f=await readFile(_0x2f9992,'utf8'),_0x91b0f=_0x40831f['split']('\x0a')['filter'](_0x2484a8=>!_0x2484a8['startsWith'](toolboxEnvSettingKeys['seedDevResources'])||!_0x2484a8['startsWith'](toolboxEnvSettingKeys['seedDemoResources']))['join']('\x0a'),_0x142270=join(process['cwd'](),_0x3221b1);await writeFile(_0x142270+'/'+files['dotEnv'],_0x91b0f);}}__name(copyEnvFile,'copyEnvFile');function getPathWithCwd(_0x306799){return process['cwd']()+'/'+_0x306799;}__name(getPathWithCwd,'getPathWithCwd');

export { cliCreateTest };
