import { logger } from "../../../logger/index.js";
import { joinUrlParts } from "../../../util/url.js";
import { withCache } from "../../../util/cache/package/with-cache.js";
import { Datasource } from "../datasource.js";
import { id } from "../../versioning/hex/index.js";
import { HexRelease } from "./schema.js";

//#region lib/modules/datasource/hex/index.ts
var HexDatasource = class HexDatasource extends Datasource {
	static id = "hex";
	constructor() {
		super(HexDatasource.id);
	}
	defaultRegistryUrls = ["https://hex.pm"];
	defaultVersioning = id;
	releaseTimestampSupport = true;
	releaseTimestampNote = "The release timestamp is determined the `inserted_at` field in the results.";
	sourceUrlSupport = "package";
	sourceUrlNote = "The source URL is determined from the `Github` field in the results.";
	async _getReleases({ packageName, registryUrl }) {
		/* v8 ignore next 3 -- should never happen */
		if (!registryUrl) return null;
		const [hexPackageName, organizationName] = packageName.split(":");
		const hexUrl = joinUrlParts(registryUrl, `/api/${organizationName ? `repos/${organizationName}/` : ""}packages/${hexPackageName}`);
		const { val: result, err } = await this.http.getJsonSafe(hexUrl, HexRelease).onError((err) => {
			logger.warn({
				url: hexUrl,
				datasource: "hex",
				packageName,
				err
			}, "Error fetching from url");
		}).unwrap();
		if (err) this.handleGenericErrors(err);
		return result;
	}
	getReleases(config) {
		return withCache({
			namespace: `datasource-${HexDatasource.id}`,
			key: config.packageName,
			fallback: true
		}, () => this._getReleases(config));
	}
};

//#endregion
export { HexDatasource };
//# sourceMappingURL=index.js.map