/**
 * @license Copyright (c) 2017-2025, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md.
 */

'use strict';

const upath = require( 'upath' );
const fs = require( 'fs-extra' );

/**
 * @returns {Promise<void>}
 */
module.exports = async function copyProjectIcons( projectGlobals, buildDirectory ) {
	const availableProjects = Object.keys( projectGlobals ).filter( projectName => projectName !== 'common' );

	for ( const project of availableProjects ) {
		const projectDetails = projectGlobals[ project ];
		const destinationIconsPath = upath.join( buildDirectory, projectDetails.BASE_PATH, 'assets', 'icons' );

		for ( const [ sourceFile, destinationName ] of projectDetails._icons ) {
			const fullSourcePath = upath.join( projectDetails.config.projectRootPath, 'node_modules', sourceFile );
			const fullDestinationPath = upath.join( destinationIconsPath, destinationName );

			try {
				await fs.copy( fullSourcePath, fullDestinationPath );
			} catch {
				console.warn(
					`⚠️ The "${ sourceFile }" icon cannot be copied because it does not exist in "${ fullSourcePath }".`
				);
			}
		}
	}
};
