"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UvProcessor = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const shlex_1 = require("shlex");
const error_messages_1 = require("../../../../constants/error-messages");
const logger_1 = require("../../../../logger");
const exec_1 = require("../../../../util/exec");
const fs_1 = require("../../../../util/fs");
const result_1 = require("../../../../util/result");
const schema_1 = require("../schema");
const utils_1 = require("../utils");
const uvUpdateCMD = 'uv lock';
class UvProcessor {
    process(project, deps) {
        const uv = project.tool?.uv;
        if (is_1.default.nullOrUndefined(uv)) {
            return deps;
        }
        deps.push(...(0, utils_1.parseDependencyList)(utils_1.depTypes.uvDevDependencies, uv['dev-dependencies']));
        // https://docs.astral.sh/uv/concepts/dependencies/#dependency-sources
        // Skip sources that are either not yet handled by Renovate (e.g. git), or do not make sense to handle (e.g. path).
        if (uv.sources) {
            for (const dep of deps) {
                if (!dep.depName) {
                    continue;
                }
                const depSource = uv.sources[dep.depName];
                if (depSource) {
                    if (depSource.git) {
                        dep.skipReason = 'git-dependency';
                    }
                    else if (depSource.url) {
                        dep.skipReason = 'unsupported-url';
                    }
                    else if (depSource.path) {
                        dep.skipReason = 'path-dependency';
                    }
                    else if (depSource.workspace) {
                        dep.skipReason = 'inherited-dependency';
                    }
                    else {
                        dep.skipReason = 'invalid-dependency-specification';
                    }
                }
            }
        }
        return deps;
    }
    async extractLockedVersions(project, deps, packageFile) {
        const lockFileName = (0, fs_1.getSiblingFileName)(packageFile, 'uv.lock');
        const lockFileContent = await (0, fs_1.readLocalFile)(lockFileName, 'utf8');
        if (lockFileContent) {
            const { val: lockFileMapping, err } = result_1.Result.parse(lockFileContent, schema_1.UvLockfileSchema).unwrap();
            if (err) {
                logger_1.logger.debug({ packageFile, err }, `Error parsing uv lock file`);
            }
            else {
                for (const dep of deps) {
                    const packageName = dep.packageName;
                    if (packageName && packageName in lockFileMapping) {
                        dep.lockedVersion = lockFileMapping[packageName];
                    }
                }
            }
        }
        return Promise.resolve(deps);
    }
    async updateArtifacts(updateArtifact, project) {
        const { config, updatedDeps, packageFileName } = updateArtifact;
        const isLockFileMaintenance = config.updateType === 'lockFileMaintenance';
        // abort if no lockfile is defined
        const lockFileName = (0, fs_1.getSiblingFileName)(packageFileName, 'uv.lock');
        try {
            const existingLockFileContent = await (0, fs_1.readLocalFile)(lockFileName, 'utf8');
            if (is_1.default.nullOrUndefined(existingLockFileContent)) {
                logger_1.logger.debug('No uv.lock found');
                return null;
            }
            const pythonConstraint = {
                toolName: 'python',
                constraint: config.constraints?.python ?? project.project?.['requires-python'],
            };
            const uvConstraint = {
                toolName: 'uv',
                constraint: config.constraints?.uv,
            };
            const execOptions = {
                cwdFile: packageFileName,
                docker: {},
                userConfiguredEnv: config.env,
                toolConstraints: [pythonConstraint, uvConstraint],
            };
            // on lockFileMaintenance do not specify any packages and update the complete lock file
            // else only update specific packages
            let cmd;
            if (isLockFileMaintenance) {
                cmd = `${uvUpdateCMD} --upgrade`;
            }
            else {
                cmd = generateCMD(updatedDeps);
            }
            await (0, exec_1.exec)(cmd, execOptions);
            // check for changes
            const fileChanges = [];
            const newLockContent = await (0, fs_1.readLocalFile)(lockFileName, 'utf8');
            const isLockFileChanged = existingLockFileContent !== newLockContent;
            if (isLockFileChanged) {
                fileChanges.push({
                    file: {
                        type: 'addition',
                        path: lockFileName,
                        contents: newLockContent,
                    },
                });
            }
            else {
                logger_1.logger.debug('uv.lock is unchanged');
            }
            return fileChanges.length ? fileChanges : null;
        }
        catch (err) {
            // istanbul ignore if
            if (err.message === error_messages_1.TEMPORARY_ERROR) {
                throw err;
            }
            logger_1.logger.debug({ err }, 'Failed to update uv lock file');
            return [
                {
                    artifactError: {
                        lockFile: lockFileName,
                        stderr: err.message,
                    },
                },
            ];
        }
    }
}
exports.UvProcessor = UvProcessor;
function generateCMD(updatedDeps) {
    const deps = [];
    for (const dep of updatedDeps) {
        switch (dep.depType) {
            case utils_1.depTypes.optionalDependencies: {
                deps.push(dep.depName.split('/')[1]);
                break;
            }
            case utils_1.depTypes.uvDevDependencies: {
                deps.push(dep.depName);
                break;
            }
            case utils_1.depTypes.buildSystemRequires:
                // build requirements are not locked in the lock files, no need to update.
                break;
            default: {
                deps.push(dep.packageName);
            }
        }
    }
    return `${uvUpdateCMD} ${deps.map((dep) => `--upgrade-package ${(0, shlex_1.quote)(dep)}`).join(' ')}`;
}
//# sourceMappingURL=uv.js.map