// src/memory-lifecycle-ledger-utils.ts
import path from "path";
var MEMORY_LIFECYCLE_RULE_VERSION = "memory-lifecycle-ledger.v1";
var MEMORY_LIFECYCLE_EVENT_SORT_ORDER = {
  created: 0,
  updated: 1,
  promoted: 2,
  explicit_capture_accepted: 3,
  explicit_capture_queued: 4,
  imported: 5,
  merged: 6,
  restored: 7,
  superseded: 8,
  rejected: 9,
  archived: 10
};
function toMemoryPathRel(baseDir, filePath) {
  if (!baseDir) return filePath.split(path.sep).join("/");
  return path.relative(baseDir, filePath).split(path.sep).join("/");
}
function isArchivedMemoryPath(pathRel) {
  return pathRel === "archive" || pathRel.startsWith("archive/");
}
function inferMemoryStatus(frontmatter, pathRel, fallbackStatus = "active") {
  if (frontmatter.status && frontmatter.status !== "active") return frontmatter.status;
  if (frontmatter.archivedAt) return "archived";
  if (isArchivedMemoryPath(pathRel)) return "archived";
  if (frontmatter.status) return frontmatter.status;
  return fallbackStatus;
}
function isActiveMemoryStatus(status) {
  return status === void 0 || status === "active";
}
function summarizeMemoryLifecycleState(memory) {
  return {
    category: memory.frontmatter.category,
    path: memory.path,
    status: memory.frontmatter.status ?? "active",
    lifecycleState: memory.frontmatter.lifecycleState
  };
}
function makeRebuiltMemoryLifecycleEvent(memory, eventType, timestamp) {
  return {
    eventId: `rebuild-${memory.frontmatter.id}-${eventType}-${timestamp}`,
    memoryId: memory.frontmatter.id,
    eventType,
    timestamp,
    actor: "maintenance.rebuildMemoryLifecycleLedger",
    ruleVersion: MEMORY_LIFECYCLE_RULE_VERSION,
    after: summarizeMemoryLifecycleState(memory),
    relatedMemoryIds: [
      ...memory.frontmatter.supersededBy ? [memory.frontmatter.supersededBy] : [],
      ...memory.frontmatter.supersedes ? [memory.frontmatter.supersedes] : [],
      ...(memory.frontmatter.lineage ?? []).filter(Boolean)
    ]
  };
}
function buildLifecycleEventsForMemory(memory) {
  const events = [];
  const created = memory.frontmatter.created;
  const updated = memory.frontmatter.updated;
  const archivedAt = memory.frontmatter.archivedAt;
  const supersededAt = memory.frontmatter.supersededAt;
  const effectiveArchivedAt = archivedAt ?? (memory.frontmatter.status === "archived" && updated ? updated : void 0);
  events.push(makeRebuiltMemoryLifecycleEvent(memory, "created", created));
  if (updated && updated !== created && updated !== effectiveArchivedAt && updated !== supersededAt) {
    events.push(makeRebuiltMemoryLifecycleEvent(memory, "updated", updated));
  }
  if (supersededAt) {
    events.push(makeRebuiltMemoryLifecycleEvent(memory, "superseded", supersededAt));
  }
  if (effectiveArchivedAt) {
    events.push(makeRebuiltMemoryLifecycleEvent(memory, "archived", effectiveArchivedAt));
  }
  return events;
}
function sortMemoryLifecycleEvents(events) {
  return [...events].sort((a, b) => {
    if (a.memoryId !== b.memoryId) return a.memoryId.localeCompare(b.memoryId);
    if (a.timestamp !== b.timestamp) return a.timestamp.localeCompare(b.timestamp);
    return MEMORY_LIFECYCLE_EVENT_SORT_ORDER[a.eventType] - MEMORY_LIFECYCLE_EVENT_SORT_ORDER[b.eventType];
  });
}

export {
  MEMORY_LIFECYCLE_RULE_VERSION,
  MEMORY_LIFECYCLE_EVENT_SORT_ORDER,
  toMemoryPathRel,
  isArchivedMemoryPath,
  inferMemoryStatus,
  isActiveMemoryStatus,
  summarizeMemoryLifecycleState,
  makeRebuiltMemoryLifecycleEvent,
  buildLifecycleEventsForMemory,
  sortMemoryLifecycleEvents
};
//# sourceMappingURL=chunk-RULE4VG5.js.map