"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/builtins/local-backup/index.ts
var local_backup_exports = {};
__export(local_backup_exports, {
  LocalBackupAddon: () => LocalBackupAddon,
  LocalBackupService: () => LocalBackupService,
  default: () => LocalBackupAddon
});
module.exports = __toCommonJS(local_backup_exports);

// src/builtins/local-backup/local-backup.ts
var import_node_crypto = require("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 = (0, import_node_crypto.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: (0, import_node_crypto.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: (0, import_node_crypto.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
var path = __toESM(require("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
    };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  LocalBackupAddon,
  LocalBackupService
});
//# sourceMappingURL=index.js.map