export const MS_PER_DAY = 86_400_000;
const SQLITE_LOCAL_DATETIME = /^(\d{4}-\d{2}-\d{2})[ T](\d{2}:\d{2}:\d{2}(?:\.\d+)?)$/;
export function parseSqliteInstant(value) {
    if (value == null)
        return null;
    const trimmed = String(value).trim();
    if (trimmed === '')
        return null;
    const sqliteMatch = trimmed.match(SQLITE_LOCAL_DATETIME);
    if (sqliteMatch) {
        const isoUtc = `${sqliteMatch[1]}T${sqliteMatch[2]}Z`;
        const parsedUtc = new Date(isoUtc);
        if (!Number.isNaN(parsedUtc.getTime()))
            return parsedUtc;
    }
    const direct = new Date(trimmed);
    if (!Number.isNaN(direct.getTime()))
        return direct;
    return null;
}
export function resolveStaleAnchor(row) {
    const fromRecall = parseSqliteInstant(row.last_recalled_at);
    if (fromRecall) {
        return { instant: fromRecall, kind: 'last_recalled_at' };
    }
    const created = parseSqliteInstant(row.created_at);
    if (!created)
        return null;
    return { instant: created, kind: 'created_at_fallback' };
}
export function computeStaleDays(anchor, now) {
    const diffMs = now.getTime() - anchor.getTime();
    return Math.floor(diffMs / MS_PER_DAY);
}
export function computeStaleRatio(staleDays, thresholdStaleDays) {
    const denom = Math.max(thresholdStaleDays, 1);
    return Math.min(staleDays / denom, 3);
}
export function computePriority(importance, staleRatio) {
    return importance * 1000 + staleRatio * 100;
}
function isTruthyFlag(value) {
    return value === true || value === 1;
}
function isNonEmptyDeletedAt(value) {
    if (value == null)
        return false;
    return String(value).trim() !== '';
}
export function isMemoryRowActive(row) {
    if (isTruthyFlag(row.pinned))
        return false;
    if (isTruthyFlag(row.is_deleted))
        return false;
    if (isNonEmptyDeletedAt(row.deleted_at))
        return false;
    return true;
}
export function passesEligibility(row, options) {
    if (!isMemoryRowActive(row))
        return false;
    if (row.importance < options.importanceThreshold)
        return false;
    const resolved = resolveStaleAnchor(row);
    if (!resolved)
        return false;
    const staleDays = computeStaleDays(resolved.instant, options.now);
    return staleDays >= options.staleDays;
}
export function buildScoreBreakdown(row, options) {
    const resolved = resolveStaleAnchor(row);
    const stale_days = resolved ? computeStaleDays(resolved.instant, options.now) : 0;
    const anchor_kind = resolved?.kind ?? 'created_at_fallback';
    return {
        importance: row.importance,
        stale_days,
        anchor_kind,
        threshold_importance: options.importanceThreshold,
        threshold_stale_days: options.staleDays,
    };
}
export function buildReason(breakdown) {
    return (`eligible: importance=${breakdown.importance.toFixed(3)}>=${breakdown.threshold_importance}, ` +
        `stale=${breakdown.stale_days}d>=${breakdown.threshold_stale_days}d, anchor=${breakdown.anchor_kind}`);
}
//# sourceMappingURL=memory-review-candidate-selection-scoring.js.map