"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractPackageFile = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../../../logger");
const array_1 = require("../../../util/array");
const yaml_1 = require("../../../util/yaml");
const orb_1 = require("../../datasource/orb");
const npmVersioning = tslib_1.__importStar(require("../../versioning/npm"));
const extract_1 = require("../dockerfile/extract");
const schema_1 = require("./schema");
function extractPackageFile(content, packageFile) {
    const deps = [];
    try {
        const parsed = (0, yaml_1.parseSingleYaml)(content, {
            customSchema: schema_1.CircleCiFile,
        });
        for (const [key, orb] of Object.entries(parsed.orbs ?? {})) {
            const [packageName, currentValue] = orb.split('@');
            deps.push({
                depName: key,
                packageName,
                depType: 'orb',
                currentValue,
                versioning: npmVersioning.id,
                datasource: orb_1.OrbDatasource.id,
            });
        }
        for (const job of Object.values(parsed.jobs ?? {})) {
            for (const dockerElement of (0, array_1.coerceArray)(job.docker)) {
                deps.push({
                    ...(0, extract_1.getDep)(dockerElement.image),
                    depType: 'docker',
                });
            }
        }
        for (const alias of (0, array_1.coerceArray)(parsed.aliases)) {
            deps.push({
                ...(0, extract_1.getDep)(alias.image),
                depType: 'docker',
            });
        }
    }
    catch (err) /* istanbul ignore next */ {
        logger_1.logger.debug({ err, packageFile }, 'Error extracting circleci images');
    }
    if (!deps.length) {
        return null;
    }
    return { deps };
}
exports.extractPackageFile = extractPackageFile;
//# sourceMappingURL=extract.js.map