import { dirname } from 'path';
import { EMULSIFY_PROJECT_CONFIG_FILE } from '../../lib/constants.js';
import findFileInCurrentPath from '../fs/findFileInCurrentPath.js';
import safeResolveWithin from '../fs/safeResolveWithin.js';
import copyItemFromCache from '../cache/copyItemFromCache.js';
import catchLater from '../catchLater.js';
export default async function installGeneralAssetsFromCache(system, variant) {
    const path = findFileInCurrentPath(EMULSIFY_PROJECT_CONFIG_FILE);
    if (!path) {
        throw new Error('Unable to find an Emulsify project to install assets into.');
    }
    const projectRoot = dirname(path);
    const assets = [...(variant.directories || []), ...(variant.files || [])];
    const promises = [];
    for (const asset of assets) {
        const destination = safeResolveWithin(projectRoot, asset.destinationPath, 'General asset destination');
        promises.push(catchLater(copyItemFromCache('systems', [system.name, asset.path], destination, true)));
    }
    await Promise.all(promises);
}
