"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.matchesWithStar = matchesWithStar;
exports.remapSourcePathForTargetFolder = remapSourcePathForTargetFolder;
exports.remapPathForExtractorMetadata = remapPathForExtractorMetadata;
exports.makeBinLinksAsync = makeBinLinksAsync;
const node_path_1 = __importDefault(require("node:path"));
const link_bins_1 = __importDefault(require("@pnpm/link-bins"));
const node_core_library_1 = require("@rushstack/node-core-library");
const terminal_1 = require("@rushstack/terminal");
const constants_1 = require("./scripts/createLinks/utilities/constants");
function matchesWithStar(patternWithStar, input) {
    // Map "@types/*" --> "^\@types\/.*$"
    const pattern = '^' +
        patternWithStar
            .split('*')
            .map((x) => node_core_library_1.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"
 */
function remapSourcePathForTargetFolder(options) {
    const { sourcePath, sourceRootFolder, targetRootFolder } = options;
    const relativePath = node_path_1.default.relative(sourceRootFolder, sourcePath);
    if (relativePath.startsWith('..')) {
        throw new Error(`Source path "${sourcePath}" is not under "${sourceRootFolder}"`);
    }
    const absolutePathInTargetFolder = node_path_1.default.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"
 */
function remapPathForExtractorMetadata(folderPath, filePath) {
    const relativePath = node_path_1.default.relative(folderPath, filePath);
    if (relativePath.startsWith('..')) {
        throw new Error(`Path "${filePath}" is not under "${folderPath}"`);
    }
    return node_core_library_1.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
 */
async function makeBinLinksAsync(terminal, extractedProjectFolderPaths) {
    const binFilePaths = [];
    await node_core_library_1.Async.forEachAsync(extractedProjectFolderPaths, async (extractedProjectFolderPath) => {
        const extractedProjectNodeModulesFolderPath = `${extractedProjectFolderPath}/node_modules`;
        const extractedProjectBinFolderPath = `${extractedProjectNodeModulesFolderPath}/.bin`;
        const linkedBinPackageNames = await (0, link_bins_1.default)(extractedProjectNodeModulesFolderPath, extractedProjectBinFolderPath, {
            warn: (msg) => terminal.writeLine(terminal_1.Colorize.yellow(msg))
        });
        if (linkedBinPackageNames.length) {
            const binFolderItems = await node_core_library_1.FileSystem.readFolderItemNamesAsync(extractedProjectBinFolderPath);
            for (const binFolderItem of binFolderItems) {
                const binFilePath = `${extractedProjectBinFolderPath}/${binFolderItem}`;
                terminal.writeVerboseLine(`Created .bin file: ${binFilePath}`);
                binFilePaths.push(binFilePath);
            }
        }
    }, {
        concurrency: constants_1.MAX_CONCURRENCY
    });
    return binFilePaths;
}
//# sourceMappingURL=Utils.js.map