const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_rpc = require('../types/rpc.cjs');
let __restatedev_restate_sdk_core = require("@restatedev/restate-sdk-core");
__restatedev_restate_sdk_core = require_rolldown_runtime.__toESM(__restatedev_restate_sdk_core);

//#region src/endpoint/components.ts
function handlerInputDiscovery(handler, defaultSerde) {
	const serde$1 = handler.options?.input ?? defaultSerde;
	let contentType = void 0;
	let jsonSchema = void 0;
	if (serde$1.jsonSchema) {
		jsonSchema = serde$1.jsonSchema;
		contentType = handler.options?.accept ?? serde$1.contentType;
	} else if (handler.options?.accept) contentType = handler.options?.accept;
	else if (serde$1.contentType) contentType = serde$1.contentType;
	else return {};
	return {
		required: false,
		contentType,
		jsonSchema
	};
}
function handlerOutputDiscovery(handler, defaultSerde) {
	const serde$1 = handler.options?.output ?? defaultSerde;
	const setContentTypeIfEmpty = handler.options?.setOutputContentTypeIfEmpty ?? false;
	let contentType = void 0;
	let jsonSchema = void 0;
	if (serde$1.jsonSchema) {
		jsonSchema = serde$1.jsonSchema;
		contentType = serde$1.contentType ?? "application/json";
	} else if (serde$1.contentType) contentType = serde$1.contentType;
	else return { setContentTypeIfEmpty };
	return {
		setContentTypeIfEmpty,
		jsonSchema,
		contentType
	};
}
function createExecutionOptions(serviceOptions, handlerOptions) {
	const hooks = [...serviceOptions?.hooks ?? [], ...handlerOptions?.hooks ?? []];
	return {
		defaultSerde: handlerOptions?.serde ?? serviceOptions?.serde,
		asTerminalError: handlerOptions?.asTerminalError ?? serviceOptions?.asTerminalError,
		hooks: hooks.length > 0 ? hooks : void 0,
		explicitCancellation: handlerOptions?.explicitCancellation ?? serviceOptions?.explicitCancellation
	};
}
var ServiceComponent = class {
	handlers = /* @__PURE__ */ new Map();
	constructor(componentName, description, metadata, options) {
		this.componentName = componentName;
		this.description = description;
		this.metadata = metadata;
		this.options = options;
	}
	name() {
		return this.componentName;
	}
	add(name, handlerWrapper) {
		this.handlers.set(name, new ServiceHandler(name, handlerWrapper, this));
	}
	discovery() {
		const handlers = [...this.handlers.entries()].map(([name, handler]) => {
			return {
				name,
				...commonHandlerOptions(handler.handlerWrapper, handler.executionOptions.defaultSerde ?? __restatedev_restate_sdk_core.serde.json)
			};
		});
		return {
			name: this.componentName,
			ty: "SERVICE",
			handlers,
			documentation: this.description,
			metadata: this.metadata,
			...commonServiceOptions(this.options)
		};
	}
	handlerMatching(url) {
		return this.handlers.get(url.handlerName);
	}
};
var ServiceHandler = class {
	executionOptions;
	constructor(handlerName, handlerWrapper, parent) {
		this.handlerName = handlerName;
		this.handlerWrapper = handlerWrapper;
		this.parent = parent;
		this.executionOptions = createExecutionOptions(this.parent.options, handlerWrapper.options);
	}
	name() {
		return this.handlerName;
	}
	component() {
		return this.parent;
	}
	kind() {
		return this.handlerWrapper.kind;
	}
	invoke(context, input) {
		return this.handlerWrapper.invoke(context, input);
	}
};
var VirtualObjectComponent = class {
	handlers = /* @__PURE__ */ new Map();
	constructor(componentName, description, metadata, options) {
		this.componentName = componentName;
		this.description = description;
		this.metadata = metadata;
		this.options = options;
	}
	name() {
		return this.componentName;
	}
	add(name, wrapper) {
		this.handlers.set(name, new VirtualObjectHandler(name, wrapper, this));
	}
	discovery() {
		const handlers = [...this.handlers.entries()].map(([name, handler]) => {
			return {
				name,
				ty: handler.kind() === require_rpc.HandlerKind.EXCLUSIVE ? "EXCLUSIVE" : "SHARED",
				...commonHandlerOptions(handler.handlerWrapper, handler.executionOptions.defaultSerde ?? __restatedev_restate_sdk_core.serde.json)
			};
		});
		return {
			name: this.componentName,
			ty: "VIRTUAL_OBJECT",
			handlers,
			documentation: this.description,
			metadata: this.metadata,
			...commonServiceOptions(this.options)
		};
	}
	handlerMatching(url) {
		return this.handlers.get(url.handlerName);
	}
};
var VirtualObjectHandler = class {
	executionOptions;
	constructor(handlerName, handlerWrapper, parent) {
		this.handlerName = handlerName;
		this.handlerWrapper = handlerWrapper;
		this.parent = parent;
		this.executionOptions = createExecutionOptions(this.parent.options, handlerWrapper.options);
	}
	name() {
		return this.handlerName;
	}
	component() {
		return this.parent;
	}
	kind() {
		return this.handlerWrapper.kind;
	}
	invoke(context, input) {
		return this.handlerWrapper.invoke(context, input);
	}
};
var WorkflowComponent = class {
	handlers = /* @__PURE__ */ new Map();
	constructor(componentName, description, metadata, options) {
		this.componentName = componentName;
		this.description = description;
		this.metadata = metadata;
		this.options = options;
	}
	name() {
		return this.componentName;
	}
	add(name, wrapper) {
		this.handlers.set(name, new WorkflowHandler(name, wrapper, this));
	}
	discovery() {
		const handlers = [...this.handlers.entries()].map(([name, handler]) => {
			return {
				name,
				ty: handler.kind() === require_rpc.HandlerKind.WORKFLOW ? "WORKFLOW" : "SHARED",
				workflowCompletionRetention: handler.kind() === require_rpc.HandlerKind.WORKFLOW && this.options?.workflowRetention !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(this.options?.workflowRetention) : void 0,
				...commonHandlerOptions(handler.handlerWrapper, handler.executionOptions.defaultSerde ?? __restatedev_restate_sdk_core.serde.json)
			};
		});
		return {
			name: this.componentName,
			ty: "WORKFLOW",
			handlers,
			documentation: this.description,
			metadata: this.metadata,
			...commonServiceOptions(this.options)
		};
	}
	handlerMatching(url) {
		return this.handlers.get(url.handlerName);
	}
};
var WorkflowHandler = class {
	executionOptions;
	constructor(handlerName, handlerWrapper, parent) {
		this.handlerName = handlerName;
		this.handlerWrapper = handlerWrapper;
		this.parent = parent;
		this.executionOptions = createExecutionOptions(this.parent.options, handlerWrapper.options);
	}
	name() {
		return this.handlerName;
	}
	component() {
		return this.parent;
	}
	kind() {
		return this.handlerWrapper.kind;
	}
	invoke(context, input) {
		return this.handlerWrapper.invoke(context, input);
	}
};
function parseUrlComponents(urlPath) {
	if (!urlPath) return {
		type: "unknown",
		path: ""
	};
	const fragments = urlPath.split("/");
	if (fragments.length >= 3 && fragments[fragments.length - 3] === "invoke") return {
		type: "invoke",
		componentName: fragments[fragments.length - 2],
		handlerName: fragments[fragments.length - 1]
	};
	if (fragments.length > 0 && fragments[fragments.length - 1] === "discover") return { type: "discover" };
	if (fragments.length > 0 && fragments[fragments.length - 1] === "health") return { type: "health" };
	return {
		type: "unknown",
		path: urlPath
	};
}
function commonServiceOptions(options) {
	return {
		journalRetention: options?.journalRetention !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options.journalRetention) : void 0,
		idempotencyRetention: options?.idempotencyRetention !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options.idempotencyRetention) : void 0,
		inactivityTimeout: options?.inactivityTimeout !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options.inactivityTimeout) : void 0,
		abortTimeout: options?.abortTimeout !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options.abortTimeout) : void 0,
		ingressPrivate: options?.ingressPrivate,
		enableLazyState: options !== void 0 && "enableLazyState" in options ? options.enableLazyState : void 0,
		retryPolicyExponentiationFactor: options?.retryPolicy?.exponentiationFactor,
		retryPolicyInitialInterval: options?.retryPolicy?.initialInterval !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options?.retryPolicy?.initialInterval) : void 0,
		retryPolicyMaxInterval: options?.retryPolicy?.maxInterval !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options?.retryPolicy?.maxInterval) : void 0,
		retryPolicyMaxAttempts: options?.retryPolicy?.maxAttempts,
		retryPolicyOnMaxAttempts: options?.retryPolicy?.onMaxAttempts === "kill" ? "KILL" : options?.retryPolicy?.onMaxAttempts === "pause" ? "PAUSE" : void 0
	};
}
function commonHandlerOptions(wrapper, defaultSerde) {
	return {
		input: handlerInputDiscovery(wrapper, defaultSerde),
		output: handlerOutputDiscovery(wrapper, defaultSerde),
		journalRetention: wrapper.options?.journalRetention !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(wrapper.options?.journalRetention) : void 0,
		idempotencyRetention: wrapper.options?.idempotencyRetention !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(wrapper.options?.idempotencyRetention) : void 0,
		inactivityTimeout: wrapper.options?.inactivityTimeout !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(wrapper.options?.inactivityTimeout) : void 0,
		abortTimeout: wrapper.options?.abortTimeout !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(wrapper.options?.abortTimeout) : void 0,
		ingressPrivate: wrapper.options?.ingressPrivate,
		enableLazyState: wrapper.options !== void 0 && "enableLazyState" in wrapper.options ? wrapper.options?.enableLazyState : void 0,
		retryPolicyExponentiationFactor: wrapper.options?.retryPolicy?.exponentiationFactor,
		retryPolicyInitialInterval: wrapper.options?.retryPolicy?.initialInterval !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(wrapper.options?.retryPolicy?.initialInterval) : void 0,
		retryPolicyMaxInterval: wrapper.options?.retryPolicy?.maxInterval !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(wrapper.options?.retryPolicy?.maxInterval) : void 0,
		retryPolicyMaxAttempts: wrapper.options?.retryPolicy?.maxAttempts,
		retryPolicyOnMaxAttempts: wrapper.options?.retryPolicy?.onMaxAttempts === "kill" ? "KILL" : wrapper.options?.retryPolicy?.onMaxAttempts === "pause" ? "PAUSE" : void 0,
		documentation: wrapper.options?.description,
		metadata: wrapper.options?.metadata
	};
}

//#endregion
exports.ServiceComponent = ServiceComponent;
exports.VirtualObjectComponent = VirtualObjectComponent;
exports.WorkflowComponent = WorkflowComponent;
exports.parseUrlComponents = parseUrlComponents;