"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPackageFile = void 0;
const logger_1 = require("../../../logger");
const fs_1 = require("../../../util/fs");
const regex_1 = require("../../../util/regex");
const git_tags_1 = require("../../datasource/git-tags");
const github_tags_1 = require("../../datasource/github-tags");
const hex_1 = require("../../datasource/hex");
const depSectionRegExp = (0, regex_1.regEx)(/defp\s+deps.*do/g);
const depMatchRegExp = (0, regex_1.regEx)(/{:(?<app>\w+)(\s*,\s*"(?<requirement>[^"]+)")?(\s*,\s*(?<opts>[^}]+))?}/gm);
const gitRegexp = (0, regex_1.regEx)(/git:\s*"(?<value>[^"]+)"/);
const githubRegexp = (0, regex_1.regEx)(/github:\s*"(?<value>[^"]+)"/);
const refRegexp = (0, regex_1.regEx)(/ref:\s*"(?<value>[^"]+)"/);
const branchOrTagRegexp = (0, regex_1.regEx)(/(?:branch|tag):\s*"(?<value>[^"]+)"/);
const organizationRegexp = (0, regex_1.regEx)(/organization:\s*"(?<value>[^"]+)"/);
const commentMatchRegExp = (0, regex_1.regEx)(/#.*$/);
async function extractPackageFile(content, fileName) {
    logger_1.logger.trace('mix.extractPackageFile()');
    const deps = [];
    const contentArr = content
        .split(regex_1.newlineRegex)
        .map((line) => line.replace(commentMatchRegExp, ''));
    for (let lineNumber = 0; lineNumber < contentArr.length; lineNumber += 1) {
        if (contentArr[lineNumber].match(depSectionRegExp)) {
            let depBuffer = '';
            do {
                depBuffer += contentArr[lineNumber] + '\n';
                lineNumber += 1;
            } while (!contentArr[lineNumber].includes('end'));
            let depMatchGroups = depMatchRegExp.exec(depBuffer)?.groups;
            while (depMatchGroups) {
                const { app, requirement, opts } = depMatchGroups;
                const github = githubRegexp.exec(opts)?.groups?.value;
                const git = gitRegexp.exec(opts)?.groups?.value;
                const ref = refRegexp.exec(opts)?.groups?.value;
                const branchOrTag = branchOrTagRegexp.exec(opts)?.groups?.value;
                const organization = organizationRegexp.exec(opts)?.groups?.value;
                let dep;
                if (git ?? github) {
                    dep = {
                        depName: app,
                        currentDigest: ref,
                        currentValue: branchOrTag,
                        datasource: git ? git_tags_1.GitTagsDatasource.id : github_tags_1.GithubTagsDatasource.id,
                        packageName: git ?? github,
                    };
                }
                else {
                    dep = {
                        depName: app,
                        currentValue: requirement,
                        datasource: hex_1.HexDatasource.id,
                        packageName: organization ? `${app}:${organization}` : app,
                    };
                }
                deps.push(dep);
                depMatchGroups = depMatchRegExp.exec(depBuffer)?.groups;
            }
        }
    }
    const res = { deps };
    const lockFileName = (await (0, fs_1.findLocalSiblingOrParent)(fileName, 'mix.lock')) ?? 'mix.lock';
    // istanbul ignore if
    if (await (0, fs_1.localPathExists)(lockFileName)) {
        res.lockFiles = [lockFileName];
    }
    return res;
}
exports.extractPackageFile = extractPackageFile;
//# sourceMappingURL=extract.js.map