"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPackageFile = void 0;
const js_yaml_1 = require("js-yaml");
const logger_1 = require("../../../logger");
const regex_1 = require("../../../util/regex");
const extract_1 = require("../dockerfile/extract");
function extractPackageFile(content) {
    logger_1.logger.trace('kubernetes.extractPackageFile()');
    const isKubernetesManifest = (0, regex_1.regEx)(/\s*apiVersion\s*:/).test(content) &&
        (0, regex_1.regEx)(/\s*kind\s*:/).test(content);
    if (!isKubernetesManifest) {
        return null;
    }
    const deps = [
        ...extractImages(content),
        ...extractApis(content),
    ];
    return deps.length ? { deps } : null;
}
exports.extractPackageFile = extractPackageFile;
function extractImages(content) {
    const deps = [];
    for (const line of content.split(regex_1.newlineRegex)) {
        const match = (0, regex_1.regEx)(/^\s*-?\s*image:\s*'?"?([^\s'"]+)'?"?\s*$/).exec(line);
        if (match) {
            const currentFrom = match[1];
            const dep = (0, extract_1.getDep)(currentFrom);
            logger_1.logger.debug({
                depName: dep.depName,
                currentValue: dep.currentValue,
                currentDigest: dep.currentDigest,
            }, 'Kubernetes image');
            deps.push(dep);
        }
    }
    return deps.filter((dep) => !dep.currentValue?.includes('${'));
}
function extractApis(content) {
    let doc;
    try {
        doc = (0, js_yaml_1.loadAll)(content);
    }
    catch (err) {
        logger_1.logger.debug({ err, content }, 'Failed to parse Kubernetes configuration.');
        return [];
    }
    return doc.map((configuration) => ({
        depName: configuration.kind,
        currentValue: configuration.apiVersion,
    }));
}
//# sourceMappingURL=extract.js.map