"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RequiredProviderExtractor = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const base_1 = require("../../base");
class RequiredProviderExtractor extends base_1.TerraformProviderExtractor {
    getCheckList() {
        return ['required_providers'];
    }
    extract(hclRoot, locks) {
        const terraformBlocks = hclRoot?.terraform;
        if (is_1.default.nullOrUndefined(terraformBlocks)) {
            return [];
        }
        const dependencies = [];
        for (const terraformBlock of terraformBlocks) {
            const requiredProviders = terraformBlock.required_providers;
            if (is_1.default.nullOrUndefined(requiredProviders)) {
                continue;
            }
            for (const requiredProvidersMap of requiredProviders) {
                for (const requiredProviderName of Object.keys(requiredProvidersMap)) {
                    const value = requiredProvidersMap[requiredProviderName];
                    // name = version declaration method
                    let dep;
                    if (typeof value === 'string') {
                        dep = {
                            currentValue: value,
                            managerData: {
                                moduleName: requiredProviderName,
                            },
                        };
                    }
                    // block declaration aws = { source = 'aws', version = '2.0.0' }
                    dep ?? (dep = {
                        currentValue: value['version'],
                        managerData: {
                            moduleName: requiredProviderName,
                            source: value['source'],
                        },
                    });
                    const massagedDep = this.analyzeTerraformProvider(dep, locks, 'required_provider');
                    dependencies.push(massagedDep);
                }
            }
        }
        return dependencies;
    }
}
exports.RequiredProviderExtractor = RequiredProviderExtractor;
//# sourceMappingURL=required-provider.js.map