"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AzureBicepResourceDatasource = void 0;
const tslib_1 = require("tslib");
const decorator_1 = require("../../../util/cache/package/decorator");
const azureRestApiVersioningApi = tslib_1.__importStar(require("../../versioning/azure-rest-api"));
const datasource_1 = require("../datasource");
const schema_1 = require("./schema");
const BICEP_TYPES_INDEX_URL = 'https://raw.githubusercontent.com/Azure/bicep-types-az/main/generated/index.json';
class AzureBicepResourceDatasource extends datasource_1.Datasource {
    static id = 'azure-bicep-resource';
    defaultConfig = {
        commitMessageTopic: 'resource {{depName}}',
        commitMessageExtra: 'to {{{newVersion}}}',
        prBodyColumns: ['Resource', 'Change'],
        prBodyDefinitions: {
            Resource: '{{{depNameLinked}}}',
        },
    };
    defaultVersioning = azureRestApiVersioningApi.id;
    constructor() {
        super(AzureBicepResourceDatasource.id);
    }
    async getReleases(getReleasesConfig) {
        const { packageName } = getReleasesConfig;
        const resourceVersionIndex = await this.getResourceVersionIndex();
        const versions = resourceVersionIndex[packageName.toLowerCase()];
        if (!versions) {
            return null;
        }
        const firstSlashIndex = packageName.indexOf('/');
        const namespaceProvider = packageName
            .slice(0, firstSlashIndex)
            .toLowerCase();
        const type = packageName.slice(firstSlashIndex + 1).toLowerCase();
        return {
            releases: versions.map((version) => ({
                version,
                changelogUrl: `https://learn.microsoft.com/en-us/azure/templates/${namespaceProvider}/change-log/${type}#${version}`,
            })),
        };
    }
    async getResourceVersionIndex() {
        const res = await this.getBicepTypeIndex();
        const releaseMap = new Map();
        for (const resourceReference of Object.keys(res.Resources)) {
            const [type, version] = resourceReference.toLowerCase().split('@', 2);
            const versions = releaseMap.get(type) ?? [];
            versions.push(version);
            releaseMap.set(type, versions);
        }
        for (const functionResource of Object.entries(res.Functions)) {
            const [type, versionMap] = functionResource;
            const versions = Object.keys(versionMap);
            releaseMap.set(type, versions);
        }
        return Object.fromEntries(releaseMap);
    }
    async getBicepTypeIndex() {
        const res = await this.http.getJson(BICEP_TYPES_INDEX_URL, schema_1.BicepTypeIndex);
        return res.body;
    }
}
tslib_1.__decorate([
    (0, decorator_1.cache)({
        namespace: `datasource-${AzureBicepResourceDatasource.id}`,
        key: ({ packageName }) => `getReleases-${packageName}`,
    })
], AzureBicepResourceDatasource.prototype, "getReleases", null);
tslib_1.__decorate([
    (0, decorator_1.cache)({
        namespace: `datasource-${AzureBicepResourceDatasource.id}`,
        key: 'getResourceVersionIndex',
        ttlMinutes: 24 * 60,
    })
], AzureBicepResourceDatasource.prototype, "getResourceVersionIndex", null);
exports.AzureBicepResourceDatasource = AzureBicepResourceDatasource;
//# sourceMappingURL=index.js.map