"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);
var snapshot_exports = {};
__export(snapshot_exports, {
  SnapshotManager: () => SnapshotManager
});
module.exports = __toCommonJS(snapshot_exports);
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
const SNAPSHOTS_DIR = "snapshots";
class SnapshotManager {
  constructor(db, projectRoot) {
    this.db = db;
    this.snapshotsDir = path.join(projectRoot, ".kirograph", SNAPSHOTS_DIR);
  }
  ensureDir() {
    fs.mkdirSync(this.snapshotsDir, { recursive: true });
  }
  save(label) {
    this.ensureDir();
    const timestamp = Date.now();
    const snapshotLabel = label ?? new Date(timestamp).toISOString().slice(0, 19).replace(/[T:]/g, "-");
    const nodes = this.db.getAllNodes().map((n) => ({
      id: n.id,
      name: n.name,
      kind: n.kind,
      filePath: n.filePath
    }));
    const edges = this.db.getAllEdges().map((e) => ({
      source: e.source,
      target: e.target,
      kind: e.kind
    }));
    const snapshot = {
      label: snapshotLabel,
      timestamp,
      nodeCount: nodes.length,
      edgeCount: edges.length,
      nodes,
      edges
    };
    const filename = `${snapshotLabel}.json`;
    fs.writeFileSync(path.join(this.snapshotsDir, filename), JSON.stringify(snapshot));
    return snapshot;
  }
  list() {
    if (!fs.existsSync(this.snapshotsDir)) return [];
    return fs.readdirSync(this.snapshotsDir).filter((f) => f.endsWith(".json")).flatMap((f) => {
      try {
        const data = JSON.parse(fs.readFileSync(path.join(this.snapshotsDir, f), "utf8"));
        return [{ label: data.label, timestamp: data.timestamp, nodeCount: data.nodeCount, edgeCount: data.edgeCount }];
      } catch {
        return [];
      }
    }).sort((a, b) => b.timestamp - a.timestamp);
  }
  load(label) {
    const candidates = [
      path.join(this.snapshotsDir, `${label}.json`),
      path.join(this.snapshotsDir, label)
    ];
    for (const p of candidates) {
      if (fs.existsSync(p)) {
        try {
          return JSON.parse(fs.readFileSync(p, "utf8"));
        } catch {
          return null;
        }
      }
    }
    return null;
  }
  loadLatest() {
    const all = this.list();
    return all.length > 0 ? this.load(all[0].label) : null;
  }
  currentSnapshot() {
    const nodes = this.db.getAllNodes().map((n) => ({
      id: n.id,
      name: n.name,
      kind: n.kind,
      filePath: n.filePath
    }));
    const edges = this.db.getAllEdges().map((e) => ({
      source: e.source,
      target: e.target,
      kind: e.kind
    }));
    return { label: "current", timestamp: Date.now(), nodeCount: nodes.length, edgeCount: edges.length, nodes, edges };
  }
  diff(from, to) {
    const fromNodeIds = new Set(from.nodes.map((n) => n.id));
    const toNodeIds = new Set(to.nodes.map((n) => n.id));
    const addedNodes = to.nodes.filter((n) => !fromNodeIds.has(n.id));
    const removedNodes = from.nodes.filter((n) => !toNodeIds.has(n.id));
    const edgeKey = (e) => `${e.source}|${e.target}|${e.kind}`;
    const fromEdgeKeys = new Set(from.edges.map(edgeKey));
    const toEdgeKeys = new Set(to.edges.map(edgeKey));
    const addedEdges = to.edges.filter((e) => !fromEdgeKeys.has(edgeKey(e)));
    const removedEdges = from.edges.filter((e) => !toEdgeKeys.has(edgeKey(e)));
    return {
      from: { label: from.label, timestamp: from.timestamp },
      to: { label: to.label, timestamp: to.timestamp },
      addedNodes,
      removedNodes,
      addedEdges,
      removedEdges
    };
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  SnapshotManager
});
//# sourceMappingURL=snapshot.js.map
