"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractAllPackageFiles = exports.extractPackageFile = void 0;
const logger_1 = require("../../../logger");
const fs_1 = require("../../../util/fs");
const extract_1 = require("../pip_requirements/extract");
const common_1 = require("./common");
function matchManager(filename) {
    if (filename.endsWith('setup.py')) {
        return 'pip_setup';
    }
    if (filename.endsWith('setup.cfg')) {
        return 'setup-cfg';
    }
    if (filename.endsWith('pyproject.toml')) {
        return 'pep621';
    }
    // naive, could be improved, maybe use pip_requirements.fileMatch
    if (filename.endsWith('.in')) {
        return 'pip_requirements';
    }
    return 'unknown';
}
function extractPackageFile(content, packageFile, _config) {
    logger_1.logger.trace('pip-compile.extractPackageFile()');
    const manager = matchManager(packageFile);
    // TODO(not7cd): extract based on manager: pep621, setuptools, identify other missing source types
    switch (manager) {
        // TODO(not7cd): enable in the next PR, when this can be properly tested
        // case 'pip_setup':
        //   return extractSetupPyFile(content, _packageFile, _config);
        // case 'setup-cfg':
        //   return await extractSetupCfgFile(content);
        case 'pip_requirements':
            return (0, extract_1.extractPackageFile)(content);
        case 'unknown':
            logger_1.logger.warn({ packageFile }, `pip-compile: could not determine manager for source file`);
            return null;
        default:
            logger_1.logger.warn({ packageFile, manager }, `pip-compile: support for manager is not yet implemented`);
            return null;
    }
}
exports.extractPackageFile = extractPackageFile;
async function extractAllPackageFiles(config, fileMatches) {
    logger_1.logger.trace('pip-compile.extractAllPackageFiles()');
    const lockFileArgs = new Map();
    const depsBetweenFiles = [];
    // for debugging only ^^^ (for now)
    const packageFiles = new Map();
    for (const fileMatch of fileMatches) {
        const fileContent = await (0, fs_1.readLocalFile)(fileMatch, 'utf8');
        if (!fileContent) {
            logger_1.logger.debug(`pip-compile: no content found for fileMatch ${fileMatch}`);
            continue;
        }
        let pipCompileArgs;
        try {
            pipCompileArgs = (0, common_1.extractHeaderCommand)(fileContent, fileMatch);
            lockFileArgs.set(fileMatch, pipCompileArgs);
        }
        catch (error) {
            logger_1.logger.warn({ fileMatch, error }, 'pip-compile: Failed to extract and parse command in output file header');
            continue;
        }
        for (const constraint in pipCompileArgs.constraintsFiles) {
            // TODO(not7cd): handle constraints
            /* istanbul ignore next */
            depsBetweenFiles.push({
                sourceFile: constraint,
                outputFile: fileMatch,
                type: 'constraint',
            });
        }
        // TODO(not7cd): handle locked deps
        // const lockedDeps = extractRequirementsFile(content);
        for (const packageFile of pipCompileArgs.sourceFiles) {
            depsBetweenFiles.push({
                sourceFile: packageFile,
                outputFile: fileMatch,
                type: 'requirement',
            });
            if (fileMatches.includes(packageFile)) {
                // TODO(not7cd): do something about it
                logger_1.logger.warn({ sourceFile: packageFile, lockFile: fileMatch }, 'pip-compile: lock file acts as source file for another lock file');
                continue;
            }
            if (packageFiles.has(packageFile)) {
                logger_1.logger.debug(`pip-compile: ${packageFile} used in multiple output files`);
                packageFiles.get(packageFile).lockFiles.push(fileMatch);
                continue;
            }
            const content = await (0, fs_1.readLocalFile)(packageFile, 'utf8');
            if (!content) {
                logger_1.logger.debug(`pip-compile: No content for source file ${packageFile}`);
                continue;
            }
            const packageFileContent = extractPackageFile(content, packageFile, config);
            if (packageFileContent) {
                packageFiles.set(packageFile, {
                    ...packageFileContent,
                    lockFiles: [fileMatch],
                    packageFile,
                });
            }
            else {
                logger_1.logger.warn({ packageFile }, 'pip-compile: failed to find dependencies in source file');
            }
        }
    }
    // TODO(not7cd): sort by requirement layering (-r -c within .in files)
    if (packageFiles.size === 0) {
        return null;
    }
    logger_1.logger.debug('pip-compile: dependency graph:\n' +
        (0, common_1.generateMermaidGraph)(depsBetweenFiles, lockFileArgs));
    return Array.from(packageFiles.values());
}
exports.extractAllPackageFiles = extractAllPackageFiles;
//# sourceMappingURL=extract.js.map