require("./chunk-9hOWP6kD.cjs");
let _fluid_app_widget_runtime_worker = require("@fluid-app/widget-runtime/worker");
//#region src/widgets/portal-function.ts
const PORTAL_FUNCTION_METADATA = Symbol.for("@fluid-app/portal-sdk/portal-function-metadata");
const PORTAL_FUNCTION_ERROR_PREFIX = "PORTAL_FUNCTION_ERROR:";
const PORTAL_FUNCTION_ERROR_CODES = new Set([
	"NOT_MOUNTED",
	"NOT_DECLARED",
	"INVALID_ARGUMENT",
	"UNAVAILABLE",
	"UNSUPPORTED",
	"HOST_FAILURE",
	"INVALID_RESPONSE"
]);
/** A stable, serializable failure reported by a Portal function call. */
var PortalFunctionError = class extends Error {
	code;
	constructor(code, message) {
		super(message);
		this.name = "PortalFunctionError";
		this.code = code;
	}
};
function definePortalFunction(definition, ..._contractErrors) {
	const remoteFunction = (0, _fluid_app_widget_runtime_worker.defineRemoteDomCapabilityFunction)(definition);
	const portalFunction = (async (...args) => {
		try {
			if (args.length > 1) throw new PortalFunctionError("INVALID_ARGUMENT", `Portal function "${definition.capability}.${definition.method}" accepts at most one argument.`);
			const invoke = remoteFunction;
			return args.length === 0 ? await invoke() : await invoke(args[0]);
		} catch (error) {
			throw toPortalFunctionError(error);
		}
	});
	return attachPortalFunctionMetadata(portalFunction, definition);
}
function implementPortalFunction(portalFunction, handler) {
	const definition = getPortalFunctionMetadata(portalFunction);
	if (!definition) throw new PortalFunctionError("INVALID_ARGUMENT", "implementPortalFunction requires a function created by definePortalFunction.");
	return {
		definition,
		portalFunction,
		handler
	};
}
/** @internal Used to normalize worker declarations and host implementations. */
function getPortalFunctionMetadata(value) {
	if (typeof value !== "function") return void 0;
	const portalMetadata = Reflect.get(value, PORTAL_FUNCTION_METADATA);
	if (isPortalFunctionDefinition(portalMetadata)) return portalMetadata;
	const runtimeMetadata = (0, _fluid_app_widget_runtime_worker.getRemoteDomCapabilityFunctionMetadata)(value);
	return runtimeMetadata ? toPortalFunctionDefinition(runtimeMetadata) : void 0;
}
function attachPortalFunctionMetadata(portalFunction, definition) {
	const stableDefinition = Object.freeze({ ...definition });
	Object.defineProperty(portalFunction, PORTAL_FUNCTION_METADATA, {
		configurable: false,
		enumerable: false,
		writable: false,
		value: stableDefinition
	});
	return portalFunction;
}
function toPortalFunctionDefinition(metadata) {
	return {
		capability: metadata.capability,
		version: metadata.version,
		method: metadata.method
	};
}
function isPortalFunctionDefinition(value) {
	if (typeof value !== "object" || value === null) return false;
	return "capability" in value && typeof value.capability === "string" && "version" in value && typeof value.version === "string" && "method" in value && typeof value.method === "string";
}
function toPortalFunctionError(error) {
	if (error instanceof PortalFunctionError) return error;
	if (error instanceof _fluid_app_widget_runtime_worker.RemoteDomCapabilityFunctionError) return new PortalFunctionError(error.code, error.message);
	const hostFailure = parseHostFunctionError(error);
	if (hostFailure) return hostFailure;
	return new PortalFunctionError("HOST_FAILURE", "The portal host could not complete this function call.");
}
function parseHostFunctionError(error) {
	if (!(error instanceof Error)) return null;
	const prefixIndex = error.message.indexOf(PORTAL_FUNCTION_ERROR_PREFIX);
	if (prefixIndex < 0) return null;
	const encoded = error.message.slice(prefixIndex + 22);
	const separatorIndex = encoded.indexOf(":");
	if (separatorIndex < 0) return null;
	const code = encoded.slice(0, separatorIndex);
	if (!isPortalFunctionErrorCode(code)) return null;
	return new PortalFunctionError(code, encoded.slice(separatorIndex + 1).trim() || "The portal host could not complete this function call.");
}
function isPortalFunctionErrorCode(value) {
	return PORTAL_FUNCTION_ERROR_CODES.has(value);
}
//#endregion
Object.defineProperty(exports, "PortalFunctionError", {
	enumerable: true,
	get: function() {
		return PortalFunctionError;
	}
});
Object.defineProperty(exports, "attachPortalFunctionMetadata", {
	enumerable: true,
	get: function() {
		return attachPortalFunctionMetadata;
	}
});
Object.defineProperty(exports, "definePortalFunction", {
	enumerable: true,
	get: function() {
		return definePortalFunction;
	}
});
Object.defineProperty(exports, "getPortalFunctionMetadata", {
	enumerable: true,
	get: function() {
		return getPortalFunctionMetadata;
	}
});
Object.defineProperty(exports, "implementPortalFunction", {
	enumerable: true,
	get: function() {
		return implementPortalFunction;
	}
});

//# sourceMappingURL=portal-function-Croms0WD.cjs.map