"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HelmReleaseExtractor = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const logger_1 = require("../../../../../logger");
const docker_1 = require("../../../../datasource/docker");
const helm_1 = require("../../../../datasource/helm");
const utils_1 = require("../../../helmv3/utils");
const base_1 = require("../../base");
const util_1 = require("../../util");
class HelmReleaseExtractor extends base_1.DependencyExtractor {
    getCheckList() {
        return [`"helm_release"`];
    }
    extract(hclMap) {
        const dependencies = [];
        const helmReleases = hclMap?.resource?.helm_release;
        if (is_1.default.nullOrUndefined(helmReleases)) {
            return [];
        }
        // istanbul ignore if
        if (!is_1.default.plainObject(helmReleases)) {
            logger_1.logger.debug({ helmReleases }, 'Terraform: unexpected `helmReleases` value');
            return [];
        }
        for (const helmRelease of Object.values(helmReleases).flat()) {
            const dep = {
                currentValue: helmRelease.version,
                depType: 'helm_release',
                depName: helmRelease.chart,
                datasource: helm_1.HelmDatasource.id,
            };
            if (is_1.default.nonEmptyString(helmRelease.repository)) {
                if ((0, utils_1.isOCIRegistry)(helmRelease.repository)) {
                    // For oci repos, we remove the oci:// and use the docker datasource
                    dep.registryUrls = [
                        helmRelease.repository.replace('oci://', 'https://'),
                    ];
                    dep.datasource = docker_1.DockerDatasource.id;
                }
                else {
                    dep.registryUrls = [helmRelease.repository];
                }
            }
            if (!helmRelease.chart) {
                dep.skipReason = 'invalid-name';
            }
            else if ((0, utils_1.isOCIRegistry)(helmRelease.chart)) {
                // For oci charts, we remove the oci:// and use the docker datasource
                dep.depName = helmRelease.chart.replace('oci://', '');
                dep.datasource = docker_1.DockerDatasource.id;
            }
            else if ((0, util_1.checkIfStringIsPath)(helmRelease.chart)) {
                dep.skipReason = 'local-chart';
            }
            dependencies.push(dep);
        }
        return dependencies;
    }
}
exports.HelmReleaseExtractor = HelmReleaseExtractor;
//# sourceMappingURL=helm-release.js.map