"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.supportedDatasources = exports.categories = exports.defaultConfig = void 0;
exports.extractPackageFile = extractPackageFile;
const tslib_1 = require("tslib");
const moo_1 = tslib_1.__importDefault(require("moo"));
const regex_1 = require("../../../util/regex");
const nuget_1 = require("../../datasource/nuget");
exports.defaultConfig = {
    fileMatch: ['\\.cake$'],
};
exports.categories = ['dotnet'];
const lexer = moo_1.default.states({
    main: {
        lineComment: { match: /\/\/.*?$/ }, // TODO #12870
        multiLineComment: { match: /\/\*[^]*?\*\//, lineBreaks: true }, // TODO #12870
        dependency: {
            match: /^#(?:addin|tool|module|load|l)\s+(?:nuget|dotnet):.*$/, // TODO #12870
        },
        dependencyQuoted: {
            match: /^#(?:addin|tool|module|load|l)\s+"(?:nuget|dotnet):[^"]+"\s*$/, // TODO #12870
            value: (s) => s.trim().slice(1, -1),
        },
        unknown: moo_1.default.fallback,
    },
});
function parseDependencyLine(line) {
    try {
        let url = line.replace((0, regex_1.regEx)(/^[^:]*:/), '');
        const isEmptyHost = url.startsWith('?');
        url = isEmptyHost ? `http://localhost/${url}` : url;
        const { origin, pathname, protocol, searchParams } = new URL(url);
        const registryUrl = `${origin}${pathname}`;
        const depName = searchParams.get('package');
        const currentValue = searchParams.get('version') ?? undefined;
        const result = {
            datasource: nuget_1.NugetDatasource.id,
            depName,
            currentValue,
        };
        if (!isEmptyHost) {
            if (protocol.startsWith('http')) {
                result.registryUrls = [registryUrl];
            }
            else {
                result.skipReason = 'unsupported-url';
            }
        }
        return result;
    }
    catch {
        return null;
    }
}
function extractPackageFile(content) {
    const deps = [];
    lexer.reset(content);
    let token = lexer.next();
    while (token) {
        const { type, value } = token;
        if (type === 'dependency' || type === 'dependencyQuoted') {
            const dep = parseDependencyLine(value);
            if (dep) {
                deps.push(dep);
            }
        }
        token = lexer.next();
    }
    return { deps };
}
exports.supportedDatasources = [nuget_1.NugetDatasource.id];
//# sourceMappingURL=index.js.map