// src/builtins/local-backup/local-backup.ts
import { randomUUID } from "crypto";
var LocalBackupService = class {
  constructor(config, logger, eventBus, storage) {
    this.config = config;
    this.logger = logger;
    this.eventBus = eventBus;
    this.storage = storage;
  }
  manifests = [];
  /** Create a backup of specified locations */
  async backup(options) {
    const id = randomUUID();
    const timestamp = Date.now();
    const locations = options?.locations ?? ["config", "events", "logs"];
    this.logger.info(`Starting backup ${id} (${locations.join(", ")})`);
    const manifest = {
      id,
      timestamp,
      label: options?.label,
      locations,
      sizeMB: 0,
      path: `${this.config.backupDir}/${id}`
    };
    const updated = [...this.manifests, manifest];
    this.manifests = updated;
    await this.pruneOldBackups();
    this.eventBus.emit({
      id: randomUUID(),
      timestamp: /* @__PURE__ */ new Date(),
      source: { type: "addon", id: "local-backup" },
      category: "backup.completed",
      data: { backupId: id, locations: [...locations], sizeMB: manifest.sizeMB }
    });
    this.logger.info(`Backup ${id} completed`);
    return manifest;
  }
  /** Restore from a backup */
  async restore(backupId) {
    const manifest = this.manifests.find((m) => m.id === backupId);
    if (!manifest) throw new Error(`Backup ${backupId} not found`);
    this.logger.info(`Restoring from backup ${backupId}`);
    this.eventBus.emit({
      id: randomUUID(),
      timestamp: /* @__PURE__ */ new Date(),
      source: { type: "addon", id: "local-backup" },
      category: "backup.restored",
      data: { backupId }
    });
  }
  /** List all backups sorted by timestamp descending */
  list() {
    return [...this.manifests].sort((a, b) => b.timestamp - a.timestamp);
  }
  /** Delete a specific backup */
  async delete(backupId) {
    this.manifests = this.manifests.filter((m) => m.id !== backupId);
  }
  async pruneOldBackups() {
    const sorted = [...this.manifests].sort((a, b) => a.timestamp - b.timestamp);
    while (sorted.length > this.config.retentionCount) {
      const oldest = sorted.shift();
      if (oldest) {
        this.logger.info(`Pruning old backup ${oldest.id}`);
        this.manifests = this.manifests.filter((m) => m.id !== oldest.id);
      }
    }
  }
};

// src/builtins/local-backup/local-backup.addon.ts
import * as path from "path";
var LocalBackupAddon = class {
  manifest = {
    id: "local-backup",
    name: "Local Backup",
    version: "1.0.0",
    capabilities: ["backup"]
  };
  service = null;
  currentConfig = {
    retentionCount: 7
  };
  async initialize(context) {
    this.currentConfig = {
      retentionCount: context.addonConfig.retentionCount ?? this.currentConfig.retentionCount
    };
    const backupConfig = {
      backupDir: path.join(context.locationPaths.data, "backups"),
      retentionCount: this.currentConfig.retentionCount
    };
    this.service = new LocalBackupService(
      backupConfig,
      context.logger,
      context.eventBus,
      context.storage
    );
    context.logger.info("Local Backup initialized");
  }
  async shutdown() {
    this.service = null;
  }
  getService() {
    if (!this.service) throw new Error("Local Backup not initialized");
    return this.service;
  }
  getCapabilityProvider(name) {
    if (name === "backup" && this.service) {
      return this.service;
    }
    return null;
  }
  getConfigSchema() {
    return {
      sections: [
        {
          id: "backup-retention",
          title: "Backup Retention",
          description: "How many local backup snapshots to keep on disk.",
          columns: 1,
          fields: [
            {
              type: "number",
              key: "retentionCount",
              label: "Retention Count",
              description: "Number of backup snapshots to keep before deleting the oldest",
              min: 1,
              max: 100,
              step: 1
            }
          ]
        }
      ]
    };
  }
  getConfig() {
    return { ...this.currentConfig };
  }
  async onConfigChange(config) {
    this.currentConfig = {
      retentionCount: config.retentionCount ?? this.currentConfig.retentionCount
    };
  }
};

export {
  LocalBackupService,
  LocalBackupAddon
};
//# sourceMappingURL=chunk-SO4LROOT.mjs.map