import {
  ensureMemoryDir
} from "./chunk-ND6MY37M.js";
import {
  generateMemId
} from "./chunk-RIAAGL2E.js";
import {
  MEMORY_PATTERNS,
  appendNode
} from "./chunk-DFR4DJBM.js";
import {
  ErrorCodes,
  createError
} from "./chunk-RXRKBBSM.js";

// ../memory/dist/mem-checkpoint-core.js
var ERROR_MESSAGES = {
  NOTE_REQUIRED: "note is required",
  NOTE_EMPTY: "note cannot be empty",
  WU_ID_INVALID: "Invalid WU ID format. Expected pattern: WU-XXX (e.g., WU-123)"
};
var NODE_TYPE_CHECKPOINT = "checkpoint";
var LIFECYCLE_SESSION = "session";
function isValidWuId(wuId) {
  if (!wuId)
    return true;
  return MEMORY_PATTERNS.WU_ID.test(wuId);
}
function generateCheckpointContent(note) {
  return `Checkpoint: ${note}`;
}
async function createCheckpoint(baseDir, options) {
  const { note, sessionId, wuId, progress, nextSteps, trigger, gitDiffStat } = options;
  if (note == null) {
    throw createError(ErrorCodes.VALIDATION_ERROR, ERROR_MESSAGES.NOTE_REQUIRED);
  }
  if (note === "") {
    throw createError(ErrorCodes.VALIDATION_ERROR, ERROR_MESSAGES.NOTE_EMPTY);
  }
  if (wuId && !isValidWuId(wuId)) {
    throw createError(ErrorCodes.INVALID_WU_ID, ERROR_MESSAGES.WU_ID_INVALID);
  }
  const memoryDir = await ensureMemoryDir(baseDir);
  const timestamp = (/* @__PURE__ */ new Date()).toISOString();
  const content = generateCheckpointContent(note);
  const idContent = `${content}-${timestamp}`;
  const id = generateMemId(idContent);
  const metadata = {};
  if (progress) {
    metadata.progress = progress;
  }
  if (nextSteps) {
    metadata.nextSteps = nextSteps;
  }
  if (trigger) {
    metadata.trigger = trigger;
  }
  if (gitDiffStat) {
    metadata.gitDiffStat = gitDiffStat;
  }
  const checkpointNode = {
    id,
    type: NODE_TYPE_CHECKPOINT,
    lifecycle: LIFECYCLE_SESSION,
    content,
    created_at: timestamp
  };
  if (wuId) {
    checkpointNode.wu_id = wuId;
  }
  if (sessionId) {
    checkpointNode.session_id = sessionId;
  }
  if (Object.keys(metadata).length > 0) {
    checkpointNode.metadata = metadata;
  }
  await appendNode(memoryDir, checkpointNode);
  return {
    success: true,
    checkpoint: checkpointNode
  };
}

export {
  createCheckpoint
};
