import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
import {
  useApp
} from "../chunk-3UPX5OIS.mjs";
import {
  Capabilities,
  Events,
  PluginManager,
  contributes,
  defineModule,
  definePlugin
} from "../chunk-XJZGUJ3H.mjs";

// src/testing/withPluginManager.tsx
import React, { useEffect, useMemo } from "react";
import { raise } from "@dxos/debug";
import { useAsyncEffect } from "@dxos/react-hooks";
import { getProviderValue } from "@dxos/util";
var setupPluginManager = ({ capabilities, plugins = [], core = plugins.map(({ meta }) => meta.id), ...options } = {}) => {
  const pluginManager = new PluginManager({
    pluginLoader: () => raise(new Error("Not implemented")),
    plugins: [
      StoryPlugin(),
      ...plugins
    ],
    core: [
      StoryPlugin.meta.id,
      ...core
    ],
    ...options
  });
  if (capabilities) {
    getProviderValue(capabilities, pluginManager.context).forEach((capability) => {
      pluginManager.context.contributeCapability({
        interface: capability.interface,
        implementation: capability.implementation,
        module: "story"
      });
    });
  }
  return pluginManager;
};
var withPluginManager = (init = {}) => {
  return (Story, context) => {
    const options = typeof init === "function" ? init(context) : init;
    const pluginManager = useMemo(() => setupPluginManager(options), [
      init
    ]);
    useEffect(() => {
      const capability = contributes(Capabilities.ReactRoot, {
        id: context.id,
        root: () => /* @__PURE__ */ React.createElement(Story, null)
      });
      pluginManager.context.contributeCapability({
        ...capability,
        module: "dxos.org/app-framework/withPluginManager"
      });
      return () => {
        pluginManager.context.removeCapability(capability.interface, capability.implementation);
      };
    }, [
      pluginManager,
      context
    ]);
    useAsyncEffect(async () => {
      await Promise.all(options.fireEvents?.map((event) => pluginManager.activate(event)) ?? []);
    }, [
      pluginManager
    ]);
    const App = useApp({
      pluginManager
    });
    return /* @__PURE__ */ React.createElement(App, null);
  };
};
var storyMeta = {
  id: "dxos.org/app-framework/story",
  name: "Story"
};
var StoryPlugin = definePlugin(storyMeta, () => [
  defineModule({
    id: storyMeta.id,
    activatesOn: Events.Startup,
    activate: () => []
  })
]);
export {
  setupPluginManager,
  withPluginManager
};
//# sourceMappingURL=index.mjs.map
