"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DotnetVersionDatasource = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const decorator_1 = require("../../../util/cache/package/decorator");
const datasource_1 = require("../datasource");
const schema_1 = require("./schema");
class DotnetVersionDatasource extends datasource_1.Datasource {
    constructor() {
        super(DotnetVersionDatasource.id);
        this.caching = true;
        this.customRegistrySupport = false;
        this.defaultRegistryUrls = [
            'https://dotnetcli.blob.core.windows.net/dotnet/release-metadata/releases-index.json',
        ];
    }
    async getReleases({ packageName, }) {
        if (!(packageName === 'dotnet-sdk' || packageName === 'dotnet-runtime')) {
            return null;
        }
        let result = null;
        let raw = null;
        try {
            raw = await this.http.getJson(this.defaultRegistryUrls[0], schema_1.DotnetReleasesIndexSchema);
        }
        catch (err) {
            this.handleGenericErrors(err);
        }
        const body = raw?.body;
        if (body) {
            const releases = [];
            const { 'releases-index': releasesIndex } = body;
            for (const { 'releases.json': releasesUrl } of releasesIndex) {
                const channelReleases = await this.getChannelReleases(releasesUrl, packageName);
                if (channelReleases) {
                    releases.push(...channelReleases);
                }
            }
            const sourceUrl = packageName === 'dotnet-sdk'
                ? 'https://github.com/dotnet/sdk'
                : 'https://github.com/dotnet/runtime';
            result = { releases, sourceUrl };
        }
        return result;
    }
    async getChannelReleases(releaseUrl, packageName) {
        let result = [];
        let raw = null;
        try {
            raw = await this.http.getJson(releaseUrl, schema_1.DotnetReleasesSchema);
        }
        catch (err) {
            this.handleGenericErrors(err);
        }
        const body = raw?.body;
        if (body) {
            const type = DotnetVersionDatasource.getType(packageName);
            const { releases: releases } = body;
            result = releases
                .filter((release) => {
                return !is_1.default.nullOrUndefined(release[type]);
            })
                .map((release) => {
                return {
                    version: release[type].version,
                    releaseTimestamp: release['release-date'],
                    changelogUrl: release['release-notes'],
                };
            });
        }
        return result;
    }
    static getType(packageName) {
        return packageName === 'dotnet-sdk' ? 'sdk' : 'runtime';
    }
}
DotnetVersionDatasource.id = 'dotnet-version';
tslib_1.__decorate([
    (0, decorator_1.cache)({
        namespace: `datasource-${DotnetVersionDatasource.id}`,
        key: ({ packageName }) => packageName,
        ttlMinutes: 1440,
    })
], DotnetVersionDatasource.prototype, "getReleases", null);
tslib_1.__decorate([
    (0, decorator_1.cache)({
        namespace: `datasource-${DotnetVersionDatasource.id}`,
        key: (releaseUrl, packageName) => `${releaseUrl}:${packageName}`,
        ttlMinutes: 1440,
    })
], DotnetVersionDatasource.prototype, "getChannelReleases", null);
exports.DotnetVersionDatasource = DotnetVersionDatasource;
//# sourceMappingURL=index.js.map