// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import path from 'node:path';
import pnpmLinkBins from '@pnpm/link-bins';
import { Async, FileSystem, Path, Text } from '@rushstack/node-core-library';
import { Colorize } from '@rushstack/terminal';
import { MAX_CONCURRENCY } from './scripts/createLinks/utilities/constants';
export function matchesWithStar(patternWithStar, input) {
    // Map "@types/*" --> "^\@types\/.*$"
    const pattern = '^' +
        patternWithStar
            .split('*')
            .map((x) => Text.escapeRegExp(x))
            .join('.*') +
        '$';
    // eslint-disable-next-line @rushstack/security/no-unsafe-regexp
    const regExp = new RegExp(pattern);
    return regExp.test(input);
}
/**
 * Maps a file path under the provided {@link IRemapPathForTargetFolder.sourceRootFolder} to the provided
 * {@link IExtractorOptions.targetRootFolder}.
 *
 * Example input: "C:\\MyRepo\\libraries\\my-lib"
 * Example output: "C:\\MyRepo\\common\\deploy\\libraries\\my-lib"
 */
export function remapSourcePathForTargetFolder(options) {
    const { sourcePath, sourceRootFolder, targetRootFolder } = options;
    const relativePath = path.relative(sourceRootFolder, sourcePath);
    if (relativePath.startsWith('..')) {
        throw new Error(`Source path "${sourcePath}" is not under "${sourceRootFolder}"`);
    }
    const absolutePathInTargetFolder = path.join(targetRootFolder, relativePath);
    return absolutePathInTargetFolder;
}
/**
 * Maps a file path under the provided folder path to the expected path format for the extractor metadata.
 *
 * Example input: "C:\\MyRepo\\libraries\\my-lib"
 * Example output: "common/deploy/libraries/my-lib"
 */
export function remapPathForExtractorMetadata(folderPath, filePath) {
    const relativePath = path.relative(folderPath, filePath);
    if (relativePath.startsWith('..')) {
        throw new Error(`Path "${filePath}" is not under "${folderPath}"`);
    }
    return Path.convertToSlashes(relativePath);
}
/**
 * Creates the .bin files for the extracted projects and returns the paths to the created .bin files.
 *
 * @param terminal - The terminal to write to
 * @param extractedProjectFolderPaths - The paths to the extracted projects
 */
export async function makeBinLinksAsync(terminal, extractedProjectFolderPaths) {
    const binFilePaths = [];
    await Async.forEachAsync(extractedProjectFolderPaths, async (extractedProjectFolderPath) => {
        const extractedProjectNodeModulesFolderPath = `${extractedProjectFolderPath}/node_modules`;
        const extractedProjectBinFolderPath = `${extractedProjectNodeModulesFolderPath}/.bin`;
        const linkedBinPackageNames = await pnpmLinkBins(extractedProjectNodeModulesFolderPath, extractedProjectBinFolderPath, {
            warn: (msg) => terminal.writeLine(Colorize.yellow(msg))
        });
        if (linkedBinPackageNames.length) {
            const binFolderItems = await FileSystem.readFolderItemNamesAsync(extractedProjectBinFolderPath);
            for (const binFolderItem of binFolderItems) {
                const binFilePath = `${extractedProjectBinFolderPath}/${binFolderItem}`;
                terminal.writeVerboseLine(`Created .bin file: ${binFilePath}`);
                binFilePaths.push(binFilePath);
            }
        }
    }, {
        concurrency: MAX_CONCURRENCY
    });
    return binFilePaths;
}
//# sourceMappingURL=Utils.js.map