"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPackageFile = exports.parseVendir = exports.extractHelmChart = void 0;
const logger_1 = require("../../../logger");
const yaml_1 = require("../../../util/yaml");
const helm_1 = require("../../datasource/helm");
const extract_1 = require("../dockerfile/extract");
const utils_1 = require("../helmv3/utils");
const schema_1 = require("./schema");
// TODO: Add support for other vendir types (like git tags, github releases, etc.)
// Recommend looking at the kustomize manager for more information on support.
function extractHelmChart(helmChart, aliases) {
    if ((0, utils_1.isOCIRegistry)(helmChart.repository.url)) {
        const dep = (0, extract_1.getDep)(`${helmChart.repository.url.replace('oci://', '')}/${helmChart.name}:${helmChart.version}`, false, aliases);
        return {
            ...dep,
            depName: helmChart.name,
            packageName: dep.depName,
            // https://github.com/helm/helm/issues/10312
            // https://github.com/helm/helm/issues/10678
            pinDigests: false,
        };
    }
    return {
        depName: helmChart.name,
        currentValue: helmChart.version,
        registryUrls: [helmChart.repository.url],
        datasource: helm_1.HelmDatasource.id,
    };
}
exports.extractHelmChart = extractHelmChart;
function parseVendir(content, packageFile) {
    try {
        return (0, yaml_1.parseSingleYaml)(content, {
            customSchema: schema_1.Vendir,
            removeTemplates: true,
        });
    }
    catch (e) {
        logger_1.logger.debug({ packageFile }, 'Error parsing vendir.yml file');
        return null;
    }
}
exports.parseVendir = parseVendir;
function extractPackageFile(content, packageFile, config) {
    logger_1.logger.trace(`vendir.extractPackageFile(${packageFile})`);
    const deps = [];
    const pkg = parseVendir(content, packageFile);
    if (!pkg) {
        return null;
    }
    // grab the helm charts
    const contents = pkg.directories.flatMap((directory) => directory.contents);
    for (const content of contents) {
        const dep = extractHelmChart(content.helmChart, config.registryAliases);
        if (dep) {
            deps.push({
                ...dep,
                depType: 'HelmChart',
            });
        }
    }
    if (!deps.length) {
        return null;
    }
    return { deps };
}
exports.extractPackageFile = extractPackageFile;
//# sourceMappingURL=extract.js.map