Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
require("../chunk-9hOWP6kD.cjs");
const require_portal_function = require("../portal-function-Croms0WD.cjs");
let react = require("react");
let _fluid_app_widget_runtime_worker = require("@fluid-app/widget-runtime/worker");
//#region src/widgets/portal-built-in-functions.ts
const ACCOUNT_DEFINITION = {
	capability: "account",
	version: "1",
	method: "get"
};
const STORE_DEFINITION = {
	capability: "store",
	version: "1",
	method: "get"
};
const APP_DEFINITION = {
	capability: "fluidApp",
	version: "1",
	method: "get"
};
const PROFILE_DEFINITION = {
	capability: "fluidProfile",
	version: "1",
	method: "get"
};
const NAVIGATION_STATE_DEFINITION = {
	capability: "appNavigation",
	version: "1",
	method: "get"
};
const BUILD_HREF_DEFINITION = {
	capability: "appNavigation",
	version: "1",
	method: "buildHref"
};
const NAVIGATE_DEFINITION = {
	capability: "appNavigation",
	version: "1",
	method: "navigate"
};
const FULLSCREEN_STATE_DEFINITION = {
	capability: "fullscreen",
	version: "1",
	method: "get"
};
const REQUEST_FULLSCREEN_DEFINITION = {
	capability: "fullscreen",
	version: "1",
	method: "request"
};
const EXIT_FULLSCREEN_DEFINITION = {
	capability: "fullscreen",
	version: "1",
	method: "exit"
};
const getUserAccount = require_portal_function.definePortalFunction(ACCOUNT_DEFINITION);
const getStore = require_portal_function.definePortalFunction(STORE_DEFINITION);
const getPortalApp = require_portal_function.definePortalFunction(APP_DEFINITION);
const getPortalProfile = require_portal_function.definePortalFunction(PROFILE_DEFINITION);
const getNavigationState = require_portal_function.definePortalFunction(NAVIGATION_STATE_DEFINITION);
const buildPortalHref = require_portal_function.definePortalFunction(BUILD_HREF_DEFINITION);
const navigate = require_portal_function.definePortalFunction(NAVIGATE_DEFINITION);
const navigateTo = require_portal_function.attachPortalFunctionMetadata(async (target) => {
	await navigate(target);
}, NAVIGATE_DEFINITION);
const getFullscreenState = require_portal_function.definePortalFunction(FULLSCREEN_STATE_DEFINITION);
const request = require_portal_function.definePortalFunction(REQUEST_FULLSCREEN_DEFINITION);
const requestFullscreen = require_portal_function.attachPortalFunctionMetadata(async () => {
	await request();
}, REQUEST_FULLSCREEN_DEFINITION);
const exit = require_portal_function.definePortalFunction(EXIT_FULLSCREEN_DEFINITION);
const exitFullscreen = require_portal_function.attachPortalFunctionMetadata(async () => {
	await exit();
}, EXIT_FULLSCREEN_DEFINITION);
//#endregion
//#region src/widgets/worker.ts
const SOURCE_WIDGET_MARKER = "__fluidSourceWidget";
const SOURCE_WIDGET_PACKAGE_MARKER = "__fluidSourceWidgetPackage";
const portalFunctionsByWidgetName = /* @__PURE__ */ new Map();
function defineWidget(options) {
	const uses = options.uses ?? [];
	const normalized = normalizePortalFunctions(uses);
	const registrations = portalFunctionsByWidgetName.get(options.name) ?? [];
	registrations.push(normalized);
	portalFunctionsByWidgetName.set(options.name, registrations);
	return {
		...options,
		[SOURCE_WIDGET_MARKER]: true,
		defaultProps: options.defaultProps ?? {},
		uses,
		capabilities: normalized.capabilities
	};
}
function defineWidgetPackage(options) {
	const packageType = options.packageType ?? "company";
	const packageStableId = options.packageStableId;
	if (!packageStableId && packageType !== "droplet") throw new Error("defineWidgetPackage requires packageStableId for company packages.");
	const stableId = packageStableId ?? "__fluid_cli_managed__";
	return {
		[SOURCE_WIDGET_PACKAGE_MARKER]: true,
		manifestVersion: 1,
		scope: options.scope,
		packageStableId: stableId,
		packageId: `${options.scope}.${stableId}`,
		packageType,
		version: options.version ?? "0.0.0-dev",
		widgets: options.widgets,
		cssUrls: options.cssUrls ?? []
	};
}
/** Starts one Remote DOM worker containing every widget in the source package. */
function startWidgetPackage(widgetPackage) {
	return (0, _fluid_app_widget_runtime_worker.startRemoteDomWidgetWorker)({ widgets: "packageId" in widgetPackage ? Object.fromEntries(widgetPackage.widgets.map((widget) => [`${getCanonicalRuntimePackageId(widgetPackage)}.${widget.name}`, {
		component: widget.component,
		capabilities: widget.capabilities,
		functions: readSourcePortalFunctions(widget)
	}])) : Object.fromEntries(widgetPackage.widgets.map((widget) => [widget.type, {
		component: widget.component,
		capabilities: widget.capabilities ?? [],
		functions: readGeneratedPortalFunctions(widget)
	}])) });
}
function normalizePortalFunctions(uses) {
	const versionByCapability = /* @__PURE__ */ new Map();
	const functionsByIdentity = /* @__PURE__ */ new Map();
	for (const portalFunction of uses) {
		const metadata = require_portal_function.getPortalFunctionMetadata(portalFunction);
		if (!metadata) throw new Error("defineWidget uses must contain functions created by definePortalFunction.");
		const currentVersion = versionByCapability.get(metadata.capability);
		if (currentVersion && currentVersion !== metadata.version) throw new Error(`defineWidget cannot use capability "${metadata.capability}" at both version "${currentVersion}" and version "${metadata.version}".`);
		versionByCapability.set(metadata.capability, metadata.version);
		functionsByIdentity.set(JSON.stringify([
			metadata.capability,
			metadata.version,
			metadata.method
		]), metadata);
	}
	return {
		capabilities: [...versionByCapability].map(([name, version]) => ({
			name,
			version
		})),
		functions: [...functionsByIdentity.values()]
	};
}
function readGeneratedPortalFunctions(widget) {
	if (!widget.name) return [];
	const registrations = portalFunctionsByWidgetName.get(widget.name) ?? [];
	portalFunctionsByWidgetName.delete(widget.name);
	const capabilities = widget.capabilities ?? [];
	const matchingRegistrations = registrations.filter((registration) => haveSameCapabilityDeclarations(registration.capabilities, capabilities));
	const uniqueFunctions = /* @__PURE__ */ new Map();
	for (const registration of matchingRegistrations) uniqueFunctions.set(portalFunctionSetKey(registration.functions), registration.functions);
	if (uniqueFunctions.size > 1) throw new Error(`Generated worker entry cannot recover an unambiguous uses declaration for widget "${widget.name}" (${widget.type}). Multiple widgets with that name declare the same capabilities but different portal functions; give every source widget a unique name.`);
	return uniqueFunctions.values().next().value ?? [];
}
function readSourcePortalFunctions(widget) {
	portalFunctionsByWidgetName.delete(widget.name);
	return normalizePortalFunctions(widget.uses).functions;
}
function haveSameCapabilityDeclarations(left, right) {
	if (left.length !== right.length) return false;
	const rightKeys = new Set(right.map(({ name, version }) => JSON.stringify([name, version])));
	return left.every(({ name, version }) => rightKeys.has(JSON.stringify([name, version])));
}
function portalFunctionSetKey(functions) {
	return JSON.stringify(functions.map(({ capability, version, method }) => [
		capability,
		version,
		method
	]).sort(([leftCapability, leftVersion, leftMethod], right) => `${leftCapability}\u0000${leftVersion}\u0000${leftMethod}`.localeCompare(right.join("\0"))));
}
function getCanonicalRuntimePackageId(widgetPackage) {
	const owner = widgetPackage.packageType;
	const scope = widgetPackage.scope;
	const stableId = widgetPackage.packageStableId;
	return `${owner}.${scope !== "company" && scope !== "droplet" && !stableId.startsWith(`${scope}.`) ? `${scope}.${stableId}` : stableId}`;
}
function SearchSort({ onSearchChange, onSortChange, ...props }) {
	const elementRef = (0, react.useRef)(null);
	const searchChangeRef = (0, react.useRef)(onSearchChange);
	const sortChangeRef = (0, react.useRef)(onSortChange);
	searchChangeRef.current = onSearchChange;
	sortChangeRef.current = onSortChange;
	(0, react.useEffect)(() => {
		const element = elementRef.current;
		if (!element) return;
		const handleSearchChange = (event) => {
			const value = readRemoteEventValue(event);
			if (value !== void 0) searchChangeRef.current(value);
		};
		const handleSortChange = (event) => {
			const value = readRemoteEventValue(event);
			if (value !== void 0) sortChangeRef.current?.(value);
		};
		element.addEventListener("search-change", handleSearchChange);
		element.addEventListener("sort-change", handleSortChange);
		return () => {
			element.removeEventListener("search-change", handleSearchChange);
			element.removeEventListener("sort-change", handleSortChange);
		};
	}, []);
	return (0, react.createElement)("fluid-ui-search-sort", {
		...props,
		ref: elementRef
	});
}
function FluidSpacerWidget(props) {
	return (0, react.createElement)("fluid-spacer-widget", props);
}
function readRemoteEventValue(event) {
	if (!(event instanceof CustomEvent)) return void 0;
	if (typeof event.detail === "string") return event.detail;
	if (typeof event.detail === "object" && event.detail !== null && "detail" in event.detail && typeof event.detail.detail === "string") return event.detail.detail;
}
//#endregion
exports.FluidSpacerWidget = FluidSpacerWidget;
exports.PortalFunctionError = require_portal_function.PortalFunctionError;
exports.SearchSort = SearchSort;
exports.buildPortalHref = buildPortalHref;
exports.definePortalFunction = require_portal_function.definePortalFunction;
exports.defineWidget = defineWidget;
exports.defineWidgetPackage = defineWidgetPackage;
exports.exitFullscreen = exitFullscreen;
exports.getFullscreenState = getFullscreenState;
exports.getNavigationState = getNavigationState;
exports.getPortalApp = getPortalApp;
exports.getPortalProfile = getPortalProfile;
exports.getStore = getStore;
exports.getUserAccount = getUserAccount;
exports.implementPortalFunction = require_portal_function.implementPortalFunction;
exports.navigateTo = navigateTo;
exports.requestFullscreen = requestFullscreen;
exports.startWidgetPackage = startWidgetPackage;

//# sourceMappingURL=worker.cjs.map