import manifest from './module.json' with { type: 'json' };
import { ModuleManagerService } from './service.mjs';

let service = null;

export function createModuleManagerModule() {
  return {
    manifest,
    source: 'builtin',
    lifecycle: {
      async start(context) {
        service = new ModuleManagerService({
          modules: context.modules,
          stateStore: context.moduleStateStore,
          packageStore: context.modulePackageStore,
          context,
        });
      },
      async stop() { service = null; },
      async health() { return { ok: Boolean(service), protected: true }; },
    },
  };
}

export function getModuleManagerService() {
  if (!service) throw new Error('Module Manager ist noch nicht gestartet.');
  return service;
}
