import { logger } from "../../../logger/index.js";
import { ensureTrailingSlash, joinUrlParts } from "../../../util/url.js";
import { coerceArray } from "../../../util/array.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 { Timestamp } from "../../../util/timestamp.js";
import { Datasource } from "../datasource.js";
import { datasource, defaultRegistryUrl } from "./common.js";
import { getReleases } from "./prefix-dev.js";

//#region lib/modules/datasource/conda/index.ts
var CondaDatasource = class extends Datasource {
	static id = datasource;
	constructor() {
		super(datasource);
	}
	customRegistrySupport = true;
	registryStrategy = "hunt";
	defaultRegistryUrls = [defaultRegistryUrl];
	caching = true;
	sourceUrlSupport = "package";
	sourceUrlNote = "The source URL is determined from the `dev_url` field in the results.";
	async _getReleases({ registryUrl, packageName }) {
		logger.trace({
			registryUrl,
			packageName
		}, "fetching conda package");
		if (!registryUrl) return null;
		if (registryUrl.startsWith("https://prefix.dev/") || registryUrl.startsWith("https://fast.prefix.dev/")) {
			const channel = ensureTrailingSlash(registryUrl).split("/").at(-2);
			return getReleases(this.http, channel, packageName);
		}
		const url = joinUrlParts(registryUrl, packageName);
		const result = { releases: [] };
		let response;
		try {
			response = await this.http.getJsonUnchecked(url);
			result.homepage = response.body.html_url;
			result.sourceUrl = response.body.dev_url;
			const releaseDate = {};
			for (const file of coerceArray(response.body.files)) releaseDate[file.version] ??= Timestamp.parse(file.upload_time);
			response.body.versions.forEach((version) => {
				const thisRelease = {
					version,
					releaseTimestamp: releaseDate[version]
				};
				result.releases.push(thisRelease);
			});
		} catch (err) {
			if (err instanceof RequestError) {
				if (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 { CondaDatasource };
//# sourceMappingURL=index.js.map