/**
* Copyright Super iPaaS Integration LLC, an IBM Company 2024
*/
import {showInfo} from '../../helpers/common/message-helper.js';
import {getAllProjectNames} from '../../helpers/apim/root-dir-helper.js';
import {executeBuildProject} from '../../builders/project/projects-builder.js';
import {executeBuildProjectAssets} from '../../builders/project/project-assets-builder.js';
import {
	BUILD_STARTED,
	BUILDING_LIST_ALL_PROJECTS,
	CHECKING_ALL_PROJECTS,
	IDENTIFIED_PROJECTS,
	LINE,
} from '../../constants/message-constants.js';
import {DebugManager} from '../../debug/debug-manager.js';
import {COMMA} from '../../constants/app-constants.js';

export const buildProjects = async (all: boolean, localDir: string, projects: string): Promise<Buffer> => {
	showInfo(LINE);
	showInfo(BUILD_STARTED);
	showInfo(LINE);
	if (all) {
		if(DebugManager.getInstance().isDebugEnabled()) {
			showInfo(BUILDING_LIST_ALL_PROJECTS + localDir);
			showInfo(CHECKING_ALL_PROJECTS + localDir);
		}
		projects = getAllProjectNames(localDir);
		if(DebugManager.getInstance().isDebugEnabled()) {
			showInfo(IDENTIFIED_PROJECTS + projects);
		}
	}

	return executeBuildProject(localDir, projects);

};

export const buildAssets = (assets: string, localDir: string, projects: string): Promise<Buffer> => {
	showInfo(LINE);
	showInfo(BUILD_STARTED);
	showInfo(LINE);
	return executeBuildProjectAssets(projects, localDir, assets);
};

export const getOutputPath = async (projects: string | undefined,
	all: boolean,names: string, fileType: 'build' | 'test'): Promise<string> => {
	let outputPath = `studio-projects-${fileType}.zip`;

	if (all) {
	outputPath = `studio-all-${fileType}.zip`;
	}
	else {
	const projectsArray = projects ? projects.split(COMMA) : [];
	if (projectsArray.length === 1) {
		if (names) {
		outputPath = `studio-${projectsArray[0]}-api-${fileType}.zip`;
		} else {
		outputPath = `studio-${projectsArray[0]}-${fileType}.zip`;
		}
	}
	}

	return outputPath;
};
