import { logger } from "../../../logger/index.js";
import { isHttpUrl } from "../../../util/url.js";
import { withCache } from "../../../util/cache/package/with-cache.js";
import { id } from "../../versioning/hashicorp/index.js";
import { ProtocolModuleResponse, TerraformModuleResponse } from "./schema.js";
import { TerraformDatasource } from "./base.js";
import { createSDBackendURL, getRegistryRepository } from "./utils.js";
//#region lib/modules/datasource/terraform-module/index.ts
var TerraformModuleDatasource = class TerraformModuleDatasource extends TerraformDatasource {
	static id = "terraform-module";
	static terraformCloudUrl = "https://app.terraform.io";
	static defaultRegistryUrls = [TerraformModuleDatasource.terraformRegistryUrl];
	static extendedApiRegistryUrls = [TerraformModuleDatasource.terraformRegistryUrl, TerraformModuleDatasource.terraformCloudUrl];
	constructor() {
		super(TerraformModuleDatasource.id);
	}
	defaultRegistryUrls = TerraformModuleDatasource.defaultRegistryUrls;
	defaultVersioning = id;
	releaseTimestampSupport = true;
	releaseTimestampNote = "The release timestamp is only supported for the latest version, and is determined from the `published_at` field in the results.";
	sourceUrlSupport = "package";
	sourceUrlNote = "The source URL is determined from the the `source` field in the results.";
	/**
	* Resolves a module release list for the configured registry.
	*
	* Requests against the public Terraform registry and Terraform Cloud use the
	* registry-specific module endpoint, while other registries use the generic
	* Module Registry Protocol versions endpoint.
	*/
	async _getReleases({ packageName, registryUrl }) {
		/* v8 ignore next 3 -- should never happen */
		if (!registryUrl) return null;
		const { registry, repository } = getRegistryRepository(packageName, registryUrl);
		logger.trace({
			registryUrlNormalized: registry,
			terraformRepository: repository
		}, "terraform-module.getReleases()");
		try {
			if (TerraformModuleDatasource.extendedApiRegistryUrls.includes(registry)) return await this.queryTerraformRegistry(registry, repository);
			return await this.queryModuleRegistry(registry, repository);
		} catch (err) {
			this.handleGenericErrors(err);
		}
	}
	getReleases(config) {
		return withCache({
			namespace: `datasource-${TerraformModuleDatasource.id}`,
			key: TerraformModuleDatasource.getCacheKey(config),
			fallback: true
		}, () => this._getReleases(config));
	}
	/**
	* Queries the Terraform Registry module endpoint.
	*
	* The response includes the latest published version separately, so only that
	* release can be annotated with `releaseTimestamp`.
	*
	* https://developer.hashicorp.com/terraform/registry/api-docs#get-a-specific-module
	*/
	async queryTerraformRegistry(registryUrl, repository) {
		const pkgUrl = createSDBackendURL(registryUrl, "modules.v1", await this.getTerraformServiceDiscoveryResult(registryUrl), repository);
		const { body: res } = await this.http.getJson(pkgUrl, TerraformModuleResponse);
		return {
			releases: res.versions,
			sourceUrl: res.source,
			homepage: `${registryUrl}/modules/${repository}`
		};
	}
	/**
	* Queries a registry through the Terraform Module Registry Protocol.
	*
	* This is the default path for registries implementing the standard module
	* registry protocol. It returns release versions and, when present and valid,
	* the upstream source URL.
	*
	* https://developer.hashicorp.com/terraform/internals/module-registry-protocol
	*/
	async queryModuleRegistry(registryUrl, repository) {
		const pkgUrl = createSDBackendURL(registryUrl, "modules.v1", await this.getTerraformServiceDiscoveryResult(registryUrl), `${repository}/versions`);
		const { body: res } = await this.http.getJson(pkgUrl, ProtocolModuleResponse);
		return {
			releases: res.versions,
			sourceUrl: isHttpUrl(res.source) ? res.source : void 0
		};
	}
	static getCacheKey({ packageName, registryUrl }) {
		const { registry, repository } = getRegistryRepository(packageName, registryUrl);
		return `${registry}/${repository}`;
	}
};
//#endregion
export { TerraformModuleDatasource };

//# sourceMappingURL=index.js.map