"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GlasskubePackagesDatasource = void 0;
const tslib_1 = require("tslib");
const decorator_1 = require("../../../util/cache/package/decorator");
const url_1 = require("../../../util/url");
const glasskubeVersioning = tslib_1.__importStar(require("../../versioning/glasskube"));
const datasource_1 = require("../datasource");
const schema_1 = require("./schema");
class GlasskubePackagesDatasource extends datasource_1.Datasource {
    static id = 'glasskube-packages';
    static defaultRegistryUrl = 'https://packages.dl.glasskube.dev/packages';
    customRegistrySupport = true;
    defaultVersioning = glasskubeVersioning.id;
    defaultRegistryUrls = [
        GlasskubePackagesDatasource.defaultRegistryUrl,
    ];
    constructor() {
        super(GlasskubePackagesDatasource.id);
    }
    async getReleases({ packageName, registryUrl, }) {
        let versions;
        const result = { releases: [] };
        try {
            const response = await this.http.get((0, url_1.joinUrlParts)(registryUrl, packageName, 'versions.yaml'));
            versions = schema_1.GlasskubePackageVersionsYaml.parse(response.body);
        }
        catch (err) {
            this.handleGenericErrors(err);
        }
        result.releases = versions.versions.map((it) => ({
            version: it.version,
        }));
        result.tags = { latest: versions.latestVersion };
        try {
            const response = await this.http.get((0, url_1.joinUrlParts)(registryUrl, packageName, versions.latestVersion, 'package.yaml'));
            const latestManifest = schema_1.GlasskubePackageManifestYaml.parse(response.body);
            for (const ref of latestManifest?.references ?? []) {
                if (ref.label.toLowerCase() === 'github') {
                    result.sourceUrl = ref.url;
                }
                else if (ref.label.toLowerCase() === 'website') {
                    result.homepage = ref.url;
                }
            }
        }
        catch (err) {
            this.handleGenericErrors(err);
        }
        return result;
    }
}
exports.GlasskubePackagesDatasource = GlasskubePackagesDatasource;
tslib_1.__decorate([
    (0, decorator_1.cache)({
        namespace: `datasource-${GlasskubePackagesDatasource.id}`,
        key: ({ registryUrl, packageName }) => `${registryUrl}:${packageName}`,
    })
], GlasskubePackagesDatasource.prototype, "getReleases", null);
//# sourceMappingURL=index.js.map