import {
  getModuleTag
} from "./chunk-ZQO3UZ4R.mjs";
import {
  __export
} from "./chunk-J5LGTIGS.mjs";

// src/core/plugin.ts
var plugin_exports = {};
__export(plugin_exports, {
  PluginModuleTypeId: () => PluginModuleTypeId,
  PluginTypeId: () => PluginTypeId,
  Service: () => Service,
  activate: () => activate,
  addModule: () => addModule,
  define: () => define,
  isPlugin: () => isPlugin,
  isPluginModule: () => isPluginModule,
  make: () => make,
  reset: () => reset
});
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Pipeable from "effect/Pipeable";
import { invariant } from "@dxos/invariant";
var __dxlog_file = "/__w/dxos/dxos/packages/sdk/app-framework/src/core/plugin.ts";
var Service = class extends Context.Tag("@dxos/app-framework/PluginManager")() {
};
var activate = (event) => Effect.flatMap(Service, (manager) => manager.activate(event));
var reset = (event) => Effect.flatMap(Service, (manager) => manager.reset(event));
var computeModuleId = (pluginId, moduleName) => {
  return `${pluginId}/module/${moduleName}`;
};
var PluginModuleTypeId = Symbol.for("@dxos/app-framework/PluginModule");
var isPluginModule = (value) => {
  return typeof value === "object" && value !== null && PluginModuleTypeId in value;
};
var PluginModuleImpl = class {
  [PluginModuleTypeId] = PluginModuleTypeId;
  id;
  activatesOn;
  activatesBefore;
  activatesAfter;
  activate;
  constructor(options) {
    this.id = options.id;
    this.activatesOn = options.activatesOn;
    this.activatesBefore = options.activatesBefore;
    this.activatesAfter = options.activatesAfter;
    this.activate = options.activate;
  }
};
var PluginTypeId = Symbol.for("@dxos/app-framework/Plugin");
var isPlugin = (value) => {
  return typeof value === "object" && value !== null && PluginTypeId in value;
};
var PluginImpl = class {
  _meta;
  _modules;
  [PluginTypeId] = PluginTypeId;
  constructor(_meta, _modules) {
    this._meta = _meta;
    this._modules = _modules;
  }
  get meta() {
    return this._meta;
  }
  get modules() {
    return this._modules;
  }
};
var PluginBuilderImpl = class {
  meta;
  _modules = [];
  constructor(meta) {
    this.meta = meta;
  }
  get modules() {
    return this._modules;
  }
  addModule(moduleOptions) {
    this._modules.push(moduleOptions);
    return this;
  }
  pipe() {
    return Pipeable.pipeArguments(this, arguments);
  }
};
var define = (meta) => new PluginBuilderImpl(meta);
function addModule(moduleOptionsOrBuilder, moduleOptions) {
  if (moduleOptions !== void 0) {
    return moduleOptionsOrBuilder.addModule(moduleOptions);
  }
  const moduleOpts = moduleOptionsOrBuilder;
  return (builder) => builder.addModule(moduleOpts);
}
var resolveModule = (meta, module, options) => {
  const moduleOptions = typeof module === "function" ? module(options) : module;
  const id = Option.fromNullable(moduleOptions.id).pipe(Option.match({
    onNone: () => {
      const exportName = getModuleTag(moduleOptions.activate);
      invariant(exportName, `Plugin module missing name. Plugin: ${meta.id}`, {
        F: __dxlog_file,
        L: 300,
        S: void 0,
        A: [
          "exportName",
          "`Plugin module missing name. Plugin: ${meta.id}`"
        ]
      });
      return computeModuleId(meta.id, exportName);
    },
    onSome: (id2) => computeModuleId(meta.id, id2)
  }));
  return new PluginModuleImpl({
    ...moduleOptions,
    id
  });
};
function make(builder) {
  const meta = builder.meta;
  const factory = (options) => {
    const modules = builder.modules.map((module) => resolveModule(meta, module, options));
    return new PluginImpl(meta, modules);
  };
  return Object.assign(factory, {
    meta
  });
}

export {
  Service,
  activate,
  reset,
  PluginModuleTypeId,
  isPluginModule,
  PluginTypeId,
  isPlugin,
  define,
  addModule,
  make,
  plugin_exports
};
//# sourceMappingURL=chunk-KWP6PKIU.mjs.map
