"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 logger_1 = require("../../../logger");
const extractors_1 = require("./extractors");
const hcl = tslib_1.__importStar(require("./hcl"));
const util_1 = require("./util");
async function extractPackageFile(content, fileName, config) {
    logger_1.logger.trace({ content }, 'terraform.extractPackageFile()');
    const passedExtractors = [];
    for (const extractor of extractors_1.resourceExtractors) {
        if ((0, util_1.checkFileContainsDependency)(content, extractor.getCheckList())) {
            passedExtractors.push(extractor);
        }
    }
    if (!passedExtractors.length) {
        logger_1.logger.trace({ fileName }, 'preflight content check has not found any relevant content');
        return null;
    }
    logger_1.logger.trace({ fileName }, `preflight content check passed for extractors: [${passedExtractors
        .map((value) => value.constructor.name)
        .toString()}]`);
    const dependencies = [];
    const hclMap = hcl.parseHCL(content);
    if (is_1.default.nullOrUndefined(hclMap)) {
        logger_1.logger.trace({ fileName }, 'failed to parse HCL file');
        return null;
    }
    const locks = await (0, util_1.extractLocksForPackageFile)(fileName);
    for (const extractor of passedExtractors) {
        const deps = extractor.extract(hclMap, locks);
        dependencies.push(...deps);
    }
    dependencies.forEach((value) => delete value.managerData);
    return { deps: dependencies };
}
exports.extractPackageFile = extractPackageFile;
//# sourceMappingURL=extract.js.map