/**
 * Memory store/dedup helpers — used by MemoryEngine for tag normalisation,
 * safe JSON parsing, and deduplication during store operations.
 *
 * Also owns seedDemoMemoriesOnFirstBoot, called from MemoryEngine constructor.
 */
import * as path from 'node:path';
// ─── First-boot demo seeding ──────────────────────────────────────────────────
/**
 * Seed 5 welcome memories the very first time a new MemoryEngine is
 * constructed on this machine.  No-op if already seeded.
 *
 * Called from MemoryEngine constructor (synchronous, best-effort).
 */
export function seedDemoMemoriesOnFirstBoot(engine) {
    if (engine.isFirstRunSeen())
        return;
    const workspaceRoot = process.env['NEXUS_WORKSPACE_ROOT']?.trim();
    const repoName = path.basename(workspaceRoot || process.cwd());
    const seeds = [
        {
            content: 'This is Nexus Prime — your control plane for coding agents.',
            tags: ['#welcome', '#demo'],
        },
        {
            content: `Repo detected: \`${repoName}\`. Nexus will remember everything it learns about this codebase across sessions.`,
            tags: ['#welcome', '#demo', '#repo-intro'],
        },
        {
            content: 'Memory forms automatically on every tool call. The pyramid in the Memory view shows 3-tier retention: prefrontal (active) · hippocampus (episodic) · cortex (long-term).',
            tags: ['#welcome', '#demo'],
        },
        {
            content: 'Press ⌘K to open the command bar. Press ⌘⏎ to delegate a task to a specialist.',
            tags: ['#welcome', '#demo'],
        },
        {
            content: 'The Memory view shows everything Nexus has learned about this repo. Promoted memories survive session boundaries.',
            tags: ['#welcome', '#demo'],
        },
    ];
    let seeded = 0;
    for (const seed of seeds) {
        try {
            engine.store(seed.content, 0.3, seed.tags);
            seeded++;
        }
        catch {
            // best-effort — never crash the constructor
        }
    }
    if (seeded > 0) {
        try {
            engine.markFirstRunSeen();
        }
        catch { /* best-effort */ }
    }
}
// ─── Tag / object helpers ─────────────────────────────────────────────────────
export function safeParseTags(raw) {
    try {
        const parsed = JSON.parse(raw);
        return Array.isArray(parsed) ? parsed.filter((value) => typeof value === 'string') : [];
    }
    catch {
        return [];
    }
}
export function safeParseObject(raw) {
    try {
        if (typeof raw === 'string' && raw.trim()) {
            const parsed = JSON.parse(raw);
            return parsed && typeof parsed === 'object' ? parsed : {};
        }
        return raw && typeof raw === 'object' ? raw : {};
    }
    catch {
        return {};
    }
}
export function dedupeStrings(values) {
    return [...new Set(values.filter(Boolean))];
}
export function isOperatorVisibleTag(tag) {
    return !['#phantom-learning', '#swarm', '#repo-profile', '#hidden', '#system-hidden', '#system'].includes(tag);
}
