import {
  useApp
} from "../chunk-YZQ6NFG4.mjs";
import "../chunk-FHQTHCX7.mjs";
import "../chunk-4DQMLMGU.mjs";
import {
  capabilities_exports
} from "../chunk-IGRQWMWW.mjs";
import {
  activation_events_exports
} from "../chunk-NPUEVX42.mjs";
import "../chunk-YAFEA4GV.mjs";
import {
  plugin_manager_exports
} from "../chunk-WGWEEBUV.mjs";
import "../chunk-PKQT6C53.mjs";
import {
  plugin_exports
} from "../chunk-KWP6PKIU.mjs";
import {
  capability_exports
} from "../chunk-ZQO3UZ4R.mjs";
import "../chunk-J5LGTIGS.mjs";

// src/testing/service.ts
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { invariant } from "@dxos/invariant";
var __dxlog_file = "/__w/dxos/dxos/packages/sdk/app-framework/src/testing/service.ts";
var fromPlugins = (plugins) => Layer.effect(plugin_exports.Service, Effect.gen(function* () {
  const pluginLoader = (id) => Effect.sync(() => {
    const plugin = plugins.find((plugin2) => plugin2.meta.id === id);
    invariant(plugin, `Plugin not found: ${id}`, {
      F: __dxlog_file,
      L: 26,
      S: this,
      A: [
        "plugin",
        "`Plugin not found: ${id}`"
      ]
    });
    return plugin;
  });
  const manager = plugin_manager_exports.make({
    pluginLoader,
    plugins,
    core: plugins.map((plugin) => plugin.meta.id)
  });
  manager.capabilities.contribute({
    interface: capabilities_exports.PluginManager,
    implementation: manager,
    module: "dxos.org/app-framework/plugin-manager"
  });
  manager.capabilities.contribute({
    interface: capabilities_exports.AtomRegistry,
    implementation: manager.registry,
    module: "dxos.org/app-framework/atom-registry"
  });
  yield* manager.activate(activation_events_exports.Startup);
  return manager;
}));

// src/testing/withPluginManager.tsx
import * as Effect2 from "effect/Effect";
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 = plugin_manager_exports.make({
    pluginLoader: () => raise(new Error("Not implemented")),
    plugins: [
      StoryPlugin,
      ...plugins
    ],
    core: [
      StoryPlugin.meta.id,
      ...core
    ],
    ...options
  });
  if (capabilities) {
    getProviderValue(capabilities, pluginManager.capabilities).forEach((capability) => {
      pluginManager.capabilities.contribute({
        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 = capability_exports.contributes(capabilities_exports.ReactRoot, {
        id: context.id,
        root: () => /* @__PURE__ */ React.createElement(Story, null)
      });
      pluginManager.capabilities.contribute({
        ...capability,
        module: "dxos.org/app-framework/withPluginManager"
      });
      return () => {
        pluginManager.capabilities.remove(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 = plugin_exports.define(storyMeta).pipe(plugin_exports.addModule({
  id: "Story",
  activatesOn: activation_events_exports.Startup,
  activate: () => Effect2.succeed([])
}), plugin_exports.make)();
export {
  fromPlugins,
  setupPluginManager,
  withPluginManager
};
//# sourceMappingURL=index.mjs.map
