// src/memory-worth.ts
var MAX_COUNTER = 1e12;
function classifyCounter(value) {
  if (value === void 0) return { ok: true, value: 0 };
  if (typeof value !== "number") return { ok: false };
  if (!Number.isFinite(value)) return { ok: false };
  if (value < 0) return { ok: false };
  if (!Number.isInteger(value)) return { ok: false };
  if (value > MAX_COUNTER) return { ok: false };
  return { ok: true, value };
}
function parseLastAccessedMs(lastAccessed) {
  if (!lastAccessed) return null;
  const parsed = Date.parse(lastAccessed);
  if (!Number.isFinite(parsed)) return null;
  return parsed;
}
function decayFactor(ageMs, halfLifeMs) {
  if (typeof halfLifeMs !== "number") return 1;
  if (!Number.isFinite(halfLifeMs)) return 1;
  if (halfLifeMs <= 0) return 1;
  if (ageMs <= 0) return 1;
  return Math.pow(2, -ageMs / halfLifeMs);
}
function computeMemoryWorth(input) {
  const sClass = classifyCounter(input.mw_success);
  const fClass = classifyCounter(input.mw_fail);
  if (!sClass.ok || !fClass.ok) {
    return { score: 0.5, p_success: 0.5, confidence: 0 };
  }
  const rawS = sClass.value;
  const rawF = fClass.value;
  const lastAccessedMs = parseLastAccessedMs(input.lastAccessed);
  const nowMs = input.now.getTime();
  const nowUsable = Number.isFinite(nowMs);
  const ageMs = !nowUsable || lastAccessedMs === null ? 0 : Math.max(0, nowMs - lastAccessedMs);
  const factor = nowUsable ? decayFactor(ageMs, input.halfLifeMs) : 1;
  const sEff = rawS * factor;
  const fEff = rawF * factor;
  const pSuccess = (sEff + 1) / (sEff + fEff + 2);
  const clamped = Math.max(0, Math.min(1, pSuccess));
  return {
    score: clamped,
    p_success: clamped,
    confidence: sEff + fEff
  };
}

export {
  computeMemoryWorth
};
//# sourceMappingURL=chunk-IISBCCWR.js.map