import "./chunk-PZ5AY32C.js";

// src/maintenance/forget.ts
var ForgetMemoryNotFoundError = class extends Error {
  code = "memory_not_found";
  constructor(id) {
    super(`memory not found: ${id}`);
    this.name = "ForgetMemoryNotFoundError";
  }
};
var ForgetMemoryAlreadyForgottenError = class extends Error {
  code = "already_forgotten";
  constructor(id, forgottenAt) {
    super(`memory ${id} was already forgotten at ${forgottenAt}`);
    this.name = "ForgetMemoryAlreadyForgottenError";
  }
};
async function forgetMemory(storage, request) {
  const id = typeof request.id === "string" ? request.id.trim() : "";
  if (id.length === 0) {
    throw new Error("forget: memory id is required and must be non-empty");
  }
  const memory = await findMemoryById(storage, id);
  if (!memory) {
    throw new ForgetMemoryNotFoundError(id);
  }
  if (memory.frontmatter.status === "forgotten") {
    throw new ForgetMemoryAlreadyForgottenError(
      id,
      memory.frontmatter.forgottenAt ?? "(unknown)"
    );
  }
  const priorStatus = typeof memory.frontmatter.status === "string" ? memory.frontmatter.status : "active";
  const now = (request.now ?? (() => /* @__PURE__ */ new Date()))();
  const forgottenAt = now.toISOString();
  const reason = typeof request.reason === "string" ? request.reason.trim() : "";
  await storage.writeMemoryFrontmatter(memory, {
    status: "forgotten",
    forgottenAt,
    forgottenReason: reason.length > 0 ? reason : void 0,
    updated: forgottenAt
  }, {
    actor: "remnic-forget",
    reasonCode: "operator_forget"
  });
  return {
    id,
    path: memory.path,
    priorStatus,
    forgottenAt,
    reason
  };
}
async function findMemoryById(storage, id) {
  const hot = await storage.readAllMemories();
  const hotMatch = hot.find((m) => m.frontmatter.id === id);
  if (hotMatch) return hotMatch;
  const archived = await storage.readArchivedMemories();
  const archivedMatch = archived.find((m) => m.frontmatter.id === id);
  if (archivedMatch) return archivedMatch;
  const cold = await storage.readAllColdMemories();
  return cold.find((m) => m.frontmatter.id === id) ?? null;
}
export {
  ForgetMemoryAlreadyForgottenError,
  ForgetMemoryNotFoundError,
  forgetMemory
};
//# sourceMappingURL=forget-PLR6J5DN.js.map