"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseProps = exports.parseGradle = void 0;
const good_enough_parser_1 = require("good-enough-parser");
const regex_1 = require("../../../util/regex");
const apply_from_1 = require("./parser/apply-from");
const assignments_1 = require("./parser/assignments");
const dependencies_1 = require("./parser/dependencies");
const plugins_1 = require("./parser/plugins");
const registry_urls_1 = require("./parser/registry-urls");
const version_catalogs_1 = require("./parser/version-catalogs");
const utils_1 = require("./utils");
const groovy = good_enough_parser_1.lang.createLang('groovy');
function parseGradle(input, initVars = {}, packageFile = '', fileContents = {}, recursionDepth = 0) {
    let vars = { ...initVars };
    const deps = [];
    const urls = [];
    const query = good_enough_parser_1.query.tree({
        type: 'root-tree',
        maxDepth: 32,
        search: good_enough_parser_1.query.alt(assignments_1.qAssignments, dependencies_1.qDependencies, plugins_1.qPlugins, registry_urls_1.qRegistryUrls, version_catalogs_1.qVersionCatalogs, dependencies_1.qLongFormDep, apply_from_1.qApplyFrom),
    });
    const parsedResult = groovy.query(input, query, {
        packageFile,
        fileContents,
        recursionDepth,
        globalVars: initVars,
        deps: [],
        registryUrls: [],
        varTokens: [],
        tmpTokenStore: {},
        tokenMap: {},
    });
    if (parsedResult) {
        deps.push(...parsedResult.deps);
        vars = { ...vars, ...parsedResult.globalVars };
        urls.push(...parsedResult.registryUrls);
    }
    return { deps, urls, vars };
}
exports.parseGradle = parseGradle;
const propWord = '[a-zA-Z_][a-zA-Z0-9_]*(?:\\.[a-zA-Z_][a-zA-Z0-9_]*)*';
const propRegex = (0, regex_1.regEx)(`^(?<leftPart>\\s*(?<key>${propWord})\\s*[= :]\\s*['"]?)(?<value>[^\\s'"]+)['"]?\\s*$`);
function parseProps(input, packageFile) {
    let offset = 0;
    const vars = {};
    const deps = [];
    for (const line of input.split(regex_1.newlineRegex)) {
        const lineMatch = propRegex.exec(line);
        if (lineMatch?.groups) {
            const { key, value, leftPart } = lineMatch.groups;
            if ((0, utils_1.isDependencyString)(value)) {
                const dep = (0, utils_1.parseDependencyString)(value);
                if (dep) {
                    deps.push({
                        ...dep,
                        managerData: {
                            fileReplacePosition: offset + leftPart.length + dep.depName.length + 1,
                            packageFile,
                        },
                    });
                }
            }
            else {
                vars[key] = {
                    key,
                    value,
                    fileReplacePosition: offset + leftPart.length,
                    packageFile,
                };
            }
        }
        offset += line.length + 1;
    }
    return { vars, deps };
}
exports.parseProps = parseProps;
//# sourceMappingURL=parser.js.map