"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPersonalAccessToken = exports.findGithubPersonalAccessToken = exports.extractConstraints = exports.requireComposerDependencyInstallation = exports.getPhpConstraint = exports.getComposerArguments = exports.composerVersioningId = void 0;
// TODO: types (#7154)
/* eslint-disable @typescript-eslint/restrict-template-expressions */
const shlex_1 = require("shlex");
const global_1 = require("../../../config/global");
const logger_1 = require("../../../logger");
const host_rules_1 = require("../../../util/host-rules");
const regex_1 = require("../../../util/regex");
const composer_1 = require("../../versioning/composer");
Object.defineProperty(exports, "composerVersioningId", { enumerable: true, get: function () { return composer_1.id; } });
const depRequireInstall = new Set(['symfony/flex']);
function getComposerArguments(config, toolConstraint) {
    let args = '';
    if (config.composerIgnorePlatformReqs) {
        if (config.composerIgnorePlatformReqs.length === 0) {
            // TODO: toolConstraint.constraint can be null or undefined? (#7154)
            const major = composer_1.api.getMajor(toolConstraint.constraint);
            const minor = composer_1.api.getMinor(toolConstraint.constraint);
            args += composer_1.api.matches(`${major}.${minor}`, '^2.2')
                ? " --ignore-platform-req='ext-*' --ignore-platform-req='lib-*'"
                : ' --ignore-platform-reqs';
        }
        else {
            config.composerIgnorePlatformReqs.forEach((req) => {
                args += ' --ignore-platform-req ' + (0, shlex_1.quote)(req);
            });
        }
    }
    args += ' --no-ansi --no-interaction';
    if (!global_1.GlobalConfig.get('allowScripts') || config.ignoreScripts) {
        args += ' --no-scripts --no-autoloader';
    }
    if (!global_1.GlobalConfig.get('allowPlugins') || config.ignorePlugins) {
        args += ' --no-plugins';
    }
    return args;
}
exports.getComposerArguments = getComposerArguments;
function getPhpConstraint(constraints) {
    const { php } = constraints;
    if (php) {
        logger_1.logger.debug('Using php constraint from config');
        return php;
    }
    return null;
}
exports.getPhpConstraint = getPhpConstraint;
function requireComposerDependencyInstallation(lock) {
    return (lock.packages?.some((p) => depRequireInstall.has(p.name)) === true ||
        lock['packages-dev']?.some((p) => depRequireInstall.has(p.name)) === true);
}
exports.requireComposerDependencyInstallation = requireComposerDependencyInstallation;
function extractConstraints(composerJson, lockParsed) {
    const res = { composer: '1.*' };
    // extract php
    if (composerJson.config?.platform?.php) {
        const major = composer_1.api.getMajor(composerJson.config.platform.php);
        const minor = composer_1.api.getMinor(composerJson.config.platform.php) ?? 0;
        const patch = composer_1.api.getPatch(composerJson.config.platform.php) ?? 0;
        res.php = `<=${major}.${minor}.${patch}`;
    }
    else if (composerJson.require?.php) {
        res.php = composerJson.require.php;
    }
    // extract direct composer dependency
    if (composerJson.require?.['composer/composer']) {
        res.composer = composerJson.require?.['composer/composer'];
    }
    else if (composerJson['require-dev']?.['composer/composer']) {
        res.composer = composerJson['require-dev']?.['composer/composer'];
    }
    // composer platform package
    else if (composerJson.require?.['composer']) {
        res.composer = composerJson.require?.['composer'];
    }
    else if (composerJson['require-dev']?.['composer']) {
        res.composer = composerJson['require-dev']?.['composer'];
    }
    // check last used composer version
    else if (lockParsed?.['plugin-api-version']) {
        const major = composer_1.api.getMajor(lockParsed?.['plugin-api-version']);
        const minor = composer_1.api.getMinor(lockParsed?.['plugin-api-version']);
        res.composer = `^${major}.${minor}`;
    }
    // check composer api dependency
    else if (composerJson.require?.['composer-runtime-api']) {
        const major = composer_1.api.getMajor(composerJson.require?.['composer-runtime-api']);
        const minor = composer_1.api.getMinor(composerJson.require?.['composer-runtime-api']);
        res.composer = `^${major}.${minor}`;
    }
    return res;
}
exports.extractConstraints = extractConstraints;
function findGithubPersonalAccessToken(search) {
    const token = (0, host_rules_1.find)(search)?.token;
    if (token && isPersonalAccessToken(token)) {
        return token;
    }
    return undefined;
}
exports.findGithubPersonalAccessToken = findGithubPersonalAccessToken;
function isPersonalAccessToken(token) {
    return (0, regex_1.regEx)(/^ghp_/).test(token);
}
exports.isPersonalAccessToken = isPersonalAccessToken;
//# sourceMappingURL=utils.js.map