"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractAllPackageFiles = exports.extractPackageFile = exports.extractFromJob = exports.extractFromServices = exports.extractFromImage = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const js_yaml_1 = require("js-yaml");
const logger_1 = require("../../../logger");
const fs_1 = require("../../../util/fs");
const url_1 = require("../../../util/url");
const common_1 = require("./common");
const utils_1 = require("./utils");
function extractFromImage(image, registryAliases) {
    if (is_1.default.undefined(image)) {
        return null;
    }
    let dep = null;
    if (is_1.default.string(image)) {
        dep = (0, utils_1.getGitlabDep)(image, registryAliases);
        dep.depType = 'image';
    }
    else if (is_1.default.string(image?.name)) {
        dep = (0, utils_1.getGitlabDep)(image.name, registryAliases);
        dep.depType = 'image-name';
    }
    return dep;
}
exports.extractFromImage = extractFromImage;
function extractFromServices(services, registryAliases) {
    if (is_1.default.undefined(services)) {
        return [];
    }
    const deps = [];
    for (const service of services) {
        if (is_1.default.string(service)) {
            const dep = (0, utils_1.getGitlabDep)(service, registryAliases);
            dep.depType = 'service-image';
            deps.push(dep);
        }
        else if (is_1.default.string(service?.name)) {
            const dep = (0, utils_1.getGitlabDep)(service.name, registryAliases);
            dep.depType = 'service-image';
            deps.push(dep);
        }
    }
    return deps;
}
exports.extractFromServices = extractFromServices;
function extractFromJob(job, registryAliases) {
    if (is_1.default.undefined(job)) {
        return [];
    }
    const deps = [];
    if (is_1.default.object(job)) {
        const { image, services } = { ...job };
        if (is_1.default.object(image) || is_1.default.string(image)) {
            const dep = extractFromImage(image, registryAliases);
            if (dep) {
                deps.push(dep);
            }
        }
        if (is_1.default.array(services)) {
            deps.push(...extractFromServices(services, registryAliases));
        }
    }
    return deps;
}
exports.extractFromJob = extractFromJob;
function extractPackageFile(content, _fileName, config) {
    let deps = [];
    try {
        const doc = (0, js_yaml_1.load)((0, utils_1.replaceReferenceTags)(content), {
            json: true,
        });
        if (is_1.default.object(doc)) {
            for (const [property, value] of Object.entries(doc)) {
                switch (property) {
                    case 'image':
                        {
                            const dep = extractFromImage(value, config.registryAliases);
                            if (dep) {
                                deps.push(dep);
                            }
                        }
                        break;
                    case 'services':
                        deps.push(...extractFromServices(value, config.registryAliases));
                        break;
                    default:
                        deps.push(...extractFromJob(value, config.registryAliases));
                        break;
                }
            }
            deps = deps.filter(is_1.default.truthy);
        }
    }
    catch (err) /* istanbul ignore next */ {
        logger_1.logger.warn({ err }, 'Error extracting GitLab CI dependencies');
    }
    return deps.length ? { deps } : null;
}
exports.extractPackageFile = extractPackageFile;
async function extractAllPackageFiles(config, packageFiles) {
    const filesToExamine = [...packageFiles];
    const seen = new Set(packageFiles);
    const results = [];
    // extract all includes from the files
    while (filesToExamine.length > 0) {
        const file = filesToExamine.pop();
        const content = await (0, fs_1.readLocalFile)(file, 'utf8');
        if (!content) {
            logger_1.logger.debug(`Empty or non existent gitlabci file ${file}`);
            continue;
        }
        let doc;
        try {
            doc = (0, js_yaml_1.load)((0, utils_1.replaceReferenceTags)(content), {
                json: true,
            });
        }
        catch (err) {
            logger_1.logger.warn({ err, file }, 'Error extracting GitLab CI dependencies');
            continue;
        }
        if (is_1.default.array(doc?.include)) {
            for (const includeObj of doc.include.filter(common_1.isGitlabIncludeLocal)) {
                const fileObj = (0, url_1.trimLeadingSlash)(includeObj.local);
                if (!seen.has(fileObj)) {
                    seen.add(fileObj);
                    filesToExamine.push(fileObj);
                }
            }
        }
        else if (is_1.default.string(doc?.include)) {
            const fileObj = (0, url_1.trimLeadingSlash)(doc.include);
            if (!seen.has(fileObj)) {
                seen.add(fileObj);
                filesToExamine.push(fileObj);
            }
        }
        const result = extractPackageFile(content, file, config);
        if (result !== null) {
            results.push({
                packageFile: file,
                deps: result.deps,
            });
        }
    }
    logger_1.logger.trace({ packageFiles, files: filesToExamine.entries() }, 'extracted all GitLab CI files');
    if (!results.length) {
        return null;
    }
    return results;
}
exports.extractAllPackageFiles = extractAllPackageFiles;
//# sourceMappingURL=extract.js.map