"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractAllPackageFiles = void 0;
const logger_1 = require("../../../logger");
const fs_1 = require("../../../util/fs");
const package_file_1 = require("../npm/extract/common/package-file");
function matchesFileName(fileNameWithPath, fileName) {
    return (fileNameWithPath === fileName || fileNameWithPath.endsWith(`/${fileName}`));
}
async function extractAllPackageFiles(config, matchedFiles) {
    const packageFiles = [];
    for (const matchedFile of matchedFiles) {
        if (!matchesFileName(matchedFile, 'bun.lockb')) {
            logger_1.logger.warn({ matchedFile }, 'Invalid bun lockfile match');
            continue;
        }
        const packageFile = (0, fs_1.getSiblingFileName)(matchedFile, 'package.json');
        const packageFileContent = await (0, fs_1.readLocalFile)(packageFile, 'utf8');
        if (!packageFileContent) {
            logger_1.logger.debug({ packageFile }, 'No package.json found');
            continue;
        }
        let packageJson;
        try {
            packageJson = JSON.parse(packageFileContent);
        }
        catch (err) {
            logger_1.logger.debug({ err }, 'Error parsing package.json');
            continue;
        }
        const extracted = (0, package_file_1.extractPackageJson)(packageJson, packageFile);
        if (!extracted) {
            logger_1.logger.debug({ packageFile }, 'No dependencies found');
            continue;
        }
        packageFiles.push({
            ...extracted,
            packageFile,
            lockFiles: [matchedFile],
        });
    }
    return packageFiles;
}
exports.extractAllPackageFiles = extractAllPackageFiles;
//# sourceMappingURL=extract.js.map