"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPackageFile = 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 regex_1 = require("../../../util/regex");
const git_tags_1 = require("../../datasource/git-tags");
const helm_1 = require("../../datasource/helm");
const util_1 = require("../terraform/util");
function extractGitRepo(doc) {
    const dep = {
        depType: 'git_repo',
        datasource: git_tags_1.GitTagsDatasource.id,
    };
    const repo = doc.spec?.repo;
    if (!repo) {
        return {
            ...dep,
            skipReason: 'missing-depname',
        };
    }
    dep.sourceUrl = repo;
    dep.depName = repo;
    const currentValue = doc.spec.revision;
    if (!currentValue) {
        return {
            ...dep,
            skipReason: 'no-version',
        };
    }
    return {
        ...dep,
        currentValue,
    };
}
function extractFleetHelmBlock(doc) {
    const dep = {
        depType: 'fleet',
        datasource: helm_1.HelmDatasource.id,
    };
    if (!doc.chart) {
        return {
            ...dep,
            skipReason: 'missing-depname',
        };
    }
    dep.depName = doc.chart;
    dep.packageName = doc.chart;
    if (!doc.repo) {
        if ((0, util_1.checkIfStringIsPath)(doc.chart)) {
            return {
                ...dep,
                skipReason: 'local-chart',
            };
        }
        return {
            ...dep,
            skipReason: 'no-repository',
        };
    }
    dep.registryUrls = [doc.repo];
    const currentValue = doc.version;
    if (!doc.version) {
        return {
            ...dep,
            skipReason: 'no-version',
        };
    }
    return {
        ...dep,
        currentValue,
    };
}
function extractFleetFile(doc) {
    const result = [];
    result.push(extractFleetHelmBlock(doc.helm));
    if (!is_1.default.undefined(doc.targetCustomizations)) {
        for (const custom of doc.targetCustomizations) {
            const dep = extractFleetHelmBlock({
                // merge base config with customization
                ...doc.helm,
                ...custom.helm,
            });
            result.push({
                // overwrite name with customization name to allow splitting of PRs
                ...dep,
                depName: custom.name,
            });
        }
    }
    return result;
}
function extractPackageFile(content, packageFile) {
    if (!content) {
        return null;
    }
    const deps = [];
    try {
        if ((0, regex_1.regEx)('fleet.ya?ml').test(packageFile)) {
            // TODO: fix me (#9610)
            const docs = (0, js_yaml_1.loadAll)(content, null, { json: true });
            const fleetDeps = docs
                .filter((doc) => is_1.default.truthy(doc?.helm))
                .flatMap((doc) => extractFleetFile(doc));
            deps.push(...fleetDeps);
        }
        else {
            // TODO: fix me (#9610)
            const docs = (0, js_yaml_1.loadAll)(content, null, { json: true });
            const gitRepoDeps = docs
                .filter((doc) => doc.kind === 'GitRepo') // ensure only GitRepo manifests are processed
                .flatMap((doc) => extractGitRepo(doc));
            deps.push(...gitRepoDeps);
        }
    }
    catch (err) {
        logger_1.logger.warn({ error: err, packageFile }, 'Failed to parse fleet YAML');
    }
    return deps.length ? { deps } : null;
}
exports.extractPackageFile = extractPackageFile;
//# sourceMappingURL=extract.js.map