import manifest from './module.json' with { type: 'json' };
import { PlatformError } from '../../kernel/errors.mjs';
import { createDeploymentService } from './service.mjs';

let service = null;
let adapters = {};

export function configureDeploymentsModule(nextAdapters = {}) {
  adapters = { ...adapters, ...nextAdapters };
}

export function getDeploymentService() {
  if (!service) throw new PlatformError('Deployment-Modul ist noch nicht gestartet.', {
    code: 'DEPLOYMENT_MODULE_NOT_READY', status: 503, expose: true,
  });
  return service;
}

export function createDeploymentsModule() {
  return {
    manifest,
    lifecycle: {
      async start() { service = createDeploymentService(adapters); },
      async stop() { service = null; },
      async health() { return getDeploymentService().health(); },
    },
  };
}
