import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { PlatformError } from '../../kernel/errors.mjs';

const here = dirname(fileURLToPath(import.meta.url));
const manifest = JSON.parse(readFileSync(join(here, 'module.json'), 'utf8'));
let service = null;
let adapters = { etsyApi: null, ebayApi: null };

export function configureSettingsModule(nextAdapters = {}) { adapters = { ...adapters, ...nextAdapters }; }
export function getSettingsService() {
  if (!service) throw new PlatformError('Einstellungs-Modul ist noch nicht gestartet.', { code: 'SETTINGS_MODULE_NOT_READY', status: 503, expose: true });
  return service;
}
export function createSettingsModule() {
  return {
    manifest,
    lifecycle: {
      async start() { service = (await import('./service.mjs')).createSettingsService(adapters); },
      async stop() { service = null; },
      async health() { return getSettingsService().health(); },
    },
  };
}
