/**
 * @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' );

/**
 * @param rootPath
 * @param dst
 * @param {String|Array} files
 * @returns {Array}
 */
module.exports = ( rootPath, dst, files ) => {
	const filePaths = getFilePaths( files );

	return filePaths
		.filter( p => {
			const resolvedPath = upath.resolve( rootPath, p );

			try {
				fs.accessSync( resolvedPath );

				return true;
			} catch {
				console.error( `Extra file not found in ${ resolvedPath }` );

				return false;
			}
		} )
		.map( p => upath.join( dst, p ) );
};

/**
 * @param {String|Array|*} files
 * @return {Array.<String>}
 */
function getFilePaths( files ) {
	if ( typeof files === 'string' ) {
		return [ files ];
	}

	if ( Array.isArray( files ) ) {
		return files;
	}

	return [];
}
