"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PuppetForgeDatasource = void 0;
const datasource_1 = require("../datasource");
const common_1 = require("./common");
class PuppetForgeDatasource extends datasource_1.Datasource {
    static id = 'puppet-forge';
    constructor() {
        super(PuppetForgeDatasource.id);
    }
    defaultRegistryUrls = [common_1.PUPPET_FORGE];
    async getReleases({ packageName, registryUrl, }) {
        // https://forgeapi.puppet.com
        const moduleSlug = packageName.replace('/', '-');
        // TODO: types (#7154)
        const url = `${registryUrl}/v3/modules/${moduleSlug}?exclude_fields=current_release`;
        let module;
        try {
            const response = await this.http.getJson(url);
            module = response.body;
        }
        catch (err) {
            this.handleGenericErrors(err);
        }
        const releases = module?.releases?.map((release) => ({
            version: release.version,
            downloadUrl: release.file_uri,
            releaseTimestamp: release.created_at,
            registryUrl,
        }));
        if (!releases?.length) {
            return null;
        }
        return PuppetForgeDatasource.createReleaseResult(releases, module);
    }
    static createReleaseResult(releases, module) {
        const result = {
            releases,
            homepage: module.homepage_url,
        };
        if (module.deprecated_for) {
            result.deprecationMessage = module.deprecated_for;
        }
        return result;
    }
}
exports.PuppetForgeDatasource = PuppetForgeDatasource;
//# sourceMappingURL=index.js.map