"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPackageFile = void 0;
const logger_1 = require("../../../logger");
const common_1 = require("./common");
const parser_1 = require("./parser");
function extractPackageFile(content, packageFile) {
    const deps = [];
    let parsed = null;
    try {
        parsed = (0, parser_1.parse)(content);
    }
    catch (err) /* istanbul ignore next */ {
        logger_1.logger.debug({ err, packageFile }, 'Bazel parsing error');
    }
    if (!parsed) {
        return null;
    }
    const { targets, meta: meta } = parsed;
    for (let idx = 0; idx < targets.length; idx += 1) {
        const target = targets[idx];
        const dep = (0, common_1.extractDepFromTarget)(target);
        if (!dep) {
            continue;
        }
        const def = (0, common_1.getRuleDefinition)(content, meta, idx);
        // istanbul ignore if: should not happen
        if (!def) {
            logger_1.logger.warn({ dep }, `Bazel: can't extract definition fragment`);
            continue;
        }
        dep.managerData = { def };
        deps.push(dep);
    }
    return deps.length ? { deps } : null;
}
exports.extractPackageFile = extractPackageFile;
//# sourceMappingURL=extract.js.map