/**
* Copyright Super iPaaS Integration LLC, an IBM Company 2024
*/
import {prepareGatewayJson, prepareArchiveBuffer, executeDeployment} from '../deployers/project/projects-deployer.js';
import {DeployOptionsModel} from '../model/studio/command-options/deploy-options-model.js';
import {buildAssetsOrProjects, writeArchive} from './helpers/deploy-action-helper.js'
import {authTokenPrompt, passwordPrompt} from '../prompts/input-prompt.js';
import { setupDebugManager } from './test-action.js';

export const deployAction = async (projects: string, options: DeployOptionsModel) => {

	let gatewaySecret;
	let is_mcsp_enabled=false;
	if(options.username)
	{
		gatewaySecret= await passwordPrompt(options.password);
	}
	else{
		gatewaySecret= await authTokenPrompt(options.authToken);
		is_mcsp_enabled= true;
	}
	const gatewayJson = prepareGatewayJson(options.target, options.username, gatewaySecret, options.overwrite, is_mcsp_enabled);
    const debug = options.debug;
    setupDebugManager(debug);
	if(options.archive) {
		const archiveBuffer = prepareArchiveBuffer(options.archive);
		await executeDeployment(gatewayJson, archiveBuffer);
	
	} else {
		const zipBuffer = await buildAssetsOrProjects(projects, options.all, options.names, options.localDir);

		if(options.debug){
			await writeArchive(projects, options.all, options.names, zipBuffer);
		}
		await executeDeployment(gatewayJson, zipBuffer);
	}
};




