import { logger } from "../../../logger/index.js";
import { ExternalHostError } from "../../../types/errors/external-host-error.js";
import { withCache } from "../../../util/cache/package/with-cache.js";
import { RequestError } from "../../../util/http/got.js";
import "../../../util/http/index.js";
import { Datasource } from "../datasource.js";
import { datasource, defaultRegistryUrl, parsePackage } from "./common.js";
import { AdoptiumJavaResponse } from "./schema.js";
//#region lib/modules/datasource/java-version/index.ts
var JavaVersionDatasource = class extends Datasource {
	static id = datasource;
	constructor() {
		super(datasource);
	}
	customRegistrySupport = false;
	defaultRegistryUrls = [defaultRegistryUrl];
	caching = true;
	async getPageReleases(url, page) {
		const pgUrl = `${url}&page=${page}`;
		try {
			return (await this.http.getJson(pgUrl, AdoptiumJavaResponse))?.body?.versions?.map(({ semver }) => ({ version: semver })) ?? null;
		} catch (err) {
			if (page !== 0 && err instanceof RequestError && err.response?.statusCode === 404) return null;
			throw err;
		}
	}
	async _getReleases({ registryUrl, packageName }) {
		const pkgConfig = parsePackage(packageName);
		logger.trace({
			registryUrl,
			packageName,
			pkgConfig
		}, "fetching java release");
		let url = `${registryUrl}v3/info/release_versions?page_size=50&image_type=${pkgConfig.imageType}&project=jdk&release_type=ga&sort_method=DATE&sort_order=DESC`;
		if (pkgConfig.architecture) url += `&architecture=${pkgConfig.architecture}`;
		if (pkgConfig.os) url += `&os=${pkgConfig.os}`;
		const result = {
			homepage: "https://adoptium.net",
			releases: []
		};
		try {
			let page = 0;
			let releases = await this.getPageReleases(url, page);
			while (releases) {
				result.releases.push(...releases);
				if (releases.length !== 50 || page >= 50) break;
				page += 1;
				releases = await this.getPageReleases(url, page);
			}
		} catch (err) {
			if (err instanceof RequestError && err.response?.statusCode !== 404) throw new ExternalHostError(err);
			this.handleGenericErrors(err);
		}
		return result.releases.length ? result : null;
	}
	getReleases(config) {
		return withCache({
			namespace: `datasource-${datasource}`,
			key: `${config.registryUrl}:${config.packageName}`,
			fallback: true
		}, () => this._getReleases(config));
	}
};
//#endregion
export { JavaVersionDatasource };

//# sourceMappingURL=index.js.map