const require_user_agent = require('../../user_agent.cjs');
const require_utils = require('./utils.cjs');

//#region src/endpoint/handlers/discovery.ts
const ENDPOINT_MANIFEST_V2 = "application/vnd.restate.endpointmanifest.v2+json";
const ENDPOINT_MANIFEST_V3 = "application/vnd.restate.endpointmanifest.v3+json";
const ENDPOINT_MANIFEST_V4 = "application/vnd.restate.endpointmanifest.v4+json";
function handleDiscovery(endpoint, protocolMode, additionalDiscoveryFields, acceptVersionsString) {
	if (typeof acceptVersionsString !== "string") {
		const errorMessage = "Missing accept header";
		endpoint.rlog.warn(errorMessage);
		return require_utils.errorResponse(415, errorMessage);
	}
	let manifestVersion;
	if (acceptVersionsString.includes(ENDPOINT_MANIFEST_V4)) manifestVersion = 4;
	else if (acceptVersionsString.includes(ENDPOINT_MANIFEST_V3)) manifestVersion = 3;
	else if (acceptVersionsString.includes(ENDPOINT_MANIFEST_V2)) manifestVersion = 2;
	else {
		const errorMessage = `Unsupported service discovery protocol version '${acceptVersionsString}'`;
		endpoint.rlog.warn(errorMessage);
		return require_utils.errorResponse(415, errorMessage);
	}
	const discovery = {
		...endpoint.discoveryMetadata,
		...additionalDiscoveryFields,
		protocolMode
	};
	const checkUnsupportedFeature = (obj, ...fields) => {
		for (const field of fields) if (field in obj && obj[field] !== void 0) return require_utils.errorResponse(500, `The code uses the new discovery feature '${String(field)}' but the runtime doesn't support it yet (discovery protocol negotiated version ${manifestVersion}). Either remove the usage of this feature, or upgrade the runtime.`);
	};
	if (manifestVersion < 3) for (const service of discovery.services) {
		const error = checkUnsupportedFeature(service, "journalRetention", "idempotencyRetention", "inactivityTimeout", "abortTimeout", "enableLazyState", "ingressPrivate");
		if (error !== void 0) return error;
		for (const handler of service.handlers) {
			const error$1 = checkUnsupportedFeature(handler, "journalRetention", "idempotencyRetention", "workflowCompletionRetention", "inactivityTimeout", "abortTimeout", "enableLazyState", "ingressPrivate");
			if (error$1 !== void 0) return error$1;
		}
	}
	if (manifestVersion < 4) {
		discovery.lambdaCompression = void 0;
		for (const service of discovery.services) {
			const error = checkUnsupportedFeature(service, "retryPolicyExponentiationFactor", "retryPolicyInitialInterval", "retryPolicyMaxAttempts", "retryPolicyMaxInterval", "retryPolicyOnMaxAttempts");
			if (error !== void 0) return error;
			for (const handler of service.handlers) {
				const error$1 = checkUnsupportedFeature(handler, "retryPolicyExponentiationFactor", "retryPolicyInitialInterval", "retryPolicyMaxAttempts", "retryPolicyMaxInterval", "retryPolicyOnMaxAttempts");
				if (error$1 !== void 0) return error$1;
			}
		}
	}
	const body = JSON.stringify(discovery);
	return require_utils.simpleResponse(200, {
		"content-type": manifestVersion === 2 ? ENDPOINT_MANIFEST_V2 : manifestVersion === 3 ? ENDPOINT_MANIFEST_V3 : ENDPOINT_MANIFEST_V4,
		"x-restate-server": require_user_agent.X_RESTATE_SERVER
	}, new TextEncoder().encode(body));
}

//#endregion
exports.handleDiscovery = handleDiscovery;