import {
  useApp
} from "../chunk-FO2PH7M3.mjs";
import {
  Capabilities,
  Events,
  PluginManager,
  contributes,
  defineModule,
  definePlugin
} from "../chunk-FMN65HSW.mjs";

// src/testing/withPluginManager.tsx
import React, { useEffect, useMemo } from "react";
import { raise } from "@dxos/debug";
var getValue = (providerOrValue, context) => {
  return typeof providerOrValue === "function" ? providerOrValue(context) : providerOrValue;
};
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: [
      STORY_PLUGIN,
      ...core
    ],
    ...options
  });
  if (capabilities) {
    getValue(capabilities, pluginManager.context).forEach((capability) => {
      pluginManager.context.contributeCapability({
        interface: capability.interface,
        implementation: capability.implementation,
        module: "story"
      });
    });
  }
  return pluginManager;
};
var withPluginManager = (options = {}) => {
  return (Story, context) => {
    const pluginManager = useMemo(() => setupPluginManager(options), [
      options
    ]);
    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
    ]);
    useEffect(() => {
      const timeout = setTimeout(async () => {
        await Promise.all(options.fireEvents?.map((event) => pluginManager.activate(event)) ?? []);
      });
      return () => clearTimeout(timeout);
    }, [
      pluginManager
    ]);
    const App = useApp({
      pluginManager
    });
    return /* @__PURE__ */ React.createElement(App, null);
  };
};
var STORY_PLUGIN = "dxos.org/app-framework/story";
var storyPlugin = () => definePlugin({
  id: STORY_PLUGIN,
  name: "Story"
}, [
  defineModule({
    id: STORY_PLUGIN,
    activatesOn: Events.Startup,
    activate: () => []
  })
]);
export {
  getValue,
  setupPluginManager,
  withPluginManager
};
//# sourceMappingURL=index.mjs.map
