{"version":3,"file":"build-remote-widget-plugin-manifests.mjs","names":[],"sources":["../../src/utils/build-remote-widget-plugin-manifests.ts"],"sourcesContent":["import type { ComponentType } from \"react\";\nimport { createRemoteWidgetCapabilityGrants } from \"@fluid-app/portal-core/remote-widget-capability-grants\";\nimport {\n  isPropertyFieldType,\n  type PropertyField,\n  type WidgetManifest,\n} from \"@fluid-app/portal-core/registries\";\nimport type {\n  RemoteDomWidgetDefinition,\n  RemoteDomWidgetPackageDescriptor,\n} from \"@fluid-app/portal-core/remote-dom-widget-package\";\nimport {\n  projectRemoteWidgetPackages,\n  type RemoteDomWidgetRegistryOptions,\n  type WidgetRegistryWarningSink,\n} from \"../widgets/remote/host/build-widget-registry\";\n\nconst REMOTE_WIDGET_PACKAGE_CATEGORY_PREFIX = \"custom-widget-package\";\n\nexport interface RemoteWidgetPluginHostOptions extends RemoteDomWidgetRegistryOptions {\n  readonly warningSink?: WidgetRegistryWarningSink;\n}\n\n/**\n * Projects accepted canonical packages into builder manifests using the same\n * package-atomic acceptance pass as the runtime registry.\n */\nexport function buildRemoteWidgetPluginManifests(\n  packages: readonly RemoteDomWidgetPackageDescriptor[],\n  hostOptions: RemoteWidgetPluginHostOptions = {},\n): WidgetManifest[] {\n  const { warningSink, ...registryOptions } = hostOptions;\n  const projection = projectRemoteWidgetPackages(\n    {},\n    packages,\n    registryOptions,\n    warningSink ?? console.warn,\n  );\n  return projection.acceptedWidgets.map(\n    ({ remotePackage, widget, component }) =>\n      toWidgetManifest(remotePackage, widget, component),\n  );\n}\n\nexport function getRemoteWidgetPackageCategoryId(\n  remotePackage: Pick<RemoteDomWidgetPackageDescriptor, \"packageId\" | \"source\">,\n): string {\n  return [\n    REMOTE_WIDGET_PACKAGE_CATEGORY_PREFIX,\n    remotePackage.source,\n    encodeURIComponent(remotePackage.packageId),\n  ].join(\":\");\n}\n\nfunction toWidgetManifest(\n  remotePackage: RemoteDomWidgetPackageDescriptor,\n  widget: RemoteDomWidgetDefinition,\n  component: ComponentType<Record<string, unknown>>,\n): WidgetManifest {\n  const resizable = toResizable(widget.resizable);\n  const capabilityGrants = createRemoteWidgetCapabilityGrants(\n    remotePackage,\n    widget,\n  );\n  return {\n    manifestVersion: 1,\n    type: widget.type,\n    component,\n    displayName: widget.displayName,\n    description: widget.description,\n    icon: widget.icon,\n    category: getRemoteWidgetPackageCategoryId(remotePackage),\n    propertySchema: normalizePropertySchema(widget),\n    defaultProps: { ...widget.defaultProps },\n    minSdkVersion: widget.minSdkVersion,\n    ...(resizable === undefined ? {} : { resizable }),\n    ...(capabilityGrants === undefined ? {} : { capabilityGrants }),\n  };\n}\n\nfunction normalizePropertySchema(\n  widget: RemoteDomWidgetDefinition,\n): WidgetManifest[\"propertySchema\"] {\n  const fields = Array.isArray(widget.propertySchema.fields)\n    ? widget.propertySchema.fields.filter(isPropertyField)\n    : [];\n  return {\n    ...widget.propertySchema,\n    widgetType: widget.type,\n    displayName:\n      typeof widget.propertySchema.displayName === \"string\"\n        ? widget.propertySchema.displayName\n        : widget.displayName,\n    fields,\n  } as WidgetManifest[\"propertySchema\"];\n}\n\nfunction toResizable(\n  resizable: RemoteDomWidgetDefinition[\"resizable\"],\n): WidgetManifest[\"resizable\"] {\n  if (resizable === false) return undefined;\n  if (resizable === true || resizable === \"both\") {\n    return { horizontal: true, vertical: true };\n  }\n  return resizable === \"horizontal\" ? { horizontal: true } : { vertical: true };\n}\n\nfunction isPropertyField(value: unknown): value is PropertyField {\n  return (\n    typeof value === \"object\" &&\n    value !== null &&\n    \"key\" in value &&\n    typeof value.key === \"string\" &&\n    \"label\" in value &&\n    typeof value.label === \"string\" &&\n    \"type\" in value &&\n    typeof value.type === \"string\" &&\n    isPropertyFieldType(value.type)\n  );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBA,MAAM,wCAAwC;;;;;AAU9C,SAAgB,iCACd,UACA,cAA6C,EAAE,EAC7B;CAClB,MAA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